我的SetupDiEnumDeviceInfo对于某些设备(C#)返回ERROR_NO_MORE_ITEMS [英] My SetupDiEnumDeviceInfo returns ERROR_NO_MORE_ITEMS for certain devices (C#)

查看:439
本文介绍了我的SetupDiEnumDeviceInfo对于某些设备(C#)返回ERROR_NO_MORE_ITEMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取设备的电源数据(cm_power_data_s)。
我正在使用 SetupDiGetDeviceRegistryProperty API进行操作。
虽然此功能在某些设备上可以正常使用,但在其他设备上则无法使用。
数据 SetupDiEnumDeviceInfo 返回的是 null ,没有这个,我就不能使用 SetupDiGetDeviceRegistryProperty
我尝试通过执行 wmi 查询来手动填充 sp_devinfo_data 以获取类 GUID ,但 SetupDiGetDeviceRegistryProperty 不会返回缓冲区大小(与传递空数据相同)。
有人可以帮忙吗?
这是我的代码:

I am trying to fetch power data for my devices(cm_power_data_s).
I am using the SetupDiGetDeviceRegistryProperty API to do so.
While this works fine for some devices, it doesn't work for others.
The data returned by SetupDiEnumDeviceInfo is null, and without this, I can't use SetupDiGetDeviceRegistryProperty.
I tried manually filling sp_devinfo_data by making a wmi query to get the class GUID, but SetupDiGetDeviceRegistryProperty doesn't return the buffer size with it (same as passing null data).
Can someone please help?
Here's my code:

uint SPDRP_DEVICE_POWER_DATA = 0x0000001E;
        int proptype;
        int size;
        //int D3, D2, D1, D3wake, cap = 0;
        DEVPROPKEY key = DEVPROPKEY.DEVPKEY_Device_PowerData;

        IntPtr hDevInfo = SetupDiGetClassDevs(IntPtr.Zero, DevID, IntPtr.Zero, DIGCF.DIGCF_ALLCLASSES | DIGCF.DIGCF_PRESENT | DIGCF.DIGCF_DEVICEINTERFACE);
        if (hDevInfo == (IntPtr)INVALID_HANDLE_VALUE)
        {
            throw new ArgumentNullException("invalidhandle");
        }
        SP_DEVINFO_DATA data = new SP_DEVINFO_DATA();
        data.cbSize = Marshal.SizeOf(data);

        SetupDiEnumDeviceInfo(hDevInfo, 0, ref data); // This data is empty for some devices
       // SetupDiGetSelectedDevice(hDevInfo, ref data);
        int aa = Marshal.GetLastWin32Error();

        bool b = SetupDiGetDeviceRegistryProperty(hDevInfo, ref data, SPDRP_DEVICE_POWER_DATA, out proptype, IntPtr.Zero, 0, out size);
        int a = Marshal.GetLastWin32Error();
        if (size == 0)
        {
                          return "";
         //   throw new ArgumentNullException("Power Data cannot be fetched");
        }

        IntPtr buffer1 = Marshal.AllocHGlobal(size);

        if (!SetupDiGetDeviceRegistryProperty(hDevInfo, ref data, SPDRP_DEVICE_POWER_DATA, out proptype, buffer1, size, out size))
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());

        }


推荐答案

I'自从这个问题是18个月前以来,您就可以肯定您已经走了很长时间,但是我只是遇到了同样的问题。我的谷歌搜索使我进入了您的话题。然后我想通了。因此,以防万一其他人遇到此问题,问题在于您提供给SetupDiGetClassDevs的标志。

I'm sure you've long since moved on since this question is from 18 months ago, but I just hit exactly this same problem. My googling brought me to your thread. Then I figured it out. So in case anyone else ever hits this, the problem is the flags that you supplied to SetupDiGetClassDevs.

您应该刚给它DIGCF_PRESENT标志。使用您提供的所有标记,您可以获得甚至不存在的设备的信息,从而导致您和我都遇到的后续错误。

You should have just given it the DIGCF_PRESENT flag. With all the flags you supplied you get back info for devices that aren't even there, leading to exactly the subsequent error you and I both got.

这篇关于我的SetupDiEnumDeviceInfo对于某些设备(C#)返回ERROR_NO_MORE_ITEMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆