Working with Devices: Retrieving Class Guids From Class Name

Its been sometime since my last post. I've been attending to my new job and new daughter.

So, the previous post demonstrated how to get the Class Name from the Guid. This time I want to show how to get the Class Guid from the Class Name. You'll be using the SetupDiClassGuidsFromName function. Here is how I declared it.

Private Declare Auto Function SetupDiClassGuidsFromName Lib "Setupapi.dll" (ByVal ClassName As String, ByVal ClassGuidList() As Guid, ByVal ClassGuidListSize As Integer, ByRef RequiredSize As Integer) As Boolean


There are four parameters to pass into this function. The first is the name. Simply enough that is just a string with the non-friendly name of the class. So for example 'Net' is the name for the Network Adapter class. The second parameter is to retrieve the Guids. The third and fourth parameter are used to size our array. The way it works is that we pass 0 to the third parameter meaning our Guid Array is empty and we have an array setup to receive the fourth parameter output. What will happen is the fourth parameter (RequiredSize) will tell us how big our array should be. We then resize our Guid Array and set the third parameter (ClassGuidListSize) to what the required size should be.

Here is the code

'Returns the Class Guid
Function GetClassGuidsFromName(ByVal ClassName As String) As Guid()
Dim intSize, intReqSize As Integer
Dim GuidList() As Guid
If (Not SetupDiClassGuidsFromName(ClassName, GuidList, intSize, intReqSize)) Then
intSize = intReqSize
ReDim GuidList(intSize - 1)
If (SetupDiClassGuidsFromName(ClassName, GuidList, intSize, intReqSize)) Then
Return GuidList
End If
End If
End Function

Calling GetClassGuidsFromName("USB") will return a Guid array with , in my case two items.

Next time we'll get the Class Description. Otherwise known as the friendly name for the Class.


*** UPDATE ***
I previously had posted a different set of code for this post, because of my inability to code to receive multiple Guids. That seems to be resolved with this above code and thus I've updated this post accordingly.

4 comments:

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
tinman said...

Hi,

I'm interested in what other methods you tried to get the class GUIDs using?

I've only tried with a dummy DLL of my own code (I don't seem to have any real devices who's class has multiple GUIDs), but it looks as simple as creating an array of Guid's and passing the reference to the first one.

In C# I've got it defined like this:

[DllImport("setupapi.dll")]
static extern Boolean SetupDiClassGuidsFromName(string ClassName, ref Guid ClassGuidList, UInt32 ClassGuidListSize, out UInt32 RequiredSize);

string class_name = "USB";
UInt32 number_guids = 2;
Boolean success;

Guid[] guids = new Guid[number_guids];
success = SetupDiClassGuidsFromName(class_name, ref guids[0], number_guids, out number_guids);

If that's something you've already tried and failed with, could you provide a list of things you have tried?

Cheers.

Rob Haupt said...

tinman,

I've taken your code provided and tweaked a little in order to get what I was looking for. Below is working code in VS 2003.

[DllImport("setupapi.dll")]
static extern Boolean SetupDiClassGuidsFromName(string ClassName, [Out] Guid[] ClassGuidList, int ClassGuidListSize, out int RequiredSize);


string name = "USB";
Guid[] GuidList = null;
int intSize,intReqSize;
intSize = 0;
intReqSize = 0;
if(!SetupDiClassGuidsFromName(name,GuidList,intSize,out intReqSize))
{
GuidList = new Guid[intReqSize];
intSize = intReqSize;
if(SetupDiClassGuidsFromName(name,GuidList,intSize,out intReqSize))
{
foreach(Guid ClassGuid in GuidList)
{
MessageBox.Show(this,ClassGuid.ToString());
}
}
}

My C# skills are a bit rusty, but this code works, which surprisingly more than I can say for the VB.Net Code. This gives me some promise that I can correct the code in my post with something that works as it should.