Calling DeviceIoControl from C# with IOCTL_DVD_* Control Codes

偶尔善良 提交于 2019-12-01 00:25:38
arbiter

The problem lies here:

result = DeviceIoControl(_hdev, CTL_CODE(0x00000033, 0x0400, 0, 1),
   IntPtr.Zero, 0, (IntPtr)sessionId, Marshal.SizeOf(sessionId),
   out bytesReturned, IntPtr.Zero);

Driver expects pointer to buffer in lpOutBuffer, but you instead provide sessionId itself (which is zero). Of course this will not work.

Here what you need to do:

IntPtr buffer = Marshal.AllocHGlobal(sizeof(int));
result = DeviceIoControl(_hdev, CTL_CODE(0x00000033, 0x0400, 0, 1),
    IntPtr.Zero, 0, buffer, sizeof(int), out bytesReturned, IntPtr.Zero);
int sessionId = Marshal.ReadInt32(buffer);
Marshal.FreeHGlobal(buffer);

BTW, the same applies to all following DeviceIoControl calls, you again provide value, when you need provide pointer to value. And you also need to check if your CTL_CODE function builds valid io code.

Again, DeviceIoControl expects pointers to buffers for in and out structures.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!