3

我试图判断是否禁用了硬件设备(特别是多点触控数字化仪) 。

我可以使用此处的DisableHardware类成功启用/禁用设备。

我找到了具有属性HardwareDisabled的DEVICE_CAPABILITIES结构。但是,如果我调用SetupDiGetDeviceRegistryPropertyW来返回 SPDRP_CAPABILITIES,我无法将返回值 (DWORD) 转换为有用的 uint。

我相信我从 byte[] => uint 的转换是错误的,我总是得到 128。

private static bool IsDeviceHardwareDisabeld(IntPtr info, SP_DEVINFO_DATA devdata)
{
    Trace.WriteLine("IsDeviceHardwareDisabeld");
    uint SPDRP_CAPABILITIES = 0x0000000F;
    uint CM_DEVCAP_HARDWAREDISABLED = 16384u;

    uint propId = SPDRP_CAPABILITIES;

    uint proptype, outsize;
    IntPtr buffer = IntPtr.Zero;
    try
    {
        uint buflen = 512;
        buffer = Marshal.AllocHGlobal((int)buflen);
        outsize = 0;
        SetupDiGetDeviceRegistryPropertyW(
            info,
            ref devdata,
            propId,
            out proptype,
            buffer,
            buflen,
            ref outsize);
        Trace.WriteLine("OUTSIZE: " + outsize.ToString()); // 4
        byte[] lbuffer = new byte[outsize];
        Marshal.Copy(buffer, lbuffer, 0, (int)outsize);
        int errcode = Marshal.GetLastWin32Error();
        if (errcode == ERROR_INVALID_DATA) throw new Exception("ERROR_INVALID_DATA");
        CheckError("SetupDiGetDeviceRegistryPropertyW", errcode);

        uint capabilities = BitConverter.ToUInt32(lbuffer, 0); // always 128
        uint bitwise = capabilities & CM_DEVCAP_HARDWAREDISABLED; // always 0
        bool isHardwareDisabled = bitwise > 0;
        Trace.WriteLine("HARDWAREDISABLED: " + isHardwareDisabled.ToString());
        return isHardwareDisabled;
    }
    finally
    {
        if (buffer != IntPtr.Zero)
            Marshal.FreeHGlobal(buffer);
    }
}

对于 DEVICE_CAPABILITIES 的值,我的转换总是返回128

4

0 回答 0