📄 cmds.cpp
字号:
int cmdDriverFiles(LPCTSTR BaseName,LPCTSTR Machine,int argc,TCHAR* argv[])
/*++
Routine Description:
STATUS <id> ...
use EnumerateDevices to do hardwareID matching
for each match, dump driver files to stdout
note that we only enumerate present devices
Arguments:
BaseName - name of executable
Machine - if non-NULL, remote machine
argc/argv - remaining parameters - passed into EnumerateDevices
Return Value:
EXIT_xxxx
--*/
{
GenericContext context;
int failcode;
if(!argc) {
return EXIT_USAGE;
}
if(Machine) {
//
// must be local machine as we need to involve class/co installers (FIND_DRIVERFILES)
//
return EXIT_USAGE;
}
context.count = 0;
context.control = FIND_DEVICE | FIND_DRIVERFILES;
failcode = EnumerateDevices(BaseName,Machine,DIGCF_PRESENT,argc,argv,FindCallback,&context);
if(failcode == EXIT_OK) {
if(!context.count) {
FormatToStream(stdout,Machine?MSG_FIND_TAIL_NONE:MSG_FIND_TAIL_NONE_LOCAL,Machine);
} else {
FormatToStream(stdout,Machine?MSG_FIND_TAIL:MSG_FIND_TAIL_LOCAL,context.count,Machine);
}
}
return failcode;
}
int cmdDriverNodes(LPCTSTR BaseName,LPCTSTR Machine,int argc,TCHAR* argv[])
/*++
Routine Description:
STATUS <id> ...
use EnumerateDevices to do hardwareID matching
for each match, dump drivernodes to stdout
note that we only enumerate present devices
Arguments:
BaseName - name of executable
Machine - if non-NULL, remote machine
argc/argv - remaining parameters - passed into EnumerateDevices
Return Value:
EXIT_xxxx
--*/
{
GenericContext context;
int failcode;
if(!argc) {
return EXIT_USAGE;
}
if(Machine) {
//
// must be local machine as we need to involve class/co installers (FIND_DRIVERNODES)
//
return EXIT_USAGE;
}
context.count = 0;
context.control = FIND_DEVICE | FIND_DRIVERNODES;
failcode = EnumerateDevices(BaseName,Machine,DIGCF_PRESENT,argc,argv,FindCallback,&context);
if(failcode == EXIT_OK) {
if(!context.count) {
FormatToStream(stdout,Machine?MSG_FIND_TAIL_NONE:MSG_FIND_TAIL_NONE_LOCAL,Machine);
} else {
FormatToStream(stdout,Machine?MSG_FIND_TAIL:MSG_FIND_TAIL_LOCAL,context.count,Machine);
}
}
return failcode;
}
int cmdHwIds(LPCTSTR BaseName,LPCTSTR Machine,int argc,TCHAR* argv[])
/*++
Routine Description:
HWIDS <id> ...
use EnumerateDevices to do hardwareID matching
for each match, dump hw/compat id's to stdout
note that we only enumerate present devices
Arguments:
BaseName - name of executable
Machine - if non-NULL, remote machine
argc/argv - remaining parameters - passed into EnumerateDevices
Return Value:
EXIT_xxxx
--*/
{
GenericContext context;
int failcode;
if(!argc) {
return EXIT_USAGE;
}
context.count = 0;
context.control = FIND_DEVICE | FIND_HWIDS;
failcode = EnumerateDevices(BaseName,Machine,DIGCF_PRESENT,argc,argv,FindCallback,&context);
if(failcode == EXIT_OK) {
if(!context.count) {
FormatToStream(stdout,Machine?MSG_FIND_TAIL_NONE:MSG_FIND_TAIL_NONE_LOCAL,Machine);
} else {
FormatToStream(stdout,Machine?MSG_FIND_TAIL:MSG_FIND_TAIL_LOCAL,context.count,Machine);
}
}
return failcode;
}
int cmdStack(LPCTSTR BaseName,LPCTSTR Machine,int argc,TCHAR* argv[])
/*++
Routine Description:
STACK <id> ...
use EnumerateDevices to do hardwareID matching
for each match, dump device class and stack to stdout
note that we only enumerate present devices
Arguments:
BaseName - name of executable
Machine - if non-NULL, remote machine
argc/argv - remaining parameters - passed into EnumerateDevices
Return Value:
EXIT_xxxx
--*/
{
GenericContext context;
int failcode;
if(!argc) {
return EXIT_USAGE;
}
context.count = 0;
context.control = FIND_DEVICE | FIND_CLASS | FIND_STACK;
failcode = EnumerateDevices(BaseName,Machine,DIGCF_PRESENT,argc,argv,FindCallback,&context);
if(failcode == EXIT_OK) {
if(!context.count) {
FormatToStream(stdout,Machine?MSG_FIND_TAIL_NONE:MSG_FIND_TAIL_NONE_LOCAL,Machine);
} else {
FormatToStream(stdout,Machine?MSG_FIND_TAIL:MSG_FIND_TAIL_LOCAL,context.count,Machine);
}
}
return failcode;
}
int ControlCallback(HDEVINFO Devs,PSP_DEVINFO_DATA DevInfo,DWORD Index,LPVOID Context)
/*++
Routine Description:
Callback for use by Enable/Disable/Restart
Invokes DIF_PROPERTYCHANGE with correct parameters
uses SetupDiCallClassInstaller so cannot be done for remote devices
Don't use CM_xxx API's, they bypass class/co-installers and this is bad.
In Enable case, we try global first, and if still disabled, enable local
Arguments:
Devs )_ uniquely identify the device
DevInfo )
Index - index of device
Context - GenericContext
Return Value:
EXIT_xxxx
--*/
{
SP_PROPCHANGE_PARAMS pcp;
GenericContext *pControlContext = (GenericContext*)Context;
SP_DEVINSTALL_PARAMS devParams;
switch(pControlContext->control) {
case DICS_ENABLE:
//
// enable both on global and config-specific profile
// do global first and see if that succeeded in enabling the device
// (global enable doesn't mark reboot required if device is still
// disabled on current config whereas vice-versa isn't true)
//
pcp.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
pcp.StateChange = pControlContext->control;
pcp.Scope = DICS_FLAG_GLOBAL;
pcp.HwProfile = 0;
//
// don't worry if this fails, we'll get an error when we try config-
// specific.
if(SetupDiSetClassInstallParams(Devs,DevInfo,&pcp.ClassInstallHeader,sizeof(pcp))) {
SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,Devs,DevInfo);
}
//
// now enable on config-specific
//
pcp.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
pcp.StateChange = pControlContext->control;
pcp.Scope = DICS_FLAG_CONFIGSPECIFIC;
pcp.HwProfile = 0;
break;
default:
//
// operate on config-specific profile
//
pcp.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
pcp.StateChange = pControlContext->control;
pcp.Scope = DICS_FLAG_CONFIGSPECIFIC;
pcp.HwProfile = 0;
break;
}
if(!SetupDiSetClassInstallParams(Devs,DevInfo,&pcp.ClassInstallHeader,sizeof(pcp)) ||
!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,Devs,DevInfo)) {
//
// failed to invoke DIF_PROPERTYCHANGE
//
DumpDeviceWithInfo(Devs,DevInfo,pControlContext->strFail);
} else {
//
// see if device needs reboot
//
devParams.cbSize = sizeof(devParams);
if(SetupDiGetDeviceInstallParams(Devs,DevInfo,&devParams) && (devParams.Flags & (DI_NEEDRESTART|DI_NEEDREBOOT))) {
DumpDeviceWithInfo(Devs,DevInfo,pControlContext->strReboot);
pControlContext->reboot = TRUE;
} else {
//
// appears to have succeeded
//
DumpDeviceWithInfo(Devs,DevInfo,pControlContext->strSuccess);
}
pControlContext->count++;
}
return EXIT_OK;
}
int cmdEnable(LPCTSTR BaseName,LPCTSTR Machine,int argc,TCHAR* argv[])
/*++
Routine Description:
ENABLE <id> ...
use EnumerateDevices to do hardwareID matching
for each match, attempt to enable global, and if needed, config specific
Arguments:
BaseName - name of executable
Machine - must be NULL (local machine only)
argc/argv - remaining parameters - passed into EnumerateDevices
Return Value:
EXIT_xxxx (EXIT_REBOOT if reboot is required)
--*/
{
GenericContext context;
TCHAR strEnable[80];
TCHAR strReboot[80];
TCHAR strFail[80];
int failcode = EXIT_FAIL;
if(!argc) {
//
// arguments required
//
return EXIT_USAGE;
}
if(Machine) {
//
// must be local machine as we need to involve class/co installers
//
return EXIT_USAGE;
}
if(!LoadString(NULL,IDS_ENABLED,strEnable,ARRAYSIZE(strEnable))) {
return EXIT_FAIL;
}
if(!LoadString(NULL,IDS_ENABLED_REBOOT,strReboot,ARRAYSIZE(strReboot))) {
return EXIT_FAIL;
}
if(!LoadString(NULL,IDS_ENABLE_FAILED,strFail,ARRAYSIZE(strFail))) {
return EXIT_FAIL;
}
context.control = DICS_ENABLE; // DICS_PROPCHANGE DICS_ENABLE DICS_DISABLE
context.reboot = FALSE;
context.count = 0;
context.strReboot = strReboot;
context.strSuccess = strEnable;
context.strFail = strFail;
failcode = EnumerateDevices(BaseName,Machine,DIGCF_PRESENT,argc,argv,ControlCallback,&context);
if(failcode == EXIT_OK) {
if(!context.count) {
FormatToStream(stdout,MSG_ENABLE_TAIL_NONE);
} else if(!context.reboot) {
FormatToStream(stdout,MSG_ENABLE_TAIL,context.count);
} else {
FormatToStream(stdout,MSG_ENABLE_TAIL_REBOOT,context.count);
failcode = EXIT_REBOOT;
}
}
return failcode;
}
int cmdDisable(LPCTSTR BaseName,LPCTSTR Machine,int argc,TCHAR* argv[])
/*++
Routine Description:
DISABLE <id> ...
use EnumerateDevices to do hardwareID matching
for each match, attempt to disable global
Arguments:
BaseName - name of executable
Machine - must be NULL (local machine only)
argc/argv - remaining parameters - passed into EnumerateDevices
Return Value:
EXIT_xxxx (EXIT_REBOOT if reboot is required)
--*/
{
GenericContext context;
TCHAR strDisable[80];
TCHAR strReboot[80];
TCHAR strFail[80];
int failcode = EXIT_FAIL;
if(!argc) {
//
// arguments required
//
return EXIT_USAGE;
}
if(Machine) {
//
// must be local machine as we need to involve class/co installers
//
return EXIT_USAGE;
}
if(!LoadString(NULL,IDS_DISABLED,strDisable,ARRAYSIZE(strDisable))) {
return EXIT_FAIL;
}
if(!LoadString(NULL,IDS_DISABLED_REBOOT,strReboot,ARRAYSIZE(strReboot))) {
return EXIT_FAIL;
}
if(!LoadString(NULL,IDS_DISABLE_FAILED,strFail,ARRAYSIZE(strFail))) {
return EXIT_FAIL;
}
context.control = DICS_DISABLE; // DICS_PROPCHANGE DICS_ENABLE DICS_DISABLE
context.reboot = FALSE;
context.count = 0;
context.strReboot = strReboot;
context.strSuccess = strDisable;
context.strFail = strFail;
failcode = EnumerateDevices(BaseName,Machine,DIGCF_PRESENT,argc,argv,ControlCallback,&context);
if(failcode == EXIT_OK) {
if(!context.count) {
FormatToStream(stdout,MSG_DISABLE_TAIL_NONE);
} else if(!context.reboot) {
FormatToStream(stdout,MSG_DISABLE_TAIL,context.count);
} else {
FormatToStream(stdout,MSG_DISABLE_TAIL_REBOOT,context.count);
failcode = EXIT_REBOOT;
}
}
return failcode;
}
int cmdRestart(LPCTSTR BaseName,LPCTSTR Machine,int argc,TCHAR* argv[])
/*++
Routine Description:
RESTART <id> ...
use EnumerateDevices to do hardwareID matching
for each match, attempt to restart by issueing a PROPCHANGE
Arguments:
BaseName - name of executable
Machine - must be NULL (local machine only)
argc/argv - remaining parameters - passed into EnumerateDevices
Return Value:
EXIT_xxxx (EXIT_REBOOT if reboot is required)
--*/
{
GenericContext context;
TCHAR strRestarted[80];
TCHAR strReboot[80];
TCHAR strFail[80];
int failcode = EXIT_FAIL;
if(!argc) {
//
// arguments required
//
return EXIT_USAGE;
}
if(Machine) {
//
// must be local machine as we need to involve class/co installers
//
return EXIT_USAGE;
}
if(!LoadString(NULL,IDS_RESTARTED,strRestarted,ARRAYSIZE(strRestarted))) {
return EXIT_FAIL;
}
if(!LoadString(NULL,IDS_REQUIRES_REBOOT,strReboot,ARRAYSIZE(strReboot))) {
return EXIT_FAIL;
}
if(!LoadString(NULL,IDS_RESTART_FAILED,strFail,ARRAYSIZE(strFail))) {
return EXIT_FAIL;
}
context.control = DICS_PROPCHANGE;
context.reboot = FALSE;
context.count = 0;
context.strReboot = strReboot;
context.strSuccess = strRestarted;
context.strFail = strFail;
failcode = EnumerateDevices(BaseName,Machine,DIGCF_PRESENT,argc,argv,ControlCallback,&context);
if(failcode == EXIT_OK) {
if(!context.count) {
FormatToStream(stdout,MSG_RESTART_TAIL_NONE);
} else if(!context.reboot) {
FormatToStream(stdout,MSG_RESTART_TAIL,context.count);
} else {
FormatToStream(stdout,MSG_RESTART_TAIL_REBOOT,context.count);
failcode = EXIT_REBOOT;
}
}
return failcode;
}
int cmdReboot(LPCTSTR BaseName,LPCTSTR Machine,int argc,TCHAR* argv[])
/*++
Routine Description:
REBOOT
reboot local machine
Arguments:
BaseName - name of executable
Machine - must be NULL (local machine only)
argc/argv - remaining parameters - ignored
Return Value:
EXIT_xxxx
--*/
{
if(Machine) {
//
// must be local machine
//
return EXIT_USAGE;
}
FormatToStream(stdout,MSG_REBOOT);
return Reboot() ? EXIT_OK : EXIT_FAIL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -