在C#中将IOCTL发送到64位机器

时间:2013-09-12 10:47:36

标签: c# windows-8 ioctl wdk wdf

我正在将IOCTL发送到我的键盘过滤器驱动程序,代码如下:

Guid GUID_DEVINTERFACE_KBFILTER = new Guid(0x3fb7299d, 0x6847, 0x4490, 0xb0, 0xc9, 0x99, 0xe0, 0x98, 0x6a, 0xb8, 0x86);
IntPtr handle = SetupDiGetClassDevs(ref GUID_DEVINTERFACE_KBFILTER, IntPtr.Zero, IntPtr.Zero, (int)(DiGetClassFlags.DIGCF_PRESENT | DiGetClassFlags.DIGCF_DEVICEINTERFACE));
if (handle != INVALID_HANDLE_VALUE)
{
    bool Success = true;
    int i = 0;
    while (Success)
    {
        SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
        deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData);

        // start the enumeration
        Success = SetupDiEnumDeviceInterfaces(handle, IntPtr.Zero, ref GUID_DEVINTERFACE_KBFILTER, (uint)i, ref deviceInterfaceData);
        if (Success)
        {
            // build a DevInfo Data structure
            SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA();
            devInfoData.cbSize = (uint)Marshal.SizeOf(devInfoData);

            // build a Device Interface Detail Data structure
            deviceInterfaceDetailData = new SP_DEVICE_INTERFACE_DETAIL_DATA();
            deviceInterfaceDetailData.cbSize = 4 + Marshal.SystemDefaultCharSize;

            // now we can get some more detailed information
            uint nRequiredSize = 0;
            int nBytes = BUFFER_SIZE;

            if (SetupDiGetDeviceInterfaceDetail(handle, ref deviceInterfaceData, ref deviceInterfaceDetailData, (uint)nBytes, out nRequiredSize, ref devInfoData))
            {
                uint ptrPrevious;
                CM_Get_Parent(out ptrPrevious, devInfoData.DevInst, 0);

                // Now we get the InstanceID of the USB level device
                IntPtr ptrInstanceBuf = Marshal.AllocHGlobal(nBytes);
                CM_Get_Device_ID(ptrPrevious, ptrInstanceBuf, nBytes, 0);
                string InstanceID = Marshal.PtrToStringAuto(ptrInstanceBuf);

                Marshal.FreeHGlobal(ptrInstanceBuf);
            }
        }
        i++;
    }
}
SetupDiDestroyDeviceInfoList(handle);
if (string.IsNullOrEmpty(deviceInterfaceDetailData.DevicePath))
{
    return false;
}

此后的代码工作正常。问题出在这里,这适用于32位机器,但不能在64位机器上运行。在64位机器上,deviceInterfaceDetailData.DevicePath是空的,在32位机器中,我得到一个有效的设备路径。在构建过程中有什么问题?

1 个答案:

答案 0 :(得分:0)

我得到了这个问题的解决方案。问题是deviceInterfaceDetailData.cbSize值设置。根据{{​​3}},我将64位机器的cbSize值更改为8,然后就可以了!