Sending IOCTL to 64bit machine in C#

730 views Asked by At

I'm sending IOCTL's to my keyboard filter driver and the code is as follows:

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;
}

The code after this works fine. The problem lies here and this works on a 32bit machine but does not work on 64 bit machines.In 64bit machines, deviceInterfaceDetailData.DevicePath is empty where as in 32 bit machines I get a valid device path.Is there something wrong in the build process?

1

There are 1 answers

0
Aster Veigas On

I got the solution to this issue. The problem was the deviceInterfaceDetailData.cbSize value set. According to http://www.pinvoke.net/default.aspx/setupapi.setupdigetdeviceinterfacedetail I changed the cbSize value to 8 for 64 bit machines and then it works!