我是 .NET 紧凑型框架的新手。我需要调用 DeviceIoControl 函数并将结构作为输入和输出参数传递给 IOControl 函数。
在PInvoke/DeviceIoControl中,我发现了如何访问函数本身。但是我怎样才能将指针作为结构InBuf
和OutBuf
参数传递呢?
DeviceIoControl 定义为 P/Invoke:
[DllImport("coredll", EntryPoint = "DeviceIoControl", SetLastError = true)]
internal static extern int DeviceIoControlCE(
int hDevice, int dwIoControlCode,
byte[] lpInBuffer, int nInBufferSize,
byte[] lpOutBuffer, int nOutBufferSize,
ref int lpBytesReturned, IntPtr lpOverlapped);
有问题的结构具有以下布局:
struct Query
{
int a;
int b;
char x[8];
}
struct Response
{
int result;
uint32 success;
}
void DoIoControl ()
{
Query q = new Query();
Response r = new Response();
int inSize = System.Runtime.InteropServices.Marshal.SizeOf(q);
int outSize = System.Runtime.InteropServices.Marshal.SizeOf(r);
NativeMethods.DeviceIoControlCE((int)handle, (int)IOCTL_MY.CODE,
ref q, inSize, ref r, outSize, ref bytesReturned, IntPtr.Zero);
}
编辑:当我尝试编译此代码时,出现错误:
cannot convert from 'ref MyNamespace.Response' to 'byte[]'
如何将结构的地址传递给 DeviceIoControl 函数什么期望指向字节的指针而不是 struct ref?