tags:

views:

796

answers:

3

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.

A: 

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?

JaredPar
According to the msdn spec, SetupDiGetClassDevs (They really need to find a way to shorten these function names) will return a list of devices or an error. At this point I'm not trying to access a device just yet. I'm actually trying to access the list. returned so that I may extract the path for the device needed.
Dark Star1
+1  A: 

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

hythlodayr
I'm using the Guid returned by the **HidD_GetHidGuid(ref tGuid);** function. SetDiGetClassDevs is returning a deviceinfoset accroding to what is specfied in the parameters. Unfortunately the only way to know whether what is returned is valid is to call the SetupDiEnum* functions.
Dark Star1
Thanks for the tip @hythodayr. I am running C# so I could not quite make out the difference required in the C# code given the C(++) example in the post, but need to play with the code when I get more time.
Thies
Oohh thanks.. That seems to have sifted out something from the list. I'll respond later when I've finished chasing this down.
Dark Star1
If that worked, please don't forget to bump up the answer :)
hythlodayr
I had forgotten to add the DIGCF_DEVICEITERFACE flag.. A look through former code showed I had this flag set.
Dark Star1
A: 

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?

Thies
My working environment is still 32bit (Win7 RC1). I can say however that using a setupDiGetClassDev + SetupDiEnumDeviceInfo will return a valid deviceinfolist which you can actually query using SetupDiGetDeviceRegistryProperty for a number of things. However using or attempting to use SetupDiGetDeviceInterfaceDetail is what derailed me and made me start over again.
Dark Star1