/*++
Copyright (c) Microsoft Corporation. All rights reserved.
Module Name:
devcon.cpp
Abstract:
Device Console
command-line interface for managing devices
--*/
#include "devcon.h"
struct GenericContext {
DWORD count;
DWORD control;
BOOL reboot;
LPCTSTR strSuccess;
LPCTSTR strReboot;
LPCTSTR strFail;
};
#define FIND_DEVICE 0x00000001 // display device
#define FIND_STATUS 0x00000002 // display status of device
#define FIND_RESOURCES 0x00000004 // display resources of device
#define FIND_DRIVERFILES 0x00000008 // display drivers used by device
#define FIND_HWIDS 0x00000010 // display hw/compat id's used by device
#define FIND_DRIVERNODES 0x00000020 // display driver nodes for a device.
#define FIND_CLASS 0x00000040 // display device's setup class
#define FIND_STACK 0x00000080 // display device's driver-stack
struct SetHwidContext {
int argc_right;
LPTSTR * argv_right;
DWORD prop;
int skipped;
int modified;
};
int cmdHelp(_In_ LPCTSTR BaseName, _In_opt_ LPCTSTR Machine, _In_ DWORD Flags, _In_ int argc, _In_reads_(argc) PTSTR argv[])
/*++
Routine Description:
HELP command
allow HELP or HELP <command>
Arguments:
BaseName - name of executable
Machine - if non-NULL, remote machine (ignored)
argc/argv - remaining parameters
Return Value:
EXIT_xxxx
--*/
{
DWORD helptext = 0;
int dispIndex;
LPCTSTR cmd = NULL;
BOOL unknown = FALSE;
UNREFERENCED_PARAMETER(Machine);
UNREFERENCED_PARAMETER(Flags);
if(argc) {
//
// user passed in a command for help on... long help
//
for(dispIndex = 0;DispatchTable[dispIndex].cmd;dispIndex++) {
if(_tcsicmp(argv[0],DispatchTable[dispIndex].cmd)==0) {
cmd = DispatchTable[dispIndex].cmd;
helptext = DispatchTable[dispIndex].longHelp;
break;
}
}
if(!cmd) {
unknown = TRUE;
cmd = argv[0];
}
}
if(helptext) {
//
// long help
//
FormatToStream(stdout,helptext,BaseName,cmd);
} else {
//
// help help
//
FormatToStream(stdout,unknown ? MSG_HELP_OTHER : MSG_HELP_LONG,BaseName,cmd);
//
// enumerate through each command and display short help for each
//
for(dispIndex = 0;DispatchTable[dispIndex].cmd;dispIndex++) {
if(DispatchTable[dispIndex].shortHelp) {
FormatToStream(stdout,DispatchTable[dispIndex].shortHelp,DispatchTable[dispIndex].cmd);
fputs("\n",stdout);
}
}
}
return EXIT_OK;
}
int cmdClasses(_In_ LPCTSTR BaseName, _In_opt_ LPCTSTR Machine, _In_ DWORD Flags, _In_ int argc, _In_reads_(argc) PTSTR argv[])
/*++
Routine Description:
CLASSES command
lists classes on (optionally) specified machine
format as <name>: <destination>
Arguments:
BaseName - name of executable
Machine - if non-NULL, remote machine
argc/argv - remaining parameters - ignored
Return Value:
EXIT_xxxx
--*/
{
DWORD reqGuids = 128;
DWORD numGuids;
LPGUID guids = NULL;
DWORD index;
int failcode = EXIT_FAIL;
UNREFERENCED_PARAMETER(BaseName);
UNREFERENCED_PARAMETER(Flags);
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(argv);
guids = new GUID[reqGuids];
if(!guids) {
goto final;
}
if(!SetupDiBuildClassInfoListEx(0,guids,reqGuids,&numGuids,Machine,NULL)) {
do {
if(GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
goto final;
}
delete [] guids;
reqGuids = numGuids;
guids = new GUID[reqGuids];
if(!guids) {
goto final;
}
} while(!SetupDiBuildClassInfoListEx(0,guids,reqGuids,&numGuids,Machine,NULL));
}
FormatToStream(stdout,Machine?MSG_CLASSES_HEADER:MSG_CLASSES_HEADER_LOCAL,numGuids,Machine);
for(index=0;index<numGuids;index++) {
TCHAR className[MAX_CLASS_NAME_LEN];
TCHAR classDesc[LINE_LEN];
if(!SetupDiClassNameFromGuidEx(&guids[index],className,MAX_CLASS_NAME_LEN,NULL,Machine,NULL)) {
if (FAILED(StringCchCopy(className,MAX_CLASS_NAME_LEN,TEXT("?")))) {
goto final;
}
}
if(!SetupDiGetClassDescriptionEx(&guids[index],classDesc,LINE_LEN,NULL,Machine,NULL)) {
if (FAILED(StringCchCopy(classDesc,LINE_LEN,className))) {
goto final;
}
}
_tprintf(TEXT("%-20s: %s\n"),className,classDesc);
}
failcode = EXIT_OK;
final:
if(guids) {
delete [] guids;
}
return failcode;
}
int cmdListClass(_In_ LPCTSTR BaseName, _In_opt_ LPCTSTR Machine, _In_ DWORD Flags, _In_ int argc, _In_reads_(argc) PTSTR argv[])
/*++
Routine Description:
LISTCLASS <name>....
lists all devices for each specified class
there can be more than one physical class for a class name (shouldn't be
though) in such cases, list each class
if machine given, list devices for that machine
Arguments:
BaseName - name of executable
Machine - if non-NULL, remote machine
argc/argv - remaining parameters - list of class names
Return Value:
EXIT_xxxx
--*/
{
DWORD reqGuids = 16;
int argIndex;
int failcode = EXIT_FAIL;
LPGUID guids = NULL;
HDEVINFO devs = INVALID_HANDLE_VALUE;
UNREFERENCED_PARAMETER(BaseName);
UNREFERENCED_PARAMETER(Flags);
if(!argc) {
return EXIT_USAGE;
}
guids = new GUID[reqGuids];
if(!guids) {
goto final;
}
for(argIndex = 0;argIndex<argc;argIndex++) {
DWORD numGuids;
DWORD index;
if(!(argv[argIndex] && argv[argIndex][0])) {
continue;
}
//
// there could be one to many name to GUID mapping
//
while(!SetupDiClassGuidsFromNameEx(argv[argIndex],guids,reqGuids,&numGuids,Machine,NULL)) {
if(GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
goto final;
}
delete [] guids;
reqGuids = numGuids;
guids = new GUID[reqGuids];
if(!guids) {
goto final;
}
}
if(numGuids == 0) {
FormatToStream(stdout,Machine?MSG_LISTCLASS_NOCLASS:MSG_LISTCLASS_NOCLASS_LOCAL,argv[argIndex],Machine);
continue;
}
for(index = 0;index<numGuids;index++) {
TCHAR className[MAX_CLASS_NAME_LEN];
TCHAR classDesc[LINE_LEN];
DWORD devCount = 0;
SP_DEVINFO_DATA devInfo;
DWORD devIndex;
devs = SetupDiGetClassDevsEx(&guids[index],NULL,NULL,DIGCF_PRESENT,NULL,Machine,NULL);
if(devs != INVALID_HANDLE_VALUE) {
//
// count number of devices
//
devInfo.cbSize = sizeof(devInfo);
while(SetupDiEnumDeviceInfo(devs,devCount,&devInfo)) {
devCount++;
}
}
if(!SetupDiClassNameFromGuidEx(&guids[index],className,MAX_CLASS_NAME_LEN,NULL,Machine,NULL)) {
if (FAILED(StringCchCopy(className,MAX_CLASS_NAME_LEN,TEXT("?")))) {
goto final;
}
}
if(!SetupDiGetClassDescriptionEx(&guids[index],classDesc,LINE_LEN,NULL,Machine,NULL)) {
if (FAILED(StringCchCopy(classDesc,LINE_LEN,className))) {
goto final;
}
评论2
最新资源