|
| 1 | +using System; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace WindowsFormsTest |
| 6 | +{ |
| 7 | + class SetupAPI |
| 8 | + { |
| 9 | + public enum SID_NAME_USE |
| 10 | + { |
| 11 | + SidTypeUser = 1, |
| 12 | + SidTypeGroup, |
| 13 | + SidTypeDomain, |
| 14 | + SidTypeAlias, |
| 15 | + SidTypeWellKnownGroup, |
| 16 | + SidTypeDeletedAccount, |
| 17 | + SidTypeInvalid, |
| 18 | + SidTypeUnknown, |
| 19 | + SidTypeComputer |
| 20 | + } |
| 21 | + |
| 22 | + // Use this to get user name |
| 23 | + [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] |
| 24 | + public extern static bool LookupAccountSid( |
| 25 | + string lpSystemName, |
| 26 | + [MarshalAs(UnmanagedType.LPArray)] byte[] Sid, |
| 27 | + StringBuilder lpName, |
| 28 | + ref uint cchName, |
| 29 | + StringBuilder ReferencedDomainName, |
| 30 | + ref uint cchReferencedDomainName, |
| 31 | + out SID_NAME_USE peUse); |
| 32 | + |
| 33 | + public enum RegPropertyType |
| 34 | + { |
| 35 | + SPDRP_DEVICEDESC = 0x00000000, // DeviceDesc (R/W) |
| 36 | + SPDRP_HARDWAREID = 0x00000001, // HardwareID (R/W) |
| 37 | + SPDRP_COMPATIBLEIDS = 0x00000002, // CompatibleIDs (R/W) |
| 38 | + SPDRP_UNUSED0 = 0x00000003, // unused |
| 39 | + SPDRP_SERVICE = 0x00000004, // Service (R/W) |
| 40 | + SPDRP_UNUSED1 = 0x00000005, // unused |
| 41 | + SPDRP_UNUSED2 = 0x00000006, // unused |
| 42 | + SPDRP_CLASS = 0x00000007, // Class (R--tied to ClassGUID) |
| 43 | + SPDRP_CLASSGUID = 0x00000008, // ClassGUID (R/W) |
| 44 | + SPDRP_DRIVER = 0x00000009, // Driver (R/W) |
| 45 | + SPDRP_CONFIGFLAGS = 0x0000000A, // ConfigFlags (R/W) |
| 46 | + SPDRP_MFG = 0x0000000B, // Mfg (R/W) |
| 47 | + SPDRP_FRIENDLYNAME = 0x0000000C, // FriendlyName (R/W) |
| 48 | + SPDRP_LOCATION_INFORMATION = 0x0000000D,// LocationInformation (R/W) |
| 49 | + SPDRP_PHYSICAL_DEVICE_OBJECT_NAME = 0x0000000E, // PhysicalDeviceObjectName (R) |
| 50 | + SPDRP_CAPABILITIES = 0x0000000F, // Capabilities (R) |
| 51 | + SPDRP_UI_NUMBER = 0x00000010, // UiNumber (R) |
| 52 | + SPDRP_UPPERFILTERS = 0x00000011, // UpperFilters (R/W) |
| 53 | + SPDRP_LOWERFILTERS = 0x00000012, // LowerFilters (R/W) |
| 54 | + SPDRP_BUSTYPEGUID = 0x00000013, // BusTypeGUID (R) |
| 55 | + SPDRP_LEGACYBUSTYPE = 0x00000014, // LegacyBusType (R) |
| 56 | + SPDRP_BUSNUMBER = 0x00000015, // BusNumber (R) |
| 57 | + SPDRP_ENUMERATOR_NAME = 0x00000016, // Enumerator Name (R) |
| 58 | + SPDRP_SECURITY = 0x00000017, // Security (R/W, binary form) |
| 59 | + SPDRP_SECURITY_SDS = 0x00000018, // Security (W, SDS form) |
| 60 | + SPDRP_DEVTYPE = 0x00000019, // Device Type (R/W) |
| 61 | + SPDRP_EXCLUSIVE = 0x0000001A, // Device is exclusive-access (R/W) |
| 62 | + SPDRP_CHARACTERISTICS = 0x0000001B, // Device Characteristics (R/W) |
| 63 | + SPDRP_ADDRESS = 0x0000001C, // Device Address (R) |
| 64 | + SPDRP_UI_NUMBER_DESC_FORMAT = 0x0000001E, // UiNumberDescFormat (R/W) |
| 65 | + SPDRP_MAXIMUM_PROPERTY = 0x0000001F // Upper bound on ordinals |
| 66 | + } |
| 67 | + |
| 68 | + public enum DiGetClassFlags : uint |
| 69 | + { |
| 70 | + DIGCF_DEFAULT = 0x00000001, // only valid with DIGCF_DEVICEINTERFACE |
| 71 | + DIGCF_PRESENT = 0x00000002, |
| 72 | + DIGCF_ALLCLASSES = 0x00000004, |
| 73 | + DIGCF_PROFILE = 0x00000008, |
| 74 | + DIGCF_DEVICEINTERFACE = 0x00000010, |
| 75 | + } |
| 76 | + |
| 77 | + [StructLayout(LayoutKind.Sequential)] |
| 78 | + public struct SP_DEVINFO_DATA |
| 79 | + { |
| 80 | + public int cbSize; |
| 81 | + public Guid ClassGuid; |
| 82 | + public int DevInst; |
| 83 | + public int Reserved; |
| 84 | + } |
| 85 | + // Device interface detail data |
| 86 | + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] |
| 87 | + public struct DATA_BUFFER |
| 88 | + { |
| 89 | + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)] |
| 90 | + public string Buffer; |
| 91 | + } |
| 92 | + // use this to get SPDRP_HARDWAREID |
| 93 | + [DllImport("setupapi.dll", CharSet = CharSet.Auto)] |
| 94 | + public extern static IntPtr SetupDiGetClassDevs( // 1st form using a ClassGUID only, with null Enumerator |
| 95 | + ref Guid ClassGuid, |
| 96 | + IntPtr Enumerator, |
| 97 | + IntPtr hwndParent, |
| 98 | + DiGetClassFlags Flags |
| 99 | + ); |
| 100 | + [DllImport("setupapi.dll", CharSet = CharSet.Auto)] // 2nd form uses an Enumerator only, with null ClassGUID |
| 101 | + public extern static IntPtr SetupDiGetClassDevs( |
| 102 | + IntPtr ClassGuid, |
| 103 | + string Enumerator, |
| 104 | + IntPtr hwndParent, |
| 105 | + DiGetClassFlags Flags |
| 106 | + ); |
| 107 | + |
| 108 | + [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)] |
| 109 | + public static extern bool SetupDiGetDeviceInstanceId( |
| 110 | + IntPtr DeviceInfoSet, |
| 111 | + ref SP_DEVINFO_DATA DeviceInfoData, |
| 112 | + StringBuilder DeviceInstanceId, |
| 113 | + uint DeviceInstanceIdSize, |
| 114 | + out UInt32 RequiredSize |
| 115 | + ); |
| 116 | + |
| 117 | + [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)] |
| 118 | + public extern static bool SetupDiGetDeviceRegistryProperty( |
| 119 | + IntPtr deviceInfoSet, |
| 120 | + ref SP_DEVINFO_DATA deviceInfoData, |
| 121 | + RegPropertyType property, |
| 122 | + out UInt32 propertyRegDataType, |
| 123 | + StringBuilder DeviceInstanceId, |
| 124 | + uint propertyBufferSize, |
| 125 | + out UInt32 requiredSize |
| 126 | + ); |
| 127 | + |
| 128 | + [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)] |
| 129 | + public extern static bool SetupDiEnumDeviceInfo( |
| 130 | + IntPtr DeviceInfoSet, |
| 131 | + int Index, |
| 132 | + ref SP_DEVINFO_DATA DeviceInfoData |
| 133 | + ); |
| 134 | + [DllImport("setupapi.dll", SetLastError = true)] |
| 135 | + public static extern bool SetupDiDestroyDeviceInfoList( |
| 136 | + IntPtr DeviceInfoSet); |
| 137 | + } |
| 138 | +} |
0 commit comments