[DllImport("setupapi.dll", SetLastError =true)]publicstaticexternboolSetupDiGetDeviceRegistryProperty(IntPtr deviceInfoSet,refSP_DEVINFO_DATA deviceInfoData,uint property,outUInt32 propertyRegDataType,IntPtr propertyBuffer,// the difference between this signature and the one above.uint propertyBufferSize,outUInt32 requiredSize );
<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
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[] buffer = new byte[BUFFER_SIZE];
if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, out RegType, buffer, BUFFER_SIZE, out RequiredSize))
{
string ControllerDeviceDesc = Encoding.Unicode.GetString(buffer).TrimEnd('\0');
}
if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, out RegType, buffer, BUFFER_SIZE, out RequiredSize))
{
string ControllerDriverKeyName = Encoding.Unicode.GetString(buffer).TrimEnd('\0');
}
}
}
}
i++;
}
SetupDiDestroyDeviceInfoList(h);
IntPtr ptrBuffer = new IntPtr();
ptrBuffer = Marshal.AllocHGlobal(BUFFER_SIZE);
UInt32 RequiredSize = 0;
UInt32 regType = REG_SZ;
if ( SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, out regType, ptrBuffer, BUFFER_SIZE, out RequiredSize) )
{
string ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuffer);
}
if ( SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, out regType, ptrBuffer, BUFFER_SIZE, out RequiredSize) )
{
string ControllerDriverKeyName = Marshal.PtrToStringAuto(ptrBuffer);
}
Marshal.FreeHGlobal(ptrBuffer);