简体   繁体   English

将指向DWORD的指针从C#传递到C ++ DLL

[英]Pass pointer to DWORD from c# to c++ DLL

I'm trying to call a unmanaged function from a DLL written in C++ from a C# console application. 我正在尝试从C#控制台应用程序以C ++编写的DLL调用非托管函数。 I have managed to do so for a couple of simple method calls however one of the functions takes a parameter of void* and I'm not sure what to pass it to get it to work. 我已经设法通过几个简单的方法调用做到了这一点,但是其中一个函数采用了void*参数,而且我不确定该如何传递它才能使其正常工作。

C++ method signature C ++方法签名

BOOL,   SetData,      (INT iDevice, BYTE iCmd, VOID* pData, DWORD nBytes)

C# method signature C#方法签名

[DllImport("NvUSB.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static unsafe extern bool SetData(int iDevice, byte iCmd, void* pData, uint nBytes);

Working C++ Call 工作C ++调用

DWORD pDatatoHi[] = { 'R','E','B','O','O','T' };
SetData(0, 0, pDatatoHi, sizeof(pDatatoHi))

Not working C# 无法使用C#

uint* cmd = stackalloc uint[6];

cmd[0] = 'R';
cmd[1] = 'E';
cmd[2] = 'B';
cmd[3] = 'O';
cmd[4] = 'O';
cmd[5] = 'T';

SetData(address, 0, cmd, 6);

The method I am trying to call from the unmanaged DLL should reboot a USB device, when the function is called as per the C++ example above the device reboots correctly. 我尝试从非托管DLL调用的方法应按照设备上方的C ++示例调用该函数时重新引导USB设备,然后正确重新引导。 When I call the C# version above the code executes and returns true, however the device does not reboot. 当我在上面调用C#版本时,代码将执行并返回true,但是设备不会重新启动。 I suspect this is due to the way in which I am passing the pData parameter? 我怀疑这是由于我传递pData参数的方式造成的?

当您调用SetData(address, 0, cmd, 6) ,字节数是24,而不是6。6是项数,而不是数组的大小,这就是您在C ++示例中传递的内容,给了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM