如何在托管 (C#) 和非托管 (C++) 代码之间来回传递数组内容
How to pass array contents back and forth between managed (C#) and unmanaged (C++) code
我正在尝试将一个浮点数组从 C# 传递到一个 C++ dll,它将对该数组做一些工作,然后以浮点数组的形式传回一个答案(不一定是相同的长度).
我有代码可以传递数组,修改它,然后 return 它,但是数组中的值并不像预期的那样,尽管数组看起来格式正确...
下面的示例采用一个浮点数组,将其传递给 C++。在 C++ 中,值 2 被添加到数组的每个元素以模拟完成的工作,结果使用回调 returned 到 C#。
每次都会 return 编辑一组不同的值。
一组典型的 returned 值是:-
81.58881, 2, 75.75787, 4
如有任何想法,我们将不胜感激..
干杯
格雷厄姆
C#
public delegate void CallBack1(IntPtr param, int len);
[DllImport("TestDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void testCPlusSide(CallBack1 cb, IntPtr arr, int len);
private void button_Click(object sender, EventArgs e)
{
float[] dataIn = new float[] { 0, 0.5f, 7.79f, 46 };
GCHandle gch = GCHandle.Alloc(dataIn);
CallBack1 testCB = new CallBack1(TestCallBack);
testCPlusSide(testCB, GCHandle.ToIntPtr(gch), dataIn.Length);
gch.Free();
}
void TestCallBack(IntPtr param, int len)
{
float[] vals = new float[len];
GCHandle gch = GCHandle.FromIntPtr(param);
Marshal.Copy(param, vals, 0, len);
StringBuilder sb = new StringBuilder();
foreach (float item in vals)
{
sb.Append(item.ToString() + "\n");
}
MessageBox.Show(sb.ToString());
}
C++
typedef void(__stdcall * CallBack1)(float * arr, int len);
__declspec(dllexport) void testCPlusSide(CallBack1 cb, float* arr, int len)
{
for (int i = 0; i < len; i++)
{
arr[i] += 2;
}
cb(arr, len);
}
更新
我添加了一个临时 float
来读取 C++ 端数组的内容,我发现数组 C++ 端的内容与传递的值没有关系——我不知道为什么....
我已经设法让事情正常进行。
我更改了 GCHandle.Alloc 方法,因此它创建了一个固定对象指针并且一切正常。
我还删除了 TestCallBackFunction 中不必要的 GCHandle 调用
完整更改的代码是:-
C#
public delegate void CallBack1(IntPtr param, int len);
[DllImport("TestDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void testCPlusSide(CallBack1 cb, IntPtr arr, int len);
private void button_Click(object sender, EventArgs e)
{
float[] dataIn = new float[] { 0, 0.5f, 7.79f, 46 };
GCHandle gch = GCHandle.Alloc(dataIn,GCHandleType.Pinned);
CallBack1 testCB = new CallBack1(TestCallBack);
testCPlusSide(testCB, gch.AddrOfPinnedObject(), dataIn.Length);
gch.Free();
}
void TestCallBack(IntPtr param, int len)
{
float[] vals = new float[len];
Marshal.Copy(param, vals, 0, len);
StringBuilder sb = new StringBuilder();
foreach (float item in vals)
{
sb.Append(item.ToString() + "\n");
}
MessageBox.Show(sb.ToString());
}
C++
typedef void(__stdcall * CallBack1)(float * arr, int len);
__declspec(dllexport) void testCPlusSide(CallBack1 cb, float* arr, int len)
{
for (int i = 0; i < len; i++)
{
arr[i] += 2;
}
cb(arr, len);
}
感谢 Carlos 和 rhughes 的帮助
干杯
格雷厄姆
我正在尝试将一个浮点数组从 C# 传递到一个 C++ dll,它将对该数组做一些工作,然后以浮点数组的形式传回一个答案(不一定是相同的长度).
我有代码可以传递数组,修改它,然后 return 它,但是数组中的值并不像预期的那样,尽管数组看起来格式正确...
下面的示例采用一个浮点数组,将其传递给 C++。在 C++ 中,值 2 被添加到数组的每个元素以模拟完成的工作,结果使用回调 returned 到 C#。
每次都会 return 编辑一组不同的值。
一组典型的 returned 值是:-
81.58881, 2, 75.75787, 4
如有任何想法,我们将不胜感激..
干杯
格雷厄姆
C#
public delegate void CallBack1(IntPtr param, int len);
[DllImport("TestDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void testCPlusSide(CallBack1 cb, IntPtr arr, int len);
private void button_Click(object sender, EventArgs e)
{
float[] dataIn = new float[] { 0, 0.5f, 7.79f, 46 };
GCHandle gch = GCHandle.Alloc(dataIn);
CallBack1 testCB = new CallBack1(TestCallBack);
testCPlusSide(testCB, GCHandle.ToIntPtr(gch), dataIn.Length);
gch.Free();
}
void TestCallBack(IntPtr param, int len)
{
float[] vals = new float[len];
GCHandle gch = GCHandle.FromIntPtr(param);
Marshal.Copy(param, vals, 0, len);
StringBuilder sb = new StringBuilder();
foreach (float item in vals)
{
sb.Append(item.ToString() + "\n");
}
MessageBox.Show(sb.ToString());
}
C++
typedef void(__stdcall * CallBack1)(float * arr, int len);
__declspec(dllexport) void testCPlusSide(CallBack1 cb, float* arr, int len)
{
for (int i = 0; i < len; i++)
{
arr[i] += 2;
}
cb(arr, len);
}
更新
我添加了一个临时 float
来读取 C++ 端数组的内容,我发现数组 C++ 端的内容与传递的值没有关系——我不知道为什么....
我已经设法让事情正常进行。
我更改了 GCHandle.Alloc 方法,因此它创建了一个固定对象指针并且一切正常。
我还删除了 TestCallBackFunction 中不必要的 GCHandle 调用
完整更改的代码是:-
C#
public delegate void CallBack1(IntPtr param, int len);
[DllImport("TestDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void testCPlusSide(CallBack1 cb, IntPtr arr, int len);
private void button_Click(object sender, EventArgs e)
{
float[] dataIn = new float[] { 0, 0.5f, 7.79f, 46 };
GCHandle gch = GCHandle.Alloc(dataIn,GCHandleType.Pinned);
CallBack1 testCB = new CallBack1(TestCallBack);
testCPlusSide(testCB, gch.AddrOfPinnedObject(), dataIn.Length);
gch.Free();
}
void TestCallBack(IntPtr param, int len)
{
float[] vals = new float[len];
Marshal.Copy(param, vals, 0, len);
StringBuilder sb = new StringBuilder();
foreach (float item in vals)
{
sb.Append(item.ToString() + "\n");
}
MessageBox.Show(sb.ToString());
}
C++
typedef void(__stdcall * CallBack1)(float * arr, int len);
__declspec(dllexport) void testCPlusSide(CallBack1 cb, float* arr, int len)
{
for (int i = 0; i < len; i++)
{
arr[i] += 2;
}
cb(arr, len);
}
感谢 Carlos 和 rhughes 的帮助
干杯
格雷厄姆