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