将IntPtr转化成byte[]
Dll函数原型:
BOOL UsbScan_ExchangData(PUCHAR pDataBuf, DWORD& dwDataLen , DWORD dwCtl);
调用方式:
[DllImport("MDcs.dll")]
private static extern bool UsbScan_ExchangData(byte[] pDataBuf, ref UInt32 dwDataLen, UInt32 dwCtl);
//检查设备存在功能
public bool IsValid(UIntPtr hDevDropEvent)
{
bool bRet = false;
UInt32 byLen = 0;
//bRet = UsbScan_ExchangData(???????????????, ref byLen, API_USBSCAN_DEVEXIT);
return bRet;
}
请问注释部分如何进行传递?
vc调用的函数如下:
BOOL CUScanDriver::IsValid(HANDLE hDevDropEvent)
{
BOOL bRet = FALSE;
ULONG dwLen;
bRet = m_Func( (PUCHAR)&hDevDropEvent,dwLen,API_USBSCAN_DEVEXIT);
return bRet;
}
------解决方案--------------------
至于题目上的问题:将IntPtr转化成byte[]
用Marshall类,满足你的要求哟~~
------解决方案--------------------
C# code
public static bool IsValid(IntPtr hDevDropEvent)
{
bool bRet = false;
UInt32 byLen = 0;
Byte[] bufHandle = new Byte[IntPtr.Size];
GCHandle gch = GCHandle.Alloc(bufHandle, GCHandleType.Pinned); //固定托管内存
Marshal.WriteIntPtr(Marshal.UnsafeAddrOfPinnedArrayElement(bufHandle, 0), hDevDropEvent);
bRet = UsbScan_ExchangData(bufHandle, ref byLen, API_USBSCAN_DEVEXIT);
gch.Free();
return bRet;
}