public extern static bool SetupDiGetDeviceRegistryProperty( [In] IntPtr DeviceInfo, [In] SP_DEVINFO_DATA DeviceInfoData, [In] DeviceRegistryProperty Property, [In, Out] ref RegistryType PropertyRegDataType, [In, Out] StringBuilder PropertyBuffer, [In] UInt32 PropertyBufferSize, [In, Out] ref UInt32 RequiredSize);
private static string TryGetDeviceRegistryProperty(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfo, DeviceRegistryProperty property) { try { return(GetDeviceRegistryProperty(deviceInfoSet, deviceInfo, property)); } catch (Win32Exception) { } return(null); }
private static string GetDeviceRegistryProperty(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfo, DeviceRegistryProperty property) { const int BufferSize = 2048; IntPtr propertyBufferPtr = Marshal.AllocHGlobal(BufferSize); try { if (NativeMethods.SetupDiGetDeviceRegistryProperty( deviceInfoSet, ref deviceInfo, property, out RegistryValueKind valueKind, propertyBufferPtr, BufferSize, out uint propertySize)) { if (propertySize > 0 && valueKind == RegistryValueKind.String) { return(Marshal.PtrToStringUni(propertyBufferPtr)); } } else { throw new Win32Exception(); } }
private static string GetDeviceRegistryProperty(IntPtr hDeviceInfo, SP_DEVINFO_DATA deviceInfoData, DeviceRegistryProperty propertyType) { RegistryType propertyRegistryType = RegistryType.REG_NONE; StringBuilder propertyBuffer = new StringBuilder(512); UInt32 sizeRequired = 0; bool result = SetupDi.SetupDiGetDeviceRegistryProperty( hDeviceInfo, deviceInfoData, propertyType, ref propertyRegistryType, propertyBuffer, (UInt32)propertyBuffer.Capacity, ref sizeRequired); if ((result == true) & (propertyRegistryType == RegistryType.REG_SZ)) { return(propertyBuffer.ToString()); } else { return(null); } }