C# 调用api 问题

RainStarNetwork 2011-02-18 04:53:17
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() ;
}
...全文
93 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2011-02-18
  • 打赏
  • 举报
回复
[DllImport("kernel32.dll", EntryPoint="DeviceIoControl")]
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
RainStarNetwork 2011-02-18
  • 打赏
  • 举报
回复
WINAPI
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
  • 打赏
  • 举报
回复
应该将API原始参数贴出来,才能对照
RainStarNetwork 2011-02-18
  • 打赏
  • 举报
回复
参数转化没有问题,都是http://pinvoke.net/default.aspx/kernel32/DeviceIoControl.html上的标准类型了
garfieldzf 2011-02-18
  • 打赏
  • 举报
回复

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧