Sample Code
windows driver samples/ USBView sample application/ C++/ devnode.c/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | /*++ Copyright (c) 1998-2011 Microsoft Corporation Module Name: DEVNODE.C --*/ /***************************************************************************** I N C L U D E S *****************************************************************************/ #include "uvcview.h" /***************************************************************************** DriverNameToDeviceInst() Finds the Device instance of the DevNode with the matching DriverName. Returns FALSE if the matching DevNode is not found and TRUE if found *****************************************************************************/ BOOL DriverNameToDeviceInst( _In_reads_bytes_(cbDriverName) PCHAR DriverName, _In_ size_t cbDriverName, _Out_ HDEVINFO *pDevInfo, _Out_writes_bytes_( sizeof (SP_DEVINFO_DATA)) PSP_DEVINFO_DATA pDevInfoData ) { HDEVINFO deviceInfo = INVALID_HANDLE_VALUE; BOOL status = TRUE; ULONG deviceIndex; SP_DEVINFO_DATA deviceInfoData; BOOL bResult = FALSE; PCHAR pDriverName = NULL; PSTR buf = NULL; BOOL done = FALSE; if (pDevInfo == NULL) { return FALSE; } if (pDevInfoData == NULL) { return FALSE; } memset (pDevInfoData, 0, sizeof (SP_DEVINFO_DATA)); *pDevInfo = INVALID_HANDLE_VALUE; // Use local string to guarantee zero termination pDriverName = ( PCHAR ) ALLOC(( DWORD ) cbDriverName + 1); if (NULL == pDriverName) { status = FALSE; goto Done; } StringCbCopyN(pDriverName, cbDriverName + 1, DriverName, cbDriverName); // // We cannot walk the device tree with CM_Get_Sibling etc. unless we assume // the device tree will stabilize. Any devnode removal (even outside of USB) // would force us to retry. Instead we use Setup API to snapshot all // devices. // // Examine all present devices to see if any match the given DriverName // deviceInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT); if (deviceInfo == INVALID_HANDLE_VALUE) { status = FALSE; goto Done; } deviceIndex = 0; deviceInfoData.cbSize = sizeof (deviceInfoData); while (done == FALSE) { // // Get devinst of the next device // status = SetupDiEnumDeviceInfo(deviceInfo, deviceIndex, &deviceInfoData); deviceIndex++; if (!status) { // // This could be an error, or indication that all devices have been // processed. Either way the desired device was not found. // done = TRUE; break ; } // // Get the DriverName value // bResult = GetDeviceProperty(deviceInfo, &deviceInfoData, SPDRP_DRIVER, &buf); // If the DriverName value matches, return the DeviceInstance // if (bResult == TRUE && buf != NULL && _stricmp(pDriverName, buf) == 0) { done = TRUE; *pDevInfo = deviceInfo; CopyMemory(pDevInfoData, &deviceInfoData, sizeof (deviceInfoData)); FREE(buf); break ; } if (buf != NULL) { FREE(buf); buf = NULL; } } Done: if (bResult == FALSE) { if (deviceInfo != INVALID_HANDLE_VALUE) { SetupDiDestroyDeviceInfoList(deviceInfo); } } if (pDriverName != NULL) { FREE(pDriverName); } return status; } /***************************************************************************** DriverNameToDeviceProperties() Returns the Device properties of the DevNode with the matching DriverName. Returns NULL if the matching DevNode is not found. The caller should free the returned structure using FREE() macro *****************************************************************************/ PUSB_DEVICE_PNP_STRINGS DriverNameToDeviceProperties( _In_reads_bytes_(cbDriverName) PCHAR DriverName, _In_ size_t cbDriverName ) { HDEVINFO deviceInfo = INVALID_HANDLE_VALUE; SP_DEVINFO_DATA deviceInfoData = {0}; ULONG len; BOOL status; PUSB_DEVICE_PNP_STRINGS DevProps = NULL; DWORD lastError; // Allocate device propeties structure DevProps = (PUSB_DEVICE_PNP_STRINGS) ALLOC( sizeof (USB_DEVICE_PNP_STRINGS)); if (NULL == DevProps) { status = FALSE; goto Done; } // Get device instance status = DriverNameToDeviceInst(DriverName, cbDriverName, &deviceInfo, &deviceInfoData); if (status == FALSE) { goto Done; } len = 0; status = SetupDiGetDeviceInstanceId(deviceInfo, &deviceInfoData, NULL, 0, &len); lastError = GetLastError(); if (status != FALSE && lastError != ERROR_INSUFFICIENT_BUFFER) { status = FALSE; goto Done; } // // An extra byte is required for the terminating character // len++; DevProps->DeviceId = ALLOC(len); if (DevProps->DeviceId == NULL) { status = FALSE; goto Done; } status = SetupDiGetDeviceInstanceId(deviceInfo, &deviceInfoData, DevProps->DeviceId, len, &len); if (status == FALSE) { goto Done; } status = GetDeviceProperty(deviceInfo, &deviceInfoData, SPDRP_DEVICEDESC, &DevProps->DeviceDesc); if (status == FALSE) { goto Done; } // // We don't fail if the following registry query fails as these fields are additional information only // GetDeviceProperty(deviceInfo, &deviceInfoData, SPDRP_HARDWAREID, &DevProps->HwId); GetDeviceProperty(deviceInfo, &deviceInfoData, SPDRP_SERVICE, &DevProps->Service); GetDeviceProperty(deviceInfo, &deviceInfoData, SPDRP_CLASS, &DevProps->DeviceClass); Done: if (deviceInfo != INVALID_HANDLE_VALUE) { SetupDiDestroyDeviceInfoList(deviceInfo); } if (status == FALSE) { if (DevProps != NULL) { FreeDeviceProperties(&DevProps); } } return DevProps; } /***************************************************************************** FreeDeviceProperties() Free the device properties structure *****************************************************************************/ VOID FreeDeviceProperties(_In_ PUSB_DEVICE_PNP_STRINGS *ppDevProps) { if (ppDevProps == NULL) { return ; } if (*ppDevProps == NULL) { return ; } if ((*ppDevProps)->DeviceId != NULL) { FREE((*ppDevProps)->DeviceId); } if ((*ppDevProps)->DeviceDesc != NULL) { FREE((*ppDevProps)->DeviceDesc); } // // The following are not necessary, but left in case // in the future there is a later failure where these // pointer fields would be allocated. // if ((*ppDevProps)->HwId != NULL) { FREE((*ppDevProps)->HwId); } if ((*ppDevProps)->Service != NULL) { FREE((*ppDevProps)->Service); } if ((*ppDevProps)->DeviceClass != NULL) { FREE((*ppDevProps)->DeviceClass); } if ((*ppDevProps)->PowerState != NULL) { FREE((*ppDevProps)->PowerState); } FREE(*ppDevProps); *ppDevProps = NULL; } |
Our Services
-
What our customers say about us?
Read our customer testimonials to find out why our clients keep returning for their projects.
View Testimonials