在 CSharp 中使用正确的数据类型加载C++ dll
Load C++ dll in CSharp with correct datatypes
需要从 CSharp 中的 DLL 加载此C++方法,我想知道我必须使用哪些数据类型?
WORD FunA (BYTE Num, BYTE *pFrameTX, DWORD nbbitTX, BYTE
*pFrameRX, DWORD *pnbbitRX)
我的第一个方法是:
[DllImport("Example.Dll")]
public static extern UInt16 FunA(byte Num, Byte[] pFrameTX, UInt32 nbbitTX, ref Byte[] pFrameRX, ref UInt32 pnbbitRX);
Byte[] toSend = new Byte[1], toReceive = new Byte[1024];
toSend[0] = 0x26;
UInt32 numberOfBitsReceived = 0;
FunA(Convert.ToByte(1), toSend, 0, ref toReceive, ref numberOfBitsReceived);
这是怎么回事?有人可以帮助我找到正确的数据类型和调用用法吗?!
谢谢!
猜猜你错过了pFrameTX前面的ref修饰符。
[DllImport("Example.Dll")]
public static extern UInt16 FunA(byte Num, ref Byte[] pFrameTX, UInt32
nbbitTX, ref Byte[] pFrameRX, ref UInt32 pnbbitRX);
[DllImport("Example.Dll")]
public static extern UInt16 FunA(byte Num, IntPtr pFrameTX, UInt32
nbbitTX, IntPtr pFrameRX, ref UInt32 pnbbitRX);
// ...
Byte[] toSend = new Byte[1], toReceive = new Byte[1024];
toSend[0] = 0x26;
UInt32 numberOfBitsReceived = 0;
// reserve unmanaged memory for IntPtr
IntPtr toSendPtr = Marshal.AllocHGlobal(Marshal.SizeOf(toSend[0])*toSend.Length),
toReceivePtr = Marshal.AllocHGlobal(Marshal.SizeOf(toReceive[0])*toReceive.Length);
// copy send buffer to Unmanaged memory
Marshal.Copy(toSend, 0, toSendPtr, toSend.Length);
// call C++ DLL method
FunA(Convert.ToByte(1), toSendPtr, 0, toReceivePtr, ref numberOfBitsReceived);
// copy receive buffer from Unmanaged to managed memory
Marshal.Copy(toReceivePtr, toReceive, 0, numberOfBitsReveived/8);
// free memory
Marshal.FreeHGlobal(toSendPtr);
Marshal.FreeHGlobal(toReceivePtr);
相关文章:
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 有效地将数据加载到 std::vector 中<char>
- C++WIN32-将RTF数据加载到Rich Edit控件
- 增强序列化,按基类型加载存档类会产生错误的数据
- 将数据从 c++ 加载到 QML Scatter3d 项
- 如何动态加载和调用具有特定于库的类型作为函数参数的符号
- 如何将训练数据从UCI加载到OpenCV?
- 将数据预加载到RAM中进行快速交易
- 在 CSharp 中使用正确的数据类型加载C++ dll
- ATL ActiveX DLL 作为映射类型图像和数据加载了两次
- 如何将数据文件加载到自动Excel工作表文档
- 从文件中读取结果数据,加载它,比较它,然后打印显示
- 从void*数据blob加载sqlite数据库
- 如何在运行时通过插件机制动态加载数据类型
- 阻止或阻塞cpu数据缓存加载
- 使用加载和保存方法对不同命名空间中的单个数据类型进行提升
- 如何将全局内存中的数据安全地加载到 CUDA 中的共享内存中
- 从文本文件加载不同的数据类型