Mercurial > hg > ucis.core
annotate USBLib/Windows/Devices/DeviceNode.cs @ 111:df53bdd49507 default tip
Merge
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Fri, 07 Nov 2014 18:37:39 +0100 |
parents | d467cd38b34e |
children |
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 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 RegistryValueKind propertyType; | |
92 byte[] propBuffer = new byte[256]; | |
93 int requiredSize; | |
94 if (!SetupApi.SetupDiGetDeviceRegistryProperty(dis, ref dd, property, out propertyType, propBuffer, propBuffer.Length, out requiredSize)) | |
95 return null; | |
96 if (requiredSize > propBuffer.Length) { | |
97 propBuffer = new Byte[requiredSize]; | |
98 if (!SetupApi.SetupDiGetDeviceRegistryProperty(dis, ref dd, property, out propertyType, propBuffer, propBuffer.Length, out requiredSize)) | |
99 throw new Win32Exception(Marshal.GetLastWin32Error()); | |
100 } | |
101 if (requiredSize < propBuffer.Length) Array.Resize(ref propBuffer, requiredSize); | |
102 return propBuffer; | |
103 } | |
104 } | |
105 public String GetPropertyString(SPDRP property) { | |
106 Byte[] buffer = GetProperty(property); | |
107 if (buffer == null) return null; | |
108 return SetupApi.GetAsString(buffer, buffer.Length); | |
109 } | |
110 public Byte[] GetProperty(CMRDP property) { | |
111 uint proplength = 0; | |
112 uint proptype; | |
113 CR ret = SetupApi.CM_Get_DevNode_Registry_Property(DevInst, property, out proptype, null, ref proplength, 0); | |
52 | 114 if (ret == CR.NO_SUCH_VALUE || ret == CR.NO_SUCH_DEVNODE) return null; |
21 | 115 if (ret != CR.BUFFER_SMALL) CMException.Throw(ret, "CM_Get_DevNode_Registry_Property"); |
116 Byte[] propbuffer = new Byte[proplength]; | |
117 ret = SetupApi.CM_Get_DevNode_Registry_Property(DevInst, property, out proptype, propbuffer, ref proplength, 0); | |
118 CMException.Throw(ret, "CM_Get_DevNode_Registry_Property"); | |
119 if (propbuffer.Length > proplength) Array.Resize(ref propbuffer, (int)proplength); | |
120 return propbuffer; | |
121 } | |
122 public String GetPropertyString(CMRDP property) { | |
123 Byte[] buffer = GetProperty(property); | |
124 if (buffer == null) return null; | |
125 return SetupApi.GetAsString(buffer, buffer.Length); | |
126 } | |
127 public String[] GetPropertyStringArray(CMRDP property) { | |
128 Byte[] buffer = GetProperty(property); | |
129 if (buffer == null) return null; | |
130 return SetupApi.GetAsStringArray(buffer, buffer.Length); | |
131 } | |
132 | |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
133 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
|
134 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
|
135 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
|
136 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
|
137 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
|
138 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
|
139 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
|
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 } |
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 |
21 | 144 public Byte[] GetCustomProperty(String name) { |
145 using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) { | |
146 if (dis.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error()); | |
147 SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true); | |
148 if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd)) | |
149 return null; | |
150 RegistryValueKind propertyType; | |
151 byte[] propBuffer = new byte[256]; | |
152 int requiredSize; | |
153 if (!SetupApi.SetupDiGetCustomDeviceProperty(dis, ref dd, name, DICUSTOMDEVPROP.NONE, out propertyType, propBuffer, propBuffer.Length, out requiredSize)) | |
154 return null; | |
155 if (requiredSize > propBuffer.Length) { | |
156 propBuffer = new Byte[requiredSize]; | |
157 if (!SetupApi.SetupDiGetCustomDeviceProperty(dis, ref dd, name, DICUSTOMDEVPROP.NONE, out propertyType, propBuffer, propBuffer.Length, out requiredSize)) | |
158 throw new Win32Exception(Marshal.GetLastWin32Error()); | |
159 } | |
160 if (requiredSize < propBuffer.Length) Array.Resize(ref propBuffer, requiredSize); | |
161 return propBuffer; | |
162 } | |
163 } | |
164 public String GetCustomPropertyString(String name) { | |
165 Byte[] buffer = GetCustomProperty(name); | |
166 if (buffer == null) return null; | |
167 return SetupApi.GetAsString(buffer, buffer.Length); | |
168 } | |
169 public String[] GetCustomPropertyStringArray(String name) { | |
170 Byte[] buffer = GetCustomProperty(name); | |
171 if (buffer == null) return null; | |
172 return SetupApi.GetAsStringArray(buffer, buffer.Length); | |
173 } | |
174 | |
50
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
175 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
|
176 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
|
177 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
|
178 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
|
179 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
|
180 return null; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
181 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
|
182 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
|
183 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
|
184 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
185 } |
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 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
|
188 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
|
189 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
|
190 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
|
191 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
|
192 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
|
193 Type registryKeyType = typeof(RegistryKey); |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
194 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
|
195 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
|
196 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
|
197 return resultKey; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
198 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
199 |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
200 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
|
201 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
|
202 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
|
203 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
|
204 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
|
205 return null; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
206 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
|
207 UInt32 requiredSize; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
208 UInt32 propertyType; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
209 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
|
210 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
|
211 return null; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
212 if (requiredSize > propBuffer.Length) { |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
213 propBuffer = new Byte[requiredSize]; |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
214 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
|
215 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
|
216 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
217 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
|
218 return propBuffer; |
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 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
221 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
|
222 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
|
223 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
|
224 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
|
225 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
226 |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
227 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
|
228 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
|
229 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
|
230 } |
556b4fb511bd
Added HWLib and USBLib functions to support USB Driver Installer
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
231 |
21 | 232 public String DeviceDescription { get { return GetPropertyString(CMRDP.DEVICEDESC); } } |
233 public String[] HardwareID { get { return GetPropertyStringArray(CMRDP.HARDWAREID); } } | |
234 public String[] CompatibleIDs { get { return GetPropertyStringArray(CMRDP.COMPATIBLEIDS); } } | |
235 public String Service { get { return GetPropertyString(CMRDP.SERVICE); } } | |
236 public String Class { get { return GetPropertyString(CMRDP.CLASS); } } | |
237 public String ClassGuid { get { return GetPropertyString(CMRDP.CLASSGUID); } } | |
238 public String DriverKey { get { return GetPropertyString(CMRDP.DRIVER); } } | |
239 public String Manufacturer { get { return GetPropertyString(CMRDP.MFG); } } | |
240 public String FriendlyName { get { return GetPropertyString(CMRDP.FRIENDLYNAME); } } | |
241 public String LocationInformation { get { return GetPropertyString(CMRDP.LOCATION_INFORMATION); } } | |
242 public String PhysicalDeviceObjectName { get { return GetPropertyString(CMRDP.PHYSICAL_DEVICE_OBJECT_NAME); } } | |
243 public Guid? BusTypeGuid { get { return SetupApi.GetAsGuid(GetProperty(CMRDP.BUSTYPEGUID)); } } | |
244 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
|
245 public Int32? Address { get { return SetupApi.GetAsInt32(GetProperty(CMRDP.ADDRESS)); } } |
21 | 246 public String EnumeratorName { get { return GetPropertyString(CMRDP.ENUMERATOR_NAME); } } |
247 | |
248 public String[] GetInterfaces(String classGuid) { | |
249 return GetInterfaces(new Guid(classGuid)); | |
250 } | |
251 public String[] GetInterfaces(Guid classGuid) { | |
252 uint len; | |
253 CR ret = SetupApi.CM_Get_Device_Interface_List_Size(out len, ref classGuid, DeviceID, 0); | |
254 CMException.Throw(ret, "CM_Get_Device_Interface_List_Size"); | |
255 if (len <= 1) return null; | |
256 Byte[] buffer = new Byte[2 * len]; | |
257 ret = SetupApi.CM_Get_Device_Interface_List(ref classGuid, DeviceID, buffer, len, 0); | |
258 CMException.Throw(ret, "CM_Get_Device_Interface_List"); | |
259 return SetupApi.GetAsStringArray(buffer, 2 * (int)len); | |
260 } | |
261 public Boolean SupportsInterface(Guid classGuid) { | |
262 uint len; | |
263 CR ret = SetupApi.CM_Get_Device_Interface_List_Size(out len, ref classGuid, DeviceID, 0); | |
264 CMException.Throw(ret, "CM_Get_Device_Interface_List_Size"); | |
265 return len > 2; | |
266 } | |
267 | |
268 public IList<DeviceNode> GetChildren() { | |
269 UInt32 child; | |
270 CR ret = SetupApi.CM_Get_Child(out child, DevInst, 0); | |
271 if (ret == CR.NO_SUCH_DEVNODE) return null; | |
272 CMException.Throw(ret, "CM_Get_Child"); | |
273 List<DeviceNode> list = new List<DeviceNode>(); | |
274 while (true) { | |
275 list.Add(new DeviceNode(child)); | |
276 ret = SetupApi.CM_Get_Sibling(out child, child, 0); | |
277 if (ret == CR.NO_SUCH_DEVNODE) break; | |
278 CMException.Throw(ret, "CM_Get_Sibling"); | |
279 } | |
280 return list; | |
281 } | |
282 | |
283 public DeviceNode GetParent() { | |
284 UInt32 node; | |
285 CR ret = SetupApi.CM_Get_Parent(out node, DevInst, 0); | |
286 if (ret == CR.NO_SUCH_DEVNODE) return null; | |
287 CMException.Throw(ret, "CM_Get_Parent"); | |
288 return new DeviceNode(node); | |
289 } | |
290 | |
90 | 291 public Boolean GetStatus(out UInt32 status, out UInt32 problem) { |
292 CR ret = SetupApi.CM_Get_DevNode_Status(out status, out problem, DevInst, 0); | |
293 if (ret == CR.NO_SUCH_DEVNODE) return false; | |
294 CMException.Throw(ret, "CM_Get_DevNode_Status"); | |
295 return true; | |
296 } | |
297 | |
21 | 298 public Boolean Present { |
299 get { | |
300 UInt32 status, problem; | |
90 | 301 return GetStatus(out status, out problem) && status != 25174016; |
21 | 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 } | |
93
d467cd38b34e
USBLib: fix for large transfers with libusb0 on Windows, added driver uninstall function
Ivo Smits <Ivo@UCIS.nl>
parents:
90
diff
changeset
|
341 |
d467cd38b34e
USBLib: fix for large transfers with libusb0 on Windows, added driver uninstall function
Ivo Smits <Ivo@UCIS.nl>
parents:
90
diff
changeset
|
342 public Boolean Uninstall() { |
d467cd38b34e
USBLib: fix for large transfers with libusb0 on Windows, added driver uninstall function
Ivo Smits <Ivo@UCIS.nl>
parents:
90
diff
changeset
|
343 using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) { |
d467cd38b34e
USBLib: fix for large transfers with libusb0 on Windows, added driver uninstall function
Ivo Smits <Ivo@UCIS.nl>
parents:
90
diff
changeset
|
344 if (dis.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error()); |
d467cd38b34e
USBLib: fix for large transfers with libusb0 on Windows, added driver uninstall function
Ivo Smits <Ivo@UCIS.nl>
parents:
90
diff
changeset
|
345 SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true); |
d467cd38b34e
USBLib: fix for large transfers with libusb0 on Windows, added driver uninstall function
Ivo Smits <Ivo@UCIS.nl>
parents:
90
diff
changeset
|
346 if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd)) |
d467cd38b34e
USBLib: fix for large transfers with libusb0 on Windows, added driver uninstall function
Ivo Smits <Ivo@UCIS.nl>
parents:
90
diff
changeset
|
347 throw new Win32Exception(Marshal.GetLastWin32Error()); |
d467cd38b34e
USBLib: fix for large transfers with libusb0 on Windows, added driver uninstall function
Ivo Smits <Ivo@UCIS.nl>
parents:
90
diff
changeset
|
348 Boolean needsReboot; |
d467cd38b34e
USBLib: fix for large transfers with libusb0 on Windows, added driver uninstall function
Ivo Smits <Ivo@UCIS.nl>
parents:
90
diff
changeset
|
349 if (!SetupApi.DiUninstallDevice(IntPtr.Zero, dis, ref dd, 0, out needsReboot)) |
d467cd38b34e
USBLib: fix for large transfers with libusb0 on Windows, added driver uninstall function
Ivo Smits <Ivo@UCIS.nl>
parents:
90
diff
changeset
|
350 throw new Win32Exception(Marshal.GetLastWin32Error()); |
d467cd38b34e
USBLib: fix for large transfers with libusb0 on Windows, added driver uninstall function
Ivo Smits <Ivo@UCIS.nl>
parents:
90
diff
changeset
|
351 return needsReboot; |
d467cd38b34e
USBLib: fix for large transfers with libusb0 on Windows, added driver uninstall function
Ivo Smits <Ivo@UCIS.nl>
parents:
90
diff
changeset
|
352 } |
d467cd38b34e
USBLib: fix for large transfers with libusb0 on Windows, added driver uninstall function
Ivo Smits <Ivo@UCIS.nl>
parents:
90
diff
changeset
|
353 } |
21 | 354 } |
355 } |