Mercurial > hg > ucis.core
annotate USBLib/Windows/Devices/DeviceNode.cs @ 63:309c705d7460
Updates and cleanup in Windows USB enumeration code
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Sun, 13 Oct 2013 18:43:45 +0200 |
parents | d4778c3232ad |
children | 9096f62d18c4 |
rev | line source |
---|---|
21 | 1 ???using System; |
2 using System.Collections.Generic; | |
3 using System.ComponentModel; | |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
4 using System.Reflection; |
21 | 5 using System.Runtime.InteropServices; |
6 using System.Text; | |
7 using Microsoft.Win32; | |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
8 using Microsoft.Win32.SafeHandles; |
21 | 9 using UCIS.USBLib.Internal.Windows; |
10 | |
11 namespace UCIS.HWLib.Windows.Devices { | |
12 public class CMException : Exception { | |
13 internal CMException(CR result, String method) | |
14 : base(method + " returned " + result.ToString()) { | |
15 } | |
16 internal static void Throw(CR result, String method) { | |
17 if (result == CR.SUCCESS) return; | |
18 throw new CMException(result, method); | |
19 } | |
20 } | |
21 public class DeviceNode { | |
22 public static DeviceNode Root { | |
23 get { | |
24 UInt32 node; | |
25 CR ret = SetupApi.CM_Locate_DevNode(out node, null, 0); | |
26 CMException.Throw(ret, "CM_Locate_DevNode"); | |
27 return new DeviceNode(node); | |
28 } | |
29 } | |
30 public static DeviceNode GetDevice(String deviceID) { | |
31 UInt32 node; | |
32 CR ret = SetupApi.CM_Locate_DevNode(out node, deviceID, 0); | |
33 CMException.Throw(ret, "CM_Locate_DevNode"); | |
34 return new DeviceNode(node); | |
35 } | |
36 private static IList<DeviceNode> GetDevicesInSet(SafeDeviceInfoSetHandle dis) { | |
37 List<DeviceNode> list = new List<DeviceNode>(); | |
38 if (dis.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error()); | |
39 SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true); | |
40 for (int index = 0; ; index++) { | |
41 if (!SetupApi.SetupDiEnumDeviceInfo(dis, index, ref dd)) break; | |
42 list.Add(new DeviceNode(dd.DevInst)); | |
43 } | |
44 return list; | |
45 } | |
46 public static IList<DeviceNode> GetDevices() { | |
47 using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, null, IntPtr.Zero, DICFG.PRESENT | DICFG.ALLCLASSES)) { | |
48 return GetDevicesInSet(dis); | |
49 } | |
50 } | |
51 public static IList<DeviceNode> GetDevices(Guid classGuid) { | |
52 using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(ref classGuid, null, IntPtr.Zero, DICFG.PRESENT | DICFG.DEVICEINTERFACE)) { | |
53 return GetDevicesInSet(dis); | |
54 } | |
55 } | |
56 public static IList<DeviceNode> GetDevices(String enumerator) { | |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
57 return GetDevices(enumerator, true); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
58 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
59 public static IList<DeviceNode> GetDevices(String enumerator, Boolean present) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
60 using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, enumerator, IntPtr.Zero, (present ? DICFG.PRESENT : 0) | DICFG.ALLCLASSES)) { |
21 | 61 //using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, enumerator, IntPtr.Zero, DICFG.ALLCLASSES | DICFG.DEVICEINTERFACE)) { |
62 return GetDevicesInSet(dis); | |
63 } | |
64 } | |
65 | |
66 public UInt32 DevInst { get; private set; } | |
67 private String _DeviceID = null; | |
68 internal DeviceNode(UInt32 node) { | |
69 DevInst = node; | |
70 } | |
71 public String DeviceID { | |
72 get { | |
73 if (_DeviceID == null) { | |
74 uint deviceidlen; | |
75 CR ret = SetupApi.CM_Get_Device_ID_Size(out deviceidlen, DevInst, 0); | |
76 CMException.Throw(ret, "CM_Get_Device_ID_Size"); | |
77 StringBuilder deviceid = new StringBuilder((int)deviceidlen); | |
78 ret = SetupApi.CM_Get_Device_ID(DevInst, deviceid, deviceid.MaxCapacity, 0); | |
79 CMException.Throw(ret, "CM_Get_Device_ID"); | |
80 _DeviceID = deviceid.ToString(); | |
81 } | |
82 return _DeviceID; | |
83 } | |
84 } | |
85 public Byte[] GetProperty(SPDRP property) { | |
86 using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) { | |
87 if (dis.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error()); | |
88 SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true); | |
89 if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd)) | |
90 return null; | |
91 //throw new Win32Exception(Marshal.GetLastWin32Error()); | |
92 RegistryValueKind propertyType; | |
93 byte[] propBuffer = new byte[256]; | |
94 int requiredSize; | |
95 if (!SetupApi.SetupDiGetDeviceRegistryProperty(dis, ref dd, property, out propertyType, propBuffer, propBuffer.Length, out requiredSize)) | |
96 return null; | |
97 if (requiredSize > propBuffer.Length) { | |
98 propBuffer = new Byte[requiredSize]; | |
99 if (!SetupApi.SetupDiGetDeviceRegistryProperty(dis, ref dd, property, out propertyType, propBuffer, propBuffer.Length, out requiredSize)) | |
100 throw new Win32Exception(Marshal.GetLastWin32Error()); | |
101 } | |
102 if (requiredSize < propBuffer.Length) Array.Resize(ref propBuffer, requiredSize); | |
103 return propBuffer; | |
104 } | |
105 } | |
106 public String GetPropertyString(SPDRP property) { | |
107 Byte[] buffer = GetProperty(property); | |
108 if (buffer == null) return null; | |
109 return SetupApi.GetAsString(buffer, buffer.Length); | |
110 } | |
111 public Byte[] GetProperty(CMRDP property) { | |
112 uint proplength = 0; | |
113 uint proptype; | |
114 CR ret = SetupApi.CM_Get_DevNode_Registry_Property(DevInst, property, out proptype, null, ref proplength, 0); | |
52 | 115 if (ret == CR.NO_SUCH_VALUE || ret == CR.NO_SUCH_DEVNODE) return null; |
21 | 116 if (ret != CR.BUFFER_SMALL) CMException.Throw(ret, "CM_Get_DevNode_Registry_Property"); |
117 Byte[] propbuffer = new Byte[proplength]; | |
118 ret = SetupApi.CM_Get_DevNode_Registry_Property(DevInst, property, out proptype, propbuffer, ref proplength, 0); | |
119 CMException.Throw(ret, "CM_Get_DevNode_Registry_Property"); | |
120 if (propbuffer.Length > proplength) Array.Resize(ref propbuffer, (int)proplength); | |
121 return propbuffer; | |
122 } | |
123 public String GetPropertyString(CMRDP property) { | |
124 Byte[] buffer = GetProperty(property); | |
125 if (buffer == null) return null; | |
126 return SetupApi.GetAsString(buffer, buffer.Length); | |
127 } | |
128 public String[] GetPropertyStringArray(CMRDP property) { | |
129 Byte[] buffer = GetProperty(property); | |
130 if (buffer == null) return null; | |
131 return SetupApi.GetAsStringArray(buffer, buffer.Length); | |
132 } | |
133 | |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
134 public void SetProperty(SPDRP property, Byte[] value) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
135 using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
136 if (dis.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error()); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
137 SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
138 if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd)) |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
139 throw new Win32Exception(Marshal.GetLastWin32Error()); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
140 if (!SetupApi.SetupDiSetDeviceRegistryProperty(dis, ref dd, property, value, (uint)value.Length)) |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
141 throw new Win32Exception(Marshal.GetLastWin32Error()); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
142 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
143 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
144 |
21 | 145 public Byte[] GetCustomProperty(String name) { |
146 using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) { | |
147 if (dis.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error()); | |
148 SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true); | |
149 if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd)) | |
150 return null; | |
151 //throw new Win32Exception(Marshal.GetLastWin32Error()); | |
152 RegistryValueKind propertyType; | |
153 byte[] propBuffer = new byte[256]; | |
154 int requiredSize; | |
155 if (!SetupApi.SetupDiGetCustomDeviceProperty(dis, ref dd, name, DICUSTOMDEVPROP.NONE, out propertyType, propBuffer, propBuffer.Length, out requiredSize)) | |
156 return null; | |
157 if (requiredSize > propBuffer.Length) { | |
158 propBuffer = new Byte[requiredSize]; | |
159 if (!SetupApi.SetupDiGetCustomDeviceProperty(dis, ref dd, name, DICUSTOMDEVPROP.NONE, out propertyType, propBuffer, propBuffer.Length, out requiredSize)) | |
160 throw new Win32Exception(Marshal.GetLastWin32Error()); | |
161 } | |
162 if (requiredSize < propBuffer.Length) Array.Resize(ref propBuffer, requiredSize); | |
163 return propBuffer; | |
164 } | |
165 } | |
166 public String GetCustomPropertyString(String name) { | |
167 Byte[] buffer = GetCustomProperty(name); | |
168 if (buffer == null) return null; | |
169 return SetupApi.GetAsString(buffer, buffer.Length); | |
170 } | |
171 public String[] GetCustomPropertyStringArray(String name) { | |
172 Byte[] buffer = GetCustomProperty(name); | |
173 if (buffer == null) return null; | |
174 return SetupApi.GetAsStringArray(buffer, buffer.Length); | |
175 } | |
176 | |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
177 public RegistryKey OpenRegistryKey(UInt32 scope, UInt32 hwProfile, UInt32 keyType, UInt32 samDesired) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
178 using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
179 if (dis.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error()); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
180 SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
181 if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd)) |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
182 return null; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
183 IntPtr handle = SetupApi.SetupDiOpenDevRegKey(dis, ref dd, scope, hwProfile, keyType, samDesired); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
184 if (handle == (IntPtr)(-1)) return null; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
185 return RegistryKeyFromHandle(handle, true, (samDesired & (0x00000002 | 0x00000004 | 0x00000020)) != 0); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
186 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
187 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
188 |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
189 private RegistryKey RegistryKeyFromHandle(IntPtr hKey, bool writable, bool ownsHandle) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
190 BindingFlags privateConstructors = BindingFlags.Instance | BindingFlags.NonPublic; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
191 Type safeRegistryHandleType = typeof(SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType("Microsoft.Win32.SafeHandles.SafeRegistryHandle"); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
192 Type[] safeRegistryHandleCtorTypes = new Type[] { typeof(IntPtr), typeof(bool) }; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
193 ConstructorInfo safeRegistryHandleCtorInfo = safeRegistryHandleType.GetConstructor(privateConstructors, null, safeRegistryHandleCtorTypes, null); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
194 Object safeHandle = safeRegistryHandleCtorInfo.Invoke(new Object[] { hKey, ownsHandle }); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
195 Type registryKeyType = typeof(RegistryKey); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
196 Type[] registryKeyConstructorTypes = new Type[] { safeRegistryHandleType, typeof(bool) }; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
197 ConstructorInfo registryKeyCtorInfo = registryKeyType.GetConstructor(privateConstructors, null, registryKeyConstructorTypes, null); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
198 RegistryKey resultKey = (RegistryKey)registryKeyCtorInfo.Invoke(new Object[] { safeHandle, writable }); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
199 return resultKey; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
200 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
201 |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
202 public Byte[] GetDeviceProperty(Guid fmtid, UInt32 pid) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
203 using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
204 if (dis.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error()); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
205 SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
206 if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd)) |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
207 return null; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
208 byte[] propBuffer = new byte[256]; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
209 UInt32 requiredSize; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
210 UInt32 propertyType; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
211 DEVPROPKEY propertyKey = new DEVPROPKEY() { fmtid = fmtid, pid = pid }; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
212 if (!SetupApi.SetupDiGetDeviceProperty(dis, ref dd, ref propertyKey, out propertyType, propBuffer, (uint)propBuffer.Length, out requiredSize, 0)) |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
213 return null; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
214 if (requiredSize > propBuffer.Length) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
215 propBuffer = new Byte[requiredSize]; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
216 if (!SetupApi.SetupDiGetDeviceProperty(dis, ref dd, ref propertyKey, out propertyType, propBuffer, (uint)propBuffer.Length, out requiredSize, 0)) |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
217 throw new Win32Exception(Marshal.GetLastWin32Error()); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
218 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
219 if (requiredSize < propBuffer.Length) Array.Resize(ref propBuffer, (int)requiredSize); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
220 return propBuffer; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
221 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
222 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
223 public String GetDevicePropertyString(Guid fmtid, UInt32 pid) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
224 Byte[] buffer = GetDeviceProperty(fmtid, pid); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
225 if (buffer == null) return null; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
226 return SetupApi.GetAsString(buffer, buffer.Length); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
227 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
228 |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
229 public void Reenumerate(UInt32 flags) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
230 CR ret = SetupApi.CM_Reenumerate_DevNode(DevInst, flags); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
231 CMException.Throw(ret, "CM_Reenumerate_DevNode"); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
232 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
233 |
21 | 234 public String DeviceDescription { get { return GetPropertyString(CMRDP.DEVICEDESC); } } |
235 public String[] HardwareID { get { return GetPropertyStringArray(CMRDP.HARDWAREID); } } | |
236 public String[] CompatibleIDs { get { return GetPropertyStringArray(CMRDP.COMPATIBLEIDS); } } | |
237 public String Service { get { return GetPropertyString(CMRDP.SERVICE); } } | |
238 public String Class { get { return GetPropertyString(CMRDP.CLASS); } } | |
239 public String ClassGuid { get { return GetPropertyString(CMRDP.CLASSGUID); } } | |
240 public String DriverKey { get { return GetPropertyString(CMRDP.DRIVER); } } | |
241 public String Manufacturer { get { return GetPropertyString(CMRDP.MFG); } } | |
242 public String FriendlyName { get { return GetPropertyString(CMRDP.FRIENDLYNAME); } } | |
243 public String LocationInformation { get { return GetPropertyString(CMRDP.LOCATION_INFORMATION); } } | |
244 public String PhysicalDeviceObjectName { get { return GetPropertyString(CMRDP.PHYSICAL_DEVICE_OBJECT_NAME); } } | |
245 public Guid? BusTypeGuid { get { return SetupApi.GetAsGuid(GetProperty(CMRDP.BUSTYPEGUID)); } } | |
246 public Int32? BusNumber { get { return SetupApi.GetAsInt32(GetProperty(CMRDP.BUSNUMBER)); } } | |
63
309c705d7460
Updates and cleanup in Windows USB enumeration code
Ivo Smits <Ivo@UCIS.nl>
parents:
52
diff
changeset
|
247 public Int32? Address { get { return SetupApi.GetAsInt32(GetProperty(CMRDP.ADDRESS)); } } |
21 | 248 public String EnumeratorName { get { return GetPropertyString(CMRDP.ENUMERATOR_NAME); } } |
249 | |
250 public String[] GetInterfaces(String classGuid) { | |
251 return GetInterfaces(new Guid(classGuid)); | |
252 } | |
253 public String[] GetInterfaces(Guid classGuid) { | |
254 uint len; | |
255 CR ret = SetupApi.CM_Get_Device_Interface_List_Size(out len, ref classGuid, DeviceID, 0); | |
256 CMException.Throw(ret, "CM_Get_Device_Interface_List_Size"); | |
257 if (len <= 1) return null; | |
258 Byte[] buffer = new Byte[2 * len]; | |
259 ret = SetupApi.CM_Get_Device_Interface_List(ref classGuid, DeviceID, buffer, len, 0); | |
260 CMException.Throw(ret, "CM_Get_Device_Interface_List"); | |
261 return SetupApi.GetAsStringArray(buffer, 2 * (int)len); | |
262 } | |
263 public Boolean SupportsInterface(Guid classGuid) { | |
264 uint len; | |
265 CR ret = SetupApi.CM_Get_Device_Interface_List_Size(out len, ref classGuid, DeviceID, 0); | |
266 CMException.Throw(ret, "CM_Get_Device_Interface_List_Size"); | |
267 return len > 2; | |
268 } | |
269 | |
270 public IList<DeviceNode> GetChildren() { | |
271 UInt32 child; | |
272 CR ret = SetupApi.CM_Get_Child(out child, DevInst, 0); | |
273 if (ret == CR.NO_SUCH_DEVNODE) return null; | |
274 CMException.Throw(ret, "CM_Get_Child"); | |
275 List<DeviceNode> list = new List<DeviceNode>(); | |
276 while (true) { | |
277 list.Add(new DeviceNode(child)); | |
278 ret = SetupApi.CM_Get_Sibling(out child, child, 0); | |
279 if (ret == CR.NO_SUCH_DEVNODE) break; | |
280 CMException.Throw(ret, "CM_Get_Sibling"); | |
281 } | |
282 return list; | |
283 } | |
284 | |
285 public DeviceNode GetParent() { | |
286 UInt32 node; | |
287 CR ret = SetupApi.CM_Get_Parent(out node, DevInst, 0); | |
288 if (ret == CR.NO_SUCH_DEVNODE) return null; | |
289 CMException.Throw(ret, "CM_Get_Parent"); | |
290 return new DeviceNode(node); | |
291 } | |
292 | |
293 public Boolean Present { | |
294 get { | |
295 UInt32 status, problem; | |
296 CR ret = SetupApi.CM_Get_DevNode_Status(out status, out problem, DevInst, 0); | |
297 if (ret == CR.NO_SUCH_DEVNODE) return false; | |
298 CMException.Throw(ret, "CM_Get_DevNode_Status"); | |
299 if (status == 25174016) return false; | |
300 return true; | |
301 } | |
302 } | |
303 | |
304 public override bool Equals(object obj) { | |
305 DeviceNode other = obj as DeviceNode; | |
306 if (ReferenceEquals(other, null)) return false; | |
307 return DevInst == other.DevInst; | |
308 } | |
309 public override int GetHashCode() { | |
310 return (int)DevInst; | |
311 } | |
312 public static Boolean operator ==(DeviceNode x, DeviceNode y) { | |
313 if (ReferenceEquals(x, y)) return true; | |
314 if (ReferenceEquals(x, null)) return false; | |
315 if (ReferenceEquals(y, null)) return false; | |
316 return x.DevInst == y.DevInst; | |
317 } | |
318 public static Boolean operator !=(DeviceNode x, DeviceNode y) { | |
319 return !(x == y); | |
320 } | |
321 | |
322 public void SetEnabled(Boolean enabled) { | |
323 using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) { | |
324 if (dis.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error()); | |
325 SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true); | |
326 if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd)) | |
327 throw new Win32Exception(Marshal.GetLastWin32Error()); | |
328 SP_PROPCHANGE_PARAMS PropChangeParams = new SP_PROPCHANGE_PARAMS(); | |
329 PropChangeParams.ClassInstallHeader.cbSize = Marshal.SizeOf(PropChangeParams.ClassInstallHeader); | |
330 PropChangeParams.ClassInstallHeader.InstallFunction = UsbApi.DIF_PROPERTYCHANGE; | |
331 PropChangeParams.Scope = UsbApi.DICS_FLAG_GLOBAL; // or use DICS_FLAG_CONFIGSPECIFIC to limit to current HW profile | |
332 PropChangeParams.HwProfile = 0; //Current hardware profile | |
333 PropChangeParams.StateChange = enabled ? UsbApi.DICS_ENABLE : UsbApi.DICS_DISABLE; | |
334 if (!SetupApi.SetupDiSetClassInstallParams(dis, ref dd, ref PropChangeParams.ClassInstallHeader, Marshal.SizeOf(PropChangeParams))) | |
335 throw new Win32Exception(Marshal.GetLastWin32Error()); | |
336 if (!SetupApi.SetupDiCallClassInstaller(UsbApi.DIF_PROPERTYCHANGE, dis, ref dd)) | |
337 throw new Win32Exception(Marshal.GetLastWin32Error()); | |
338 } | |
339 } | |
340 } | |
341 } |