My SetupDiEnumDeviceInfo returns ERROR_NO_MORE_ITEMS for certain devices (C#)

十年热恋 提交于 2019-12-13 03:45:55

问题


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());

        }

回答1:


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.

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.




回答2:


In my case I attempted to enable/disable netowrk interface, and used Setup API to get the index of the device that must be passed to SetupDiSetClassInstallParams(). I didn't cache the index, but got ERROR_NO_MORE_ITEMS arised randomly when dealing with the Setup API funcions.

The problem in behind was that the "list of network adapters" changed in between, f.ex., the "Microsoft ISATAP adapter" disappeared from the list (I also could see that it is hidden in device manager).

In my case using DIGCF_PROFILE instead of DIGCF_PRESENT worked for me.



来源:https://stackoverflow.com/questions/44615491/my-setupdienumdeviceinfo-returns-error-no-more-items-for-certain-devices-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!