为什么 DeviceIoControl 从 C# 中抛出错误 21(设备未就绪),而 C 中的等效项工作正常?

发布于 2024-10-09 04:38:53 字数 1611 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文