internal Guid GetProperty(IntPtr hDevInfoSet, NativeMethods.SP_DEVINFO_DATA devData, NativeMethods.SetupDiRegistryProperty property, Guid defaultValue) { if (devData == null) { throw new ArgumentNullException("devData"); } int propertyRegDataType = 0; int requiredSize; // ( 32(hex-digits) + 4(dashes) + 2(braces) + 1(null) ) * 2-bytes/char = 78 bytes // ex. "{745A17A0-74D3-11D0-B6FE-00A0C90F57DA}" int propertyBufferSize = 78; IntPtr propertyBuffer = Marshal.AllocHGlobal(propertyBufferSize); if (!NativeMethods.SetupDiGetDeviceRegistryProperty(hDevInfoSet, devData, property, out propertyRegDataType, propertyBuffer, propertyBufferSize, out requiredSize)) { int error = Marshal.GetLastWin32Error(); Marshal.FreeHGlobal(propertyBuffer); if (error != NativeMethods.ERROR_INVALID_DATA) { Trace.WriteLine(String.Format("Warning: {0}.GetProperty() - SetupDiGetDeviceRegistryProperty({1}) failed. {2}({3})", this.GetType(), property, new Win32Exception(error).Message, error)); } // throw new Win32Exception(error); return(defaultValue); } String guidStr = Marshal.PtrToStringAuto(propertyBuffer); Marshal.FreeHGlobal(propertyBuffer); return(new Guid(guidStr)); }
internal int GetProperty(IntPtr hDevInfoSet, NativeMethods.SP_DEVINFO_DATA devData, NativeMethods.SetupDiRegistryProperty property, int defaultValue) { if (devData == null) { throw new ArgumentNullException("devData"); } int propertyRegDataType = 0; int requiredSize; int propertyBufferSize = Marshal.SizeOf(typeof(int)); IntPtr propertyBuffer = Marshal.AllocHGlobal(propertyBufferSize); if (!NativeMethods.SetupDiGetDeviceRegistryProperty(hDevInfoSet, devData, property, out propertyRegDataType, propertyBuffer, propertyBufferSize, out requiredSize)) { int error = Marshal.GetLastWin32Error(); Marshal.FreeHGlobal(propertyBuffer); if (error != NativeMethods.ERROR_INVALID_DATA) { Trace.WriteLine(String.Format("Warning: {0}.GetProperty() - SetupDiGetDeviceRegistryProperty({1}) failed. {2}({3})", this.GetType(), property, new Win32Exception(error).Message, error)); } // throw new Win32Exception(error); return(defaultValue); } int value = (int)Marshal.PtrToStructure(propertyBuffer, typeof(int)); Marshal.FreeHGlobal(propertyBuffer); return(value); }