forked from microsoft/Windows-driver-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devcon.cpp
1163 lines (954 loc) · 27 KB
/
devcon.cpp
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
Module Name:
devcon.cpp
Abstract:
Device Console
command-line interface for managing devices
--*/
#include "devcon.h"
struct IdEntry {
LPCTSTR String; // string looking for
LPCTSTR Wild; // first wild character if any
BOOL InstanceId;
};
void FormatToStream(_In_ FILE * stream, _In_ DWORD fmt,...)
/*++
Routine Description:
Format text to stream using a particular msg-id fmt
Used for displaying localizable messages
Arguments:
stream - file stream to output to, stdout or stderr
fmt - message id
... - parameters %1...
Return Value:
none
--*/
{
va_list arglist;
LPTSTR locbuffer = NULL;
DWORD count;
va_start(arglist, fmt);
count = FormatMessage(FORMAT_MESSAGE_FROM_HMODULE|FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL,
fmt,
0, // LANGID
(LPTSTR) &locbuffer,
0, // minimum size of buffer
&arglist);
if(locbuffer) {
if(count) {
int c;
int back = 0;
//
// strip any trailing "\r\n"s and replace by a single "\n"
//
while(((c = *CharPrev(locbuffer,locbuffer+count)) == TEXT('\r')) ||
(c == TEXT('\n'))) {
count--;
back++;
}
if(back) {
locbuffer[count++] = TEXT('\n');
locbuffer[count] = TEXT('\0');
}
//
// now write to apropriate stream
//
_fputts(locbuffer,stream);
}
LocalFree(locbuffer);
}
}
void Padding(_In_ int pad)
/*++
Routine Description:
Insert padding into line before text
Arguments:
pad - number of padding tabs to insert
Return Value:
none
--*/
{
int c;
for(c=0;c<pad;c++) {
fputs(" ",stdout);
}
}
void Usage(_In_ LPCTSTR BaseName)
/*++
Routine Description:
Display simple usage text
Arguments:
BaseName - name of executable
Return Value:
none
--*/
{
FormatToStream(stderr,MSG_USAGE,BaseName);
}
void CommandUsage(_In_ LPCTSTR BaseName, _In_ LPCTSTR Cmd)
/*++
Routine Description:
Invalid command usage
Display how to get help on command
Arguments:
BaseName - name of executable
Return Value:
none
--*/
{
FormatToStream(stderr,MSG_COMMAND_USAGE,BaseName,Cmd);
}
void Failure(_In_ LPCTSTR BaseName, _In_ LPCTSTR Cmd)
/*++
Routine Description:
Display simple error text for general failure
Arguments:
BaseName - name of executable
Return Value:
none
--*/
{
FormatToStream(stderr,MSG_FAILURE,BaseName,Cmd);
}
BOOL Reboot()
/*++
Routine Description:
Attempt to reboot computer
Arguments:
none
Return Value:
TRUE if API suceeded
--*/
{
HANDLE Token;
TOKEN_PRIVILEGES NewPrivileges;
LUID Luid;
//
// we need to "turn on" reboot privilege
// if any of this fails, try reboot anyway
//
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&Token)) {
goto final;
}
if(!LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&Luid)) {
CloseHandle(Token);
goto final;
}
NewPrivileges.PrivilegeCount = 1;
NewPrivileges.Privileges[0].Luid = Luid;
NewPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(
Token,
FALSE,
&NewPrivileges,
0,
NULL,
NULL
);
CloseHandle(Token);
final:
//
// attempt reboot - inform system that this is planned hardware install
//
// warning 28159 is a warning to rearchitect to avoid rebooting. However,
// sometimes during device installation, a reboot is needed, so this warning
// is being suppressed for the call to InitiateSystemShutdownEx.
//
#pragma warning( suppress: 28159)
return InitiateSystemShutdownEx(NULL,
NULL,
0,
FALSE,
TRUE,
REASON_PLANNED_FLAG | REASON_HWINSTALL);
}
LPTSTR GetDeviceStringProperty(_In_ HDEVINFO Devs, _In_ PSP_DEVINFO_DATA DevInfo, _In_ DWORD Prop)
/*++
Routine Description:
Return a string property for a device, otherwise NULL
Arguments:
Devs )_ uniquely identify device
DevInfo )
Prop - string property to obtain
Return Value:
string containing description
--*/
{
LPTSTR buffer;
DWORD size;
DWORD reqSize;
DWORD dataType;
DWORD szChars;
size = 1024; // initial guess
buffer = new TCHAR[(size/sizeof(TCHAR))+1];
if(!buffer) {
return NULL;
}
while(!SetupDiGetDeviceRegistryProperty(Devs,DevInfo,Prop,&dataType,(LPBYTE)buffer,size,&reqSize)) {
if(GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
goto failed;
}
if(dataType != REG_SZ) {
goto failed;
}
size = reqSize;
delete [] buffer;
buffer = new TCHAR[(size/sizeof(TCHAR))+1];
if(!buffer) {
goto failed;
}
}
szChars = reqSize/sizeof(TCHAR);
buffer[szChars] = TEXT('\0');
return buffer;
failed:
if(buffer) {
delete [] buffer;
}
return NULL;
}
LPTSTR GetDeviceDescription(_In_ HDEVINFO Devs, _In_ PSP_DEVINFO_DATA DevInfo)
/*++
Routine Description:
Return a string containing a description of the device, otherwise NULL
Always try friendly name first
Arguments:
Devs )_ uniquely identify device
DevInfo )
Return Value:
string containing description
--*/
{
LPTSTR desc;
desc = GetDeviceStringProperty(Devs,DevInfo,SPDRP_FRIENDLYNAME);
if(!desc) {
desc = GetDeviceStringProperty(Devs,DevInfo,SPDRP_DEVICEDESC);
}
return desc;
}
IdEntry GetIdType(_In_ LPCTSTR Id)
/*++
Routine Description:
Determine if this is instance id or hardware id and if there's any wildcards
instance ID is prefixed by '@'
wildcards are '*'
Arguments:
Id - ptr to string to check
Return Value:
IdEntry
--*/
{
IdEntry Entry;
Entry.InstanceId = FALSE;
Entry.Wild = NULL;
Entry.String = Id;
if(Entry.String[0] == INSTANCEID_PREFIX_CHAR) {
Entry.InstanceId = TRUE;
Entry.String = CharNext(Entry.String);
}
if(Entry.String[0] == QUOTE_PREFIX_CHAR) {
//
// prefix to treat rest of string literally
//
Entry.String = CharNext(Entry.String);
} else {
//
// see if any wild characters exist
//
Entry.Wild = _tcschr(Entry.String,WILD_CHAR);
}
return Entry;
}
__drv_allocatesMem(object)
LPTSTR * GetMultiSzIndexArray(_In_ __drv_aliasesMem LPTSTR MultiSz)
/*++
Routine Description:
Get an index array pointing to the MultiSz passed in
Arguments:
MultiSz - well formed multi-sz string
Return Value:
array of strings. last entry+1 of array contains NULL
returns NULL on failure
--*/
{
LPTSTR scan;
LPTSTR * array;
int elements;
for(scan = MultiSz, elements = 0; scan[0] ;elements++) {
scan += _tcslen(scan)+1;
}
array = new LPTSTR[elements+2];
if(!array) {
return NULL;
}
array[0] = MultiSz;
array++;
if(elements) {
for(scan = MultiSz, elements = 0; scan[0]; elements++) {
array[elements] = scan;
scan += _tcslen(scan)+1;
}
}
array[elements] = NULL;
return array;
}
__drv_allocatesMem(object)
LPTSTR * CopyMultiSz(_In_opt_ PZPWSTR Array)
/*++
Routine Description:
Creates a new array from old
old array need not have been allocated by GetMultiSzIndexArray
Arguments:
Array - array of strings, last entry is NULL
Return Value:
MultiSz array allocated by GetMultiSzIndexArray
--*/
{
LPTSTR multiSz = NULL;
HRESULT hr;
int cchMultiSz = 0;
int c;
if(Array) {
for(c=0;Array[c];c++) {
cchMultiSz+=(int)_tcslen(Array[c])+1;
}
}
cchMultiSz+=1; // final Null
multiSz = new TCHAR[cchMultiSz];
if(!multiSz) {
return NULL;
}
int len = 0;
if(Array) {
for(c=0;Array[c];c++) {
hr = StringCchCopy(multiSz+len,cchMultiSz-len,Array[c]);
if(FAILED(hr)){
if(multiSz)
delete [] multiSz;
return NULL;
}
#pragma prefast(suppress:__WARNING_BUFFER_OVERFLOW, "ESP:732")
len+=(int)_tcslen(multiSz+len)+1;
}
}
if( len < cchMultiSz ){
multiSz[len] = TEXT('\0');
} else {
// This should never happen!
multiSz[cchMultiSz-1] = TEXT('\0');
}
LPTSTR * pRes = GetMultiSzIndexArray(multiSz);
if(pRes) {
return pRes;
}
delete [] multiSz;
return NULL;
}
void DelMultiSz(_In_opt_ __drv_freesMem(object) PZPWSTR Array)
/*++
Routine Description:
Deletes the string array allocated by GetDevMultiSz/GetRegMultiSz/GetMultiSzIndexArray
Arguments:
Array - pointer returned by GetMultiSzIndexArray
Return Value:
None
--*/
{
if(Array) {
Array--;
if(Array[0]) {
delete [] Array[0];
}
delete [] Array;
}
}
__drv_allocatesMem(object)
LPTSTR * GetDevMultiSz(_In_ HDEVINFO Devs, _In_ PSP_DEVINFO_DATA DevInfo, _In_ DWORD Prop)
/*++
Routine Description:
Get a multi-sz device property
and return as an array of strings
Arguments:
Devs - HDEVINFO containing DevInfo
DevInfo - Specific device
Prop - SPDRP_HARDWAREID or SPDRP_COMPATIBLEIDS
Return Value:
array of strings. last entry+1 of array contains NULL
returns NULL on failure
--*/
{
LPTSTR buffer;
DWORD size;
DWORD reqSize;
DWORD dataType;
LPTSTR * array;
DWORD szChars;
size = 8192; // initial guess, nothing magic about this
buffer = new TCHAR[(size/sizeof(TCHAR))+2];
if(!buffer) {
return NULL;
}
while(!SetupDiGetDeviceRegistryProperty(Devs,DevInfo,Prop,&dataType,(LPBYTE)buffer,size,&reqSize)) {
if(GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
goto failed;
}
if(dataType != REG_MULTI_SZ) {
goto failed;
}
size = reqSize;
delete [] buffer;
buffer = new TCHAR[(size/sizeof(TCHAR))+2];
if(!buffer) {
goto failed;
}
}
szChars = reqSize/sizeof(TCHAR);
buffer[szChars] = TEXT('\0');
buffer[szChars+1] = TEXT('\0');
array = GetMultiSzIndexArray(buffer);
if(array) {
return array;
}
failed:
if(buffer) {
delete [] buffer;
}
return NULL;
}
__drv_allocatesMem(object)
LPTSTR * GetRegMultiSz(_In_ HKEY hKey, _In_ LPCTSTR Val)
/*++
Routine Description:
Get a multi-sz from registry
and return as an array of strings
Arguments:
hKey - Registry Key
Val - Value to query
Return Value:
array of strings. last entry+1 of array contains NULL
returns NULL on failure
--*/
{
LPTSTR buffer;
DWORD size;
DWORD reqSize;
DWORD dataType;
LPTSTR * array;
DWORD szChars;
LONG regErr;
size = 8192; // initial guess, nothing magic about this
buffer = new TCHAR[(size/sizeof(TCHAR))+2];
if(!buffer) {
return NULL;
}
reqSize = size;
regErr = RegQueryValueEx(hKey,Val,NULL,&dataType,(PBYTE)buffer,&reqSize);
while((regErr != NO_ERROR)) {
if(GetLastError() != ERROR_MORE_DATA) {
goto failed;
}
if(dataType != REG_MULTI_SZ) {
goto failed;
}
size = reqSize;
delete [] buffer;
buffer = new TCHAR[(size/sizeof(TCHAR))+2];
if(!buffer) {
goto failed;
}
regErr = RegQueryValueEx(hKey,Val,NULL,&dataType,(PBYTE)buffer,&reqSize);
}
szChars = reqSize/sizeof(TCHAR);
buffer[szChars] = TEXT('\0');
buffer[szChars+1] = TEXT('\0');
array = GetMultiSzIndexArray(buffer);
if(array) {
return array;
}
failed:
if(buffer) {
delete [] buffer;
}
return NULL;
}
BOOL WildCardMatch(_In_ LPCTSTR Item, _In_ const IdEntry & MatchEntry)
/*++
Routine Description:
Compare a single item against wildcard
I'm sure there's better ways of implementing this
Other than a command-line management tools
it's a bad idea to use wildcards as it implies
assumptions about the hardware/instance ID
eg, it might be tempting to enumerate root\* to
find all root devices, however there is a CfgMgr
API to query status and determine if a device is
root enumerated, which doesn't rely on implementation
details.
Arguments:
Item - item to find match for eg a\abcd\c
MatchEntry - eg *\*bc*\*
Return Value:
TRUE if any match, otherwise FALSE
--*/
{
LPCTSTR scanItem;
LPCTSTR wildMark;
LPCTSTR nextWild;
size_t matchlen;
//
// before attempting anything else
// try and compare everything up to first wild
//
if(!MatchEntry.Wild) {
return _tcsicmp(Item,MatchEntry.String) ? FALSE : TRUE;
}
if(_tcsnicmp(Item,MatchEntry.String,MatchEntry.Wild-MatchEntry.String) != 0) {
return FALSE;
}
wildMark = MatchEntry.Wild;
scanItem = Item + (MatchEntry.Wild-MatchEntry.String);
for(;wildMark[0];) {
//
// if we get here, we're either at or past a wildcard
//
if(wildMark[0] == WILD_CHAR) {
//
// so skip wild chars
//
wildMark = CharNext(wildMark);
continue;
}
//
// find next wild-card
//
nextWild = _tcschr(wildMark,WILD_CHAR);
if(nextWild) {
//
// substring
//
matchlen = nextWild-wildMark;
} else {
//
// last portion of match
//
size_t scanlen = _tcslen(scanItem);
matchlen = _tcslen(wildMark);
if(scanlen < matchlen) {
return FALSE;
}
return _tcsicmp(scanItem+scanlen-matchlen,wildMark) ? FALSE : TRUE;
}
if(_istalpha(wildMark[0])) {
//
// scan for either lower or uppercase version of first character
//
//
// the code suppresses the warning 28193 for the calls to _totupper
// and _totlower. This suppression is done because those functions
// have a check return annotation on them. However, they don't return
// error codes and the check return annotation is really being used
// to indicate that the return value of the function should be looked
// at and/or assigned to a variable. The check return annotation means
// the return value should always be checked in all code paths.
// We assign the return values to variables but the while loop does not
// examine both values in all code paths (e.g. when scanItem[0] == 0,
// neither u nor l will be examined) and it doesn't need to examine
// the values in all code paths.
//
#pragma warning( suppress: 28193)
TCHAR u = _totupper(wildMark[0]);
#pragma warning( suppress: 28193)
TCHAR l = _totlower(wildMark[0]);
while(scanItem[0] && scanItem[0]!=u && scanItem[0]!=l) {
scanItem = CharNext(scanItem);
}
if(!scanItem[0]) {
//
// ran out of string
//
return FALSE;
}
} else {
//
// scan for first character (no case)
//
scanItem = _tcschr(scanItem,wildMark[0]);
if(!scanItem) {
//
// ran out of string
//
return FALSE;
}
}
//
// try and match the sub-string at wildMark against scanItem
//
if(_tcsnicmp(scanItem,wildMark,matchlen)!=0) {
//
// nope, try again
//
scanItem = CharNext(scanItem);
continue;
}
//
// substring matched
//
scanItem += matchlen;
wildMark += matchlen;
}
return (wildMark[0] ? FALSE : TRUE);
}
BOOL WildCompareHwIds(_In_ PZPWSTR Array, _In_ const IdEntry & MatchEntry)
/*++
Routine Description:
Compares all strings in Array against Id
Use WildCardMatch to do real compare
Arguments:
Array - pointer returned by GetDevMultiSz
MatchEntry - string to compare against
Return Value:
TRUE if any match, otherwise FALSE
--*/
{
if(Array) {
while(Array[0]) {
if(WildCardMatch(Array[0],MatchEntry)) {
return TRUE;
}
Array++;
}
}
return FALSE;
}
bool SplitCommandLine(
_In_ int & argc,
_In_reads_(argc) LPTSTR * & argv,
_Out_ int & argc_right,
_Outref_result_buffer_(argc_right) LPTSTR * & argv_right)
/*++
Routine Description:
Splits a command line into left and right of :=
this is used for some of the more complex commands
Arguments:
argc/argv - in/out
- in, specifies the existing argc/argv
- out, specifies the argc/argv to left of :=
arc_right/argv_right - out
- specifies the argc/argv to right of :=
Return Value:
true - ":=" appears in line, false otherwise
--*/
{
int i;
for(i = 0;i<argc;i++) {
if(_tcsicmp(argv[i],SPLIT_COMMAND_SEP)==0) {
argc_right = argc-(i+1);
argv_right = argv+(i+1);
argc = i;
return true;
}
}
argc_right = 0;
argv_right = argv+argc;
return false;
}
int EnumerateDevices(_In_ LPCTSTR BaseName, _In_opt_ LPCTSTR Machine, _In_ DWORD Flags, _In_ int argc, _In_reads_(argc) PWSTR* argv, _In_ CallbackFunc Callback, _In_ LPVOID Context)
/*++
Routine Description:
Generic enumerator for devices that will be passed the following arguments:
<id> [<id>...]
=<class> [<id>...]
where <id> can either be @instance-id, or hardware-id and may contain wildcards
<class> is a class name
Arguments:
BaseName - name of executable
Machine - name of machine to enumerate
Flags - extra enumeration flags (eg DIGCF_PRESENT)
argc/argv - remaining arguments on command line
Callback - function to call for each hit
Context - data to pass function for each hit
Return Value:
EXIT_xxxx
--*/
{
HDEVINFO devs = INVALID_HANDLE_VALUE;
IdEntry * templ = NULL;
int failcode = EXIT_FAIL;
int retcode;
int argIndex;
DWORD devIndex;
SP_DEVINFO_DATA devInfo;
SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
BOOL doSearch = FALSE;
BOOL match;
BOOL all = FALSE;
GUID cls;
DWORD numClass = 0;
int skip = 0;
UNREFERENCED_PARAMETER(BaseName);
if(!argc) {
return EXIT_USAGE;
}
templ = new IdEntry[argc];
if(!templ) {
goto final;
}
//
// determine if a class is specified
//
if(argc>skip && argv[skip][0]==CLASS_PREFIX_CHAR && argv[skip][1]) {
if(!SetupDiClassGuidsFromNameEx(argv[skip]+1,&cls,1,&numClass,Machine,NULL) &&
GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
goto final;
}
if(!numClass) {
failcode = EXIT_OK;
goto final;
}
skip++;
}
if(argc>skip && argv[skip][0]==WILD_CHAR && !argv[skip][1]) {
//
// catch convinient case of specifying a single argument '*'
//
all = TRUE;
skip++;
} else if(argc<=skip) {
//
// at least one parameter, but no <id>'s
//
all = TRUE;
}
//
// determine if any instance id's were specified
//
// note, if =<class> was specified with no id's
// we'll mark it as not doSearch
// but will go ahead and add them all
//
for(argIndex=skip;argIndex<argc;argIndex++) {
templ[argIndex] = GetIdType(argv[argIndex]);
if(templ[argIndex].Wild || !templ[argIndex].InstanceId) {
//
// anything other than simple InstanceId's require a search
//
doSearch = TRUE;
}
}
if(doSearch || all) {
//
// add all id's to list
// if there's a class, filter on specified class
//
devs = SetupDiGetClassDevsEx(numClass ? &cls : NULL,
NULL,
NULL,
(numClass ? 0 : DIGCF_ALLCLASSES) | Flags,
NULL,
Machine,
NULL);
} else {
//
// blank list, we'll add instance id's by hand
//
devs = SetupDiCreateDeviceInfoListEx(numClass ? &cls : NULL,
NULL,
Machine,
NULL);
}
if(devs == INVALID_HANDLE_VALUE) {
goto final;
}
for(argIndex=skip;argIndex<argc;argIndex++) {
//
// add explicit instances to list (even if enumerated all,
// this gets around DIGCF_PRESENT)
// do this even if wildcards appear to be detected since they
// might actually be part of the instance ID of a non-present device
//
if(templ[argIndex].InstanceId) {
SetupDiOpenDeviceInfo(devs,templ[argIndex].String,NULL,0,NULL);
}
}
devInfoListDetail.cbSize = sizeof(devInfoListDetail);
if(!SetupDiGetDeviceInfoListDetail(devs,&devInfoListDetail)) {
goto final;
}
//
// now enumerate them
//
if(all) {
doSearch = FALSE;
}
devInfo.cbSize = sizeof(devInfo);
for(devIndex=0;SetupDiEnumDeviceInfo(devs,devIndex,&devInfo);devIndex++) {
if(doSearch) {
for(argIndex=skip,match=FALSE;(argIndex<argc) && !match;argIndex++) {
TCHAR devID[MAX_DEVICE_ID_LEN];
LPTSTR *hwIds = NULL;
LPTSTR *compatIds = NULL;
//
// determine instance ID
//
if(CM_Get_Device_ID_Ex(devInfo.DevInst,devID,MAX_DEVICE_ID_LEN,0,devInfoListDetail.RemoteMachineHandle)!=CR_SUCCESS) {
devID[0] = TEXT('\0');
}
if(templ[argIndex].InstanceId) {
//
// match on the instance ID
//
if(WildCardMatch(devID,templ[argIndex])) {
match = TRUE;
}
} else {
//
// determine hardware ID's