【发布时间】:2017-06-19 15:19:35
【问题描述】:
我在使用 C# 的 WPF 项目中使用 mpusbapi.dll。 在 C++ 中,函数原型是:
DWORD MPUSBWrite(HANDLE handle,
PVOID pData,
DWORD dwLen,
PDWORD pLength,
DWORD dwMilliseconds);
而我在 C# 中的 p/invoke 是:
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBWrite(void* handle, void* pData, DWORD dwLen, DWORD* pLength, DWORD dwMilliseconds);
所以,因为我没有让我的所有项目都不安全,所以我想构建下一个发送数据的方法:
unsafe private void SendPacket(byte* SendData, UInt32 SendLength)
{
uint SendDelay = 10;
UInt32 SentDataLength;
openPipes();
MPUSBWrite(myOutPipe, &SendData, SendLength, &SentDataLength, SendDelay);
closePipes();
}
但 Visual Studios 向我显示参数变量类型的错误。 “无法从 'byte**' 转换为 'system.IntPtr'”和“无法从 'uint*' 转换为 'system.IntPtr'”。 我是使用 C# 的新手,现在指针让我卡住了。我应该如何将 c++ 参数转换为 C#?谢谢!
编辑: 我没有注意到我已将 Pinvoke 更改为:
[DllImport("mpusbapi.dll", CallingConvention = CallingConvention.Cdecl)]
static extern UInt32 _MPUSBWrite(IntPtr handle,IntPtr pData, UInt32 dwLen,IntPtr pLength, UInt32 dwMilliseconds);
添加: 删除方法调用中的 & 并没有修复最后的错误,并添加了这个“使用未分配的局部变量 'SendDataLenght'”。
【问题讨论】:
-
尝试删除方法调用中的
&,例如_MPUSBWrite(myOutPipe, SendData, SendLength, SentDataLength, SendDelay);...如果您还有其他错误,请将其添加到您的问题中。