我试图让 C++ 函数在 C# 中调用表单上的方法以更新 GUI。出于某种原因,来自 C++ 的函数调用以相反的顺序发送参数。
因此,UpdateDROCallback()
当使用 function(1.0, 2.0, 3.0) 调用该函数时,第一个参数为 3.0,第二个参数为 2.0,最后一个参数为 1.0。
我在这里想念什么?
[C#]
public partial class Form1 : Form
{
delegate void DROUpdateDelegate(double x, double y, double z);
DROUpdateDelegate m_DROUpdateDelegate;
static DROUpdateDelegate s_DROUpdateDelegate;
public Form1()
{
InitializeComponent();
m_DROUpdateDelegate = new DROUpdateDelegate(UpdateDROCallback);
s_DROUpdateDelegate = new DROUpdateDelegate(UpdateDRO);
}
private void btnGo_Click(object sender, EventArgs e)
{
int address = m_DROUpdateDelegate.Method.MethodHandle.GetFunctionPointer().ToInt32();
TestDll.RegisterScaleUpdateCallback(address);
}
private static void UpdateDROCallback(double x, double y, double z)
{
s_DROUpdateDelegate(x, y, z);
}
private void UpdateDRO(double x, double y, double z)
{
BeginInvoke(
new Action(
() =>
{
lblDROX.Text = x.ToString("0.0000");
lblDROY.Text = y.ToString("0.0000");
lblDROZ.Text = z.ToString("0.0000");
}));
}
}
测试DLL.cs:
public static class TestDll
{
(...)
[DllImport("test.dll", EntryPoint = "RegisterScaleUpdateCallback")]
public static extern void RegisterScaleUpdateCallback(int address);
(...)
}
[C++]
频闪测试.h:
#pragma once
class StrobeTest
{
typedef void (__stdcall *DROUpdate)(double x, double y, double z);
private:
static DROUpdate s_CallbackFunction;
public:
StrobeTest(void);
~StrobeTest(void);
static void InitializeStrobe(void);
static void MoveXAtSpeed(double velocity);
static void CALLBACK RegisterScaleUpdateCallback(DROUpdate function);
};
频闪测试.cpp
(...)
void StrobeTest::RegisterScaleUpdateCallback(DROUpdate function)
{
StrobeTest::s_CallbackFunction = function;
StrobeTest::s_CallbackFunction(1.0, 2.0, 3.0);
}
(...)