为什么 DeviceIoControl 从 C# 中抛出错误 21(设备未就绪),而 C 中的等效项工作正常?
我正在尝试使用 C# 向 GPS 中间驱动程序服务发送 IOCTL_SERVICE_REFRESH 命令,如下所示:
handle = CreateFile("GPD0:",
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero);
if (handle.ToInt32() == INVALID_HANDLE_VALUE)
{
rc = Marshal.GetLastWin32Error();
return rc;
}
int numBytesReturned = 0;
rc = DeviceIoControl(
handle,
IOCTL_SERVICE_REFRESH,
null,
0,
null,
0,
ref numBytesReturned,
IntPtr.Zero);
int error = Marshal.GetLastWin32Error();
GetLastWin32Error 总是给出错误 21(设备未就绪)。然而,从 C++ 进行的等效调用工作正常:
HANDLE hGPS = CreateFile(L"GPD0:", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hGPS != INVALID_HANDLE_VALUE) {
BOOL ret = DeviceIoControl(hGPS,IOCTL_SERVICE_REFRESH,0,0,0,0,0,0);
DWORD err = GetLastError();
}
我怀疑 PInvoke 签名有问题,但我似乎在这里找不到任何错误:
[DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
public static extern int DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
byte[] lpInBuffer,
int nInBufferSize,
byte[] lpOutBuffer,
int nOutBufferSize,
ref int lpBytesReturned,
IntPtr lpOverlapped);
[DllImport("coredll.dll", SetLastError = true)]
public static extern IntPtr CreateFile(
String lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr attr,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
我在这里缺少什么?
I'm trying to send an IOCTL_SERVICE_REFRESH command to the GPS Intermediate Driver service using C# like this:
handle = CreateFile("GPD0:",
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero);
if (handle.ToInt32() == INVALID_HANDLE_VALUE)
{
rc = Marshal.GetLastWin32Error();
return rc;
}
int numBytesReturned = 0;
rc = DeviceIoControl(
handle,
IOCTL_SERVICE_REFRESH,
null,
0,
null,
0,
ref numBytesReturned,
IntPtr.Zero);
int error = Marshal.GetLastWin32Error();
GetLastWin32Error always gives me error 21 (device not ready). However, the equivalent call when made from C++ works fine:
HANDLE hGPS = CreateFile(L"GPD0:", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hGPS != INVALID_HANDLE_VALUE) {
BOOL ret = DeviceIoControl(hGPS,IOCTL_SERVICE_REFRESH,0,0,0,0,0,0);
DWORD err = GetLastError();
}
I suspected a problem with the PInvoke signatures, but I can't seem to find anything wrong here:
[DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
public static extern int DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
byte[] lpInBuffer,
int nInBufferSize,
byte[] lpOutBuffer,
int nOutBufferSize,
ref int lpBytesReturned,
IntPtr lpOverlapped);
[DllImport("coredll.dll", SetLastError = true)]
public static extern IntPtr CreateFile(
String lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr attr,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论