SetupDiGetDeviceRegistryProperty (setupapi)
Last changed: -14.9.137.224

.

Summary:

The SetupDiGetDeviceRegistryProperty function retrieves the specified device property.

This handle is typically returned by the SetupDiGetClassDevs or SetupDiGetClassDevsEx function.

C# Signature:

/// <summary>
/// The SetupDiGetDeviceRegistryProperty function retrieves the specified device property.
/// This handle is typically returned by the SetupDiGetClassDevs or SetupDiGetClassDevsEx function.
/// </summary>
/// <param Name="DeviceInfoSet">Handle to the device information set that contains the interface and its underlying device.</param>
/// <param Name="DeviceInfoData">Pointer to an SP_DEVINFO_DATA structure that defines the device instance.</param>
/// <param Name="Property">Device property to be retrieved. SEE MSDN</param>
/// <param Name="PropertyRegDataType">Pointer to a variable that receives the registry data Type. This parameter can be NULL.</param>
/// <param Name="PropertyBuffer">Pointer to a buffer that receives the requested device property.</param>
/// <param Name="PropertyBufferSize">Size of the buffer, in bytes.</param>
/// <param Name="RequiredSize">Pointer to a variable that receives the required buffer size, in bytes. This parameter can be NULL.</param>
/// <returns>If the function succeeds, the return value is nonzero.</returns>
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetupDiGetDeviceRegistryProperty(
    IntPtr DeviceInfoSet,
    ref SP_DEVINFO_DATA DeviceInfoData,
    uint Property,
    out UInt32 PropertyRegDataType,
    byte[] PropertyBuffer,
    uint PropertyBufferSize,
    out UInt32 RequiredSize
    );

VB Signature:

<DllImport("setupapi.dll", SetLastError:=True)> _
Public Shared Function SetupDiGetDeviceRegistryProperty( _
    ByVal DeviceInfoSet As Integer, _
    ByRef DeviceInfoData As SP_DEVINFO_DATA, _
    ByVal [Property] As Integer, _
    ByRef PropertyRegDataType As Integer, _
    ByVal PropertyBuffer As Byte(), _
    ByVal PropertyBufferSize As Integer, _
    ByRef RequiredSize As Integer) As Boolean
End Function

User-Defined Types:

/// <summary>
/// The SP_DEVINFO_DATA structure defines a device instance that is a member of a device information set.
/// </summary>
public struct SP_DEVINFO_DATA
{
    /// <summary>Size of the structure, in bytes.</summary>
    public uint cbSize;
    /// <summary>GUID of the device interface class.</summary>
    public Guid ClassGuid;
    /// <summary>Handle to this device instance.</summary>
    public uint DevInst;
    /// <summary>Reserved; do not use.</summary>
    public uint Reserved;
}

Possible values for Property.

private enum SPDRP {
   SPDRP_DEVICEDESC = 0x00000000,
   SPDRP_HARDWAREID = 0x00000001,
   SPDRP_COMPATIBLEIDS = 0x00000002,
   SPDRP_NTDEVICEPATHS = 0x00000003,
   SPDRP_SERVICE = 0x00000004,
   SPDRP_CONFIGURATION = 0x00000005,
   SPDRP_CONFIGURATIONVECTOR = 0x00000006,
   SPDRP_CLASS = 0x00000007,
   SPDRP_CLASSGUID = 0x00000008,
   SPDRP_DRIVER = 0x00000009,
   SPDRP_CONFIGFLAGS = 0x0000000A,
   SPDRP_MFG = 0x0000000B,
   SPDRP_FRIENDLYNAME = 0x0000000C,
   SPDRP_LOCATION_INFORMATION = 0x0000000D,
   SPDRP_PHYSICAL_DEVICE_OBJECT_NAME = 0x0000000E,
   SPDRP_CAPABILITIES = 0x0000000F,
   SPDRP_UI_NUMBER = 0x00000010,
   SPDRP_UPPERFILTERS = 0x00000011,
   SPDRP_LOWERFILTERS = 0x00000012,
   SPDRP_MAXIMUM_PROPERTY = 0x00000013,
}

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

Guid DiskGUID = new Guid(GUID_DEVINTERFACE_DISK);

// We start at the "root" of the device tree and look for all
// devices that match the interface GUID of a disk
IntPtr h = SetupDiGetClassDevs(ref DiskGUID, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (h.ToInt32() != INVALID_HANDLE_VALUE)
{
   bool Success = true;
   int i = 0;
   while (Success)
   {
     // create a Device Interface Data structure
     SP_DEVICE_INTERFACE_DATA dia = new SP_DEVICE_INTERFACE_DATA();
     dia.cbSize = Marshal.SizeOf(dia);

     // start the enumeration
     Success = SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref DiskGUID, i, ref dia);
     if (Success)
     {
       // build a DevInfo Data structure
       SP_DEVINFO_DATA da = new SP_DEVINFO_DATA();
       da.cbSize = Marshal.SizeOf(da);

       // build a Device Interface Detail Data structure
       SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA();
       didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // trust me :)

       // now we can get some more detailed information
       int nRequiredSize = 0;
       int nBytes = BUFFER_SIZE;
       if (SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, nBytes, ref nRequiredSize, ref da))
       {    
          // get the Device Description and DriverKeyName
          Uint32 RequiredSize;
          Uint32 RegType;
          byte[] ptrBuf = new byte[BUFFER_SIZE];

          if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, out RegType, ptrBuf, BUFFER_SIZE, out RequiredSize))
          {
            string ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuf);
          }
          if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, out RegType, ptrBuf, BUFFER_SIZE, out RequiredSize))
          {
            string ControllerDriverKeyName = Marshal.PtrToStringAuto(ptrBuf);
          }
       }
     }
   }
   i++;
}
SetupDiDestroyDeviceInfoList(h);

Documentation