How can I find out what is wrong with a SetupDiGetClassDev interface in C#?
Question
How do I find out what's wrong with the device info set returned? I'm re-writing my code again and I'm still hitting the same stumbling block.
deviceInfoSet = SetupDiGetClassDevs(ref tGuid, 0, IntPtr.Zero, (uint)SetupDiFlags.DIGCF_PRESENT );
if (deviceInfoSet.ToInt32() == INVALID_DEVICE_HANDLE)
{
int errCode = Marshal.GetLastWin32Error();
errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
statusLabel.Text += "Invalid deviceinfoset returned: " + errCode + " => " + errorMessage + ".";
}
The above code doesn't cause any errors but when I use the code below:
result = true;
while (result)
{
result = SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref tGuid, Index, ref anInterface);
if (!result)
{
int errCode = Marshal.GetLastWin32Error();
errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
statusLabel.Text += "\nSetDiEnumDeviceInterface Error: " + errCode + " => " + errorMessage + ".";
break;
}
Index++;
}
to try and access the device info set list, error code 259 (No more data is available) is returned. I am at a loss as to what I'm doing wrong.
Solution
Are you sure you're using the right GUID?
Check out http://blogs.msdn.com/doronh/archive/2006/02/15/532679.aspx
Edit: Everything else looks by-the-book and correct.
Edit2: Trying including DIGCF_DEVICEINTERFACE in call to SetupDiGetClassDevs, and see if that works for you. That is, both DIGCF_PRESENT and DIGCF_DEVICEINTERFACE.
Edit3: For the 64bit issue (@Thies), check out http://social.msdn.microsoft.com/Forums/en-US/windowssdk/thread/ea816aea-f718-4a0e-b013-0aa273de037f
OTHER TIPS
I'm not familiar with this particular interface but it doesn't appear you are doing anything wrong. It simply appears that you are trying to read data from a device which currently has no data to offer.
Is that the case? Do you know for sure that the device is currently trying to return data to you?
I had simular problems which were due to running on 64-bit Windows. I never quite figured it out, and ended up hardcoding the device path in my code - which is not recommended.
I think it has to do with the API structures not setup correct for 64-bit.
Hope this may help lead someone else to an answer (and maybe help with my problem as well)
Please state if you are running 64-bit - and have you tried the same code on a 32-bit OS?
The problem starts with how SetupDiGetClassDevs is called.
If you are looking to get a device path, use SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE ,,,)
SetupDiEnumDeviceInterfaces fails with error 259 if SetupDiGetClassDevs is given the wrong GUID in ClassGuid which MS Help says is A pointer to the GUID for a device setup class or a device interface class.
Include file devguid.h contains a set of GUID_DEVCLASS values. These are NOT the same as GUID_DEVINTERFACE_* values which are the one you need.
Use #include which includes ksuuids.h where you'll find GUID_DEVINTERFACE_* values.
There's a more detailed explanation on my website, with some source code that should help in correctly enumerating USB devices.