c#通过调用api访问usb设备
其中调用DeviceIoControl函数时总是返回错误号87,意味着参数错误,请问是什么问题呢?谢谢,代码如下
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
ref long InBuffer,
int nInBufferSize,
ref long OutBuffer,
int nOutBufferSize,
ref int pBytesReturned,
ref NativeOverlapped lpOverlapped); private long ResetPort(IntPtr hDevice)
{
int outByte = 0;
System.Threading.NativeOverlapped overlapped = new System.Threading.NativeOverlapped(); overlapped.InternalLow = IntPtr.Zero;
overlapped.InternalHigh = IntPtr.Zero;
overlapped.OffsetLow = 0;
overlapped.OffsetHigh = 0;
overlapped.EventHandle = CreateEvent(IntPtr.Zero, true, false, null); long inbuffer = 0;
DeviceIoControl(hDevice, IOCTL_SCSI_PASS_THROUGH_DIRECT, ref inbuffer, 0, ref inbuffer, 0, ref outByte, ref overlapped); return Marshal.GetLastWin32Error() ;
}
其中调用DeviceIoControl函数时总是返回错误号87,意味着参数错误,请问是什么问题呢?谢谢,代码如下
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
ref long InBuffer,
int nInBufferSize,
ref long OutBuffer,
int nOutBufferSize,
ref int pBytesReturned,
ref NativeOverlapped lpOverlapped); private long ResetPort(IntPtr hDevice)
{
int outByte = 0;
System.Threading.NativeOverlapped overlapped = new System.Threading.NativeOverlapped(); overlapped.InternalLow = IntPtr.Zero;
overlapped.InternalHigh = IntPtr.Zero;
overlapped.OffsetLow = 0;
overlapped.OffsetHigh = 0;
overlapped.EventHandle = CreateEvent(IntPtr.Zero, true, false, null); long inbuffer = 0;
DeviceIoControl(hDevice, IOCTL_SCSI_PASS_THROUGH_DIRECT, ref inbuffer, 0, ref inbuffer, 0, ref outByte, ref overlapped); return Marshal.GetLastWin32Error() ;
}
DeviceIoControl(
__in HANDLE hDevice,
__in DWORD dwIoControlCode,
__in_bcount_opt(nInBufferSize) LPVOID lpInBuffer,
__in DWORD nInBufferSize,
__out_bcount_part_opt(nOutBufferSize, *lpBytesReturned) LPVOID lpOutBuffer,
__in DWORD nOutBufferSize,
__out_opt LPDWORD lpBytesReturned,
__inout_opt LPOVERLAPPED lpOverlapped
);WINBASEAPI
附上原始api
public static extern int DeviceIoControl (
int hDevice,
int dwIoControlCode,
ref int lpInBuffer,
int nInBufferSize,
ref int lpOutBuffer,
int nOutBufferSize,
ref int lpBytesReturned,
ref OVERLAPPED lpOverlapped
);http://stackoverflow.com/questions/1067216/calling-deviceiocontrol-from-c-with-ioctl-dvd-control-codes