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
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
setupdigetdeviceinstanceid (setupapi)
 
.
Summary
The SetupDiGetDeviceInstanceId function retrieves the device instance ID that is associated with a device information element.

C# Signature:

[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool SetupDiGetDeviceInstanceId(
   IntPtr DeviceInfoSet,
   ref SP_DEVINFO_DATA DeviceInfoData,
   StringBuilder DeviceInstanceId,
   int DeviceInstanceIdSize,
   out int RequiredSize
);

VB Signature:

Declare Function SetupDiGetDeviceInstanceId Lib "setupapi.dll" (TODO) As TODO

User-Defined Types:

SP_DEVINFO_DATA

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

WINSETUPAPI BOOL WINAPI  SetupDiGetDeviceInstanceId(
   IN HDEVINFO  DeviceInfoSet,
   IN PSP_DEVINFO_DATA  DeviceInfoData,
   OUT PTSTR  DeviceInstanceId,
   IN DWORD  DeviceInstanceIdSize,
   OUT PDWORD  RequiredSize  OPTIONAL
);

Tips & Tricks:

Please add some!

Sample Code:

    static string GetInstanceIDByKeyName(string DriverKeyName)
    {
        string ans = "";
        string DevEnum = REGSTR_KEY_USB;

        // Use the "enumerator form" of the SetupDiGetClassDevs API
        // to generate a list of all USB devices
        IntPtr h = SetupDiGetClassDevs(0, DevEnum, IntPtr.Zero, DIGCF_PRESENT | DIGCF_ALLCLASSES);
        if (h.ToInt32() != INVALID_HANDLE_VALUE)
        {
        IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
        string KeyName;

        bool Success = true;
        int i = 0;
        while (Success)
        {
            // create a Device Interface Data structure
            SP_DEVINFO_DATA da = new SP_DEVINFO_DATA();
            da.cbSize = Marshal.SizeOf(da);

            // start the enumeration
            Success = SetupDiEnumDeviceInfo(h, i, ref da);
            if (Success)
            {
            int RequiredSize = 0;
            int RegType = REG_SZ;

            KeyName = "";
            if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref RegType, ptrBuf, BUFFER_SIZE, ref RequiredSize))
            {
                KeyName = Marshal.PtrToStringAuto(ptrBuf);
            }

            // is it a match?
            if (KeyName == DriverKeyName)
            {
                int nBytes = BUFFER_SIZE;
                StringBuilder sb = new StringBuilder(nBytes);
                SetupDiGetDeviceInstanceId(h, ref da, sb, nBytes, out RequiredSize);
                ans = sb.ToString();
                break;
            }
            }
            i++;
        }
        Marshal.FreeHGlobal(ptrBuf);
        SetupDiDestroyDeviceInfoList(h);
        }
        return ans;
    }

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