Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SetupDiClassGuidsFromName (setupapi)
 
.
Summary
Retrieves the Class GUID for a device based on the name

C# Signature:

[DllImport("setupapi.dll", SetLastError=true)]
static extern bool SetupDiClassGuidsFromName(string ClassName, ref Guid ClassGuidArray1stItem, UInt32 ClassGuidArraySize, out UInt32 RequiredSize);
static extern bool SetupDiClassGuidsFromName(string ClassName, ref Guid ClassGuidList, UInt32 ClassGuidListSize, out UInt32 RequiredSize);

VB Signature:

<DllImport("setupapi.dll")> _
    Private Shared Function SetupDiClassGuidsFromName( _
    ByVal ClassName As StringBuilder, _
    ByRef ClassGuids As Guid, _
    ByVal ClassGuidSize As Integer, _
    ByRef ClassGuidRequiredSize As Integer) As Boolean
    End Function

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

12/01/2009

    C# signature works.
    I am unsure if this declaration is correct.  The one problem that I found is that the function asks for an Array of GUIDs.  I couldn't get that to work, but just using one did work.  I found that none of the device classes had more than one GUID so I figured this to be okay.  Please correct if you know better.

Tips & Tricks:

Please add some!

C# Sample Code:

    UInt32 RequiredSize = 0;
    Guid[] GuidArray = new Guid[1];
    // read Guids
    bool Status = SetupDiClassGuidsFromName("class name here", ref GuidArray[0], 1, out RequiredSize);
    if (true == Status)
    {
            if (1 < RequiredSize)
        {
        GuidArray = new Guid[RequiredSize];
        SetupDiClassGuidsFromName("class name here", ref GuidArray[0], RequiredSize, out RequiredSize);
        }
10/02/2009     C# signature corrected. I didn't manage to use a real array of Guids (with one Guid works fine as reported above). So a little bit of lower-level operations as follows:
    // first check the required number of classes
    UInt32 size = 0;
    SetupDiClassGuidsFromNameEx("class name here", IntPtr.Zero, 0, out size, null, IntPtr.Zero);
    // allocate unmanaged memory - one GUID needs 16 bytes
    IntPtr ptr = Marshal.AllocHGlobal(GUID_SIZE * (int)size);
    // read guids
    SetupDiClassGuidsFromNameEx("class name here", ptr, size, out size, null, IntPtr.Zero);
    // if needed - convert to real Guids
    Guid[] guids = new Guid[(int)size];
    byte[] buffer = new byte[GUID_SIZE];
    for (int n = 0; n < size; n++)
        {
            for (int pos = 0; pos < GUID_SIZE; pos++)
            {
                buffer[pos] = Marshal.ReadByte(ptr, (n * GUID_SIZE) + pos);
                bGuids[n][pos] = buffer[pos];
            }
            guids[n] = new Guid(buffer);
        }
    }
    else
    {
        UInt32 ErrorCode;
        ErrorCode = GetLastError();
    }

VB.Net Sample Code:

    Dim ClassName As New StringBuilder("net")
    Dim ClassGuid As Guid
    Dim GuidSize As Integer = 0
    Dim GuidReqtSize As Integer
    Dim intRtrn As Integer
    intRtrn = SetupDiClassGuidsFromName(ClassName, ClassGuid, GuidSize, GuidReqtSize)
    GuidSize = GuidReqtSize
    intRtrn = SetupDiClassGuidsFromName(ClassName, ClassGuid, GuidSize, GuidReqtSize)
    MsgBox(ClassGuid.ToString)

Tips & Tricks:

Please add some!

Sample Code:

    Dim ClassName As New StringBuilder("net")
    Dim ClassGuid As Guid
    Dim GuidSize As Integer = 0
    Dim GuidReqtSize As Integer
    Dim intRtrn As Integer
    intRtrn = SetupDiClassGuidsFromName(ClassName, ClassGuid, GuidSize, GuidReqtSize)
    GuidSize = GuidReqtSize
    intRtrn = SetupDiClassGuidsFromName(ClassName, ClassGuid, GuidSize, GuidReqtSize)
    MsgBox(ClassGuid.ToString)

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions