📄 cmds.cpp
字号:
SP_DEVINSTALL_PARAMS devParams;
ULONG status;
ULONG problem;
LPTSTR * hwlist = NULL;
bool modified = false;
int result = EXIT_FAIL;
//
// processes the sub-commands on each callback
// not most efficient way of doing things, but perf isn't important
//
TCHAR devID[MAX_DEVICE_ID_LEN];
BOOL b = TRUE;
SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
devInfoListDetail.cbSize = sizeof(devInfoListDetail);
if((!SetupDiGetDeviceInfoListDetail(Devs,&devInfoListDetail)) ||
(CM_Get_Device_ID_Ex(DevInfo->DevInst,devID,MAX_DEVICE_ID_LEN,0,devInfoListDetail.RemoteMachineHandle)!=CR_SUCCESS) ||
(CM_Get_DevNode_Status_Ex(&status,&problem,DevInfo->DevInst,0,devInfoListDetail.RemoteMachineHandle)!=CR_SUCCESS)) {
//
// skip this
//
return EXIT_OK;
}
//
// this is how to verify it's root enumerated
//
if(!(status & DN_ROOT_ENUMERATED)) {
_tprintf(TEXT("%-60s: "),devID);
FormatToStream(stdout,MSG_SETHWID_NOTROOT);
pControlContext->skipped++;
return EXIT_OK;
}
hwlist = GetDevMultiSz(Devs,DevInfo,pControlContext->prop);
if(hwlist == NULL) {
hwlist = CopyMultiSz(NULL);
if(hwlist == NULL) {
return EXIT_FAIL;
}
}
//
// modify hwid list (only relevent for root-enumerated devices)
//
int i;
int mark = -1;
for(i=0;i<pControlContext->argc_right;i++) {
LPTSTR op = pControlContext->argv_right[i];
if(op[0] == TEXT('=')) {
//
// clear the hwid list first
//
hwlist[0] = NULL;
mark = 0;
op++;
} else if(op[0] == TEXT('+')) {
//
// insert as better match
//
mark = 0;
op++;
} else if(op[0] == TEXT('-')) {
//
// insert as worse match
//
mark = -1;
op++;
} else if(op[0] == TEXT('!')) {
//
// delete
//
mark = -2;
op++;
} else {
//
// treat as a hardware id
//
}
if(!*op) {
result = EXIT_USAGE;
goto final;
}
int cnt;
for(cnt = 0;hwlist[cnt];cnt++) {
// nothing
}
if((mark == -1) || (mark>cnt)) {
mark = cnt;
}
LPTSTR * tmpArray = new LPTSTR[cnt+2];
if(!tmpArray) {
goto final;
}
int dst = 0;
int ent;
for(ent=0;ent<mark;ent++) {
if(_tcsicmp(hwlist[ent],op)==0) {
continue;
}
tmpArray[dst++] = hwlist[ent];
}
if(mark>=0) {
tmpArray[dst++] = op;
}
for(;ent<cnt;ent++) {
if(_tcsicmp(hwlist[ent],op)==0) {
continue;
}
tmpArray[dst++] = hwlist[ent];
}
tmpArray[dst] = NULL;
LPTSTR * newArray = CopyMultiSz(tmpArray);
delete [] tmpArray;
if(!newArray) {
goto final;
}
DelMultiSz(hwlist);
hwlist = newArray;
modified = true;
mark++;
}
//
// re-set the hwid list
//
if(modified) {
if(hwlist[0]) {
int len = 0;
LPTSTR multiSz = hwlist[-1];
LPTSTR p = multiSz;
while(*p) {
p+=lstrlen(p)+1;
}
p++; // skip past final null
len = (p-multiSz)*sizeof(TCHAR);
if(!SetupDiSetDeviceRegistryProperty(Devs,
DevInfo,
pControlContext->prop,
(LPBYTE)multiSz,
len)) {
result = EXIT_FAIL;
goto final;
}
} else {
//
// delete list
//
if(!SetupDiSetDeviceRegistryProperty(Devs,
DevInfo,
pControlContext->prop,
NULL,
0)) {
result = EXIT_FAIL;
goto final;
}
}
}
result = EXIT_OK;
pControlContext->modified++;
_tprintf(TEXT("%-60s: "),devID);
for(mark=0;hwlist[mark];mark++) {
if(mark > 0) {
_tprintf(TEXT(","));
}
_tprintf(TEXT("%s"),hwlist[mark]);
}
_tprintf(TEXT("\n"));
//
// cleanup
//
final:
if(hwlist) {
DelMultiSz(hwlist);
}
return result;
}
int cmdSetHwid(__in LPCTSTR BaseName, __in LPCTSTR Machine, __in DWORD Flags, __in int argc, __in_ecount(argc) TCHAR* argv[])
/*++
Routine Description:
SETHWID
changes the hardware ID's of the listed root-enumerated devices
This demonstrates how to differentiate between root-enumerated and
non root-enumerated devices.
It also demonstrates how to get/set hardware ID's of root-enumerated
devices.
Arguments:
BaseName - name of executable
Machine - machine name, must be NULL
argc/argv - remaining parameters
Return Value:
EXIT_xxxx
--*/
{
SetHwidContext context;
int failcode = EXIT_FAIL;
if(!SplitCommandLine(argc,argv,context.argc_right,context.argv_right)
|| (argc == 0)
|| (context.argc_right == 0)) {
//
// arguments required both left and right of ':='
//
return EXIT_USAGE;
}
context.skipped = 0;
context.modified = 0;
context.prop = SPDRP_HARDWAREID;
failcode = EnumerateDevices(BaseName,Machine,DIGCF_PRESENT,argc,argv,SetHwidCallback,&context);
if(failcode == EXIT_OK) {
if(context.skipped) {
FormatToStream(stdout,MSG_SETHWID_TAIL_SKIPPED,context.skipped,context.modified);
} else if(context.modified) {
FormatToStream(stdout,MSG_SETHWID_TAIL_MODIFIED,context.modified);
} else {
FormatToStream(stdout,MSG_SETHWID_TAIL_NONE);
}
}
return failcode;
}
int cmdDPAdd(__in LPCTSTR BaseName, __in LPCTSTR Machine, __in DWORD Flags, __in int argc, __in_ecount(argc) TCHAR* argv[])
/*++
Routine Description:
dp_add
Add a driver package to the machine.
Arguments:
BaseName - name of executable
Machine - machine name, must be NULL
argc/argv - remaining parameters
Return Value:
EXIT_xxxx
--*/
{
int failcode = EXIT_FAIL;
DWORD res;
TCHAR SourceInfFileName[MAX_PATH];
TCHAR DestinationInfFileName[MAX_PATH];
PTSTR DestinationInfFileNameComponent = NULL;
PTSTR FilePart = NULL;
if(!argc) {
return EXIT_USAGE;
}
res = GetFullPathName(argv[0],
ARRAYSIZE(SourceInfFileName),
SourceInfFileName,
&FilePart);
if ((!res) || (res >= ARRAYSIZE(SourceInfFileName))) {
FormatToStream(stdout,MSG_DPADD_INVALID_INF);
goto final;
}
if (!SetupCopyOEMInf(SourceInfFileName,
NULL,
SPOST_PATH,
0,
DestinationInfFileName,
ARRAYSIZE(DestinationInfFileName),
NULL,
&DestinationInfFileNameComponent)) {
FormatToStream(stdout,MSG_DPADD_FAILED);
goto final;
}
//
// Successfully added the driver package to the machine.
//
FormatToStream(stdout,MSG_DPADD_SUCCESS,DestinationInfFileNameComponent);
failcode = EXIT_OK;
final:
return failcode;
}
int cmdDPDelete(__in LPCTSTR BaseName, __in LPCTSTR Machine, __in DWORD Flags, __in int argc, __in_ecount(argc) TCHAR* argv[])
/*++
Routine Description:
dp_delete
Deletes a driver package to the machine.
Arguments:
BaseName - name of executable
Machine - machine name, must be NULL
argc/argv - remaining parameters
Return Value:
EXIT_xxxx
--*/
{
int failcode = EXIT_FAIL;
DWORD res;
TCHAR InfFileName[MAX_PATH];
PTSTR FilePart = NULL;
HMODULE setupapiMod = NULL;
SetupUninstallOEMInfProto SUOIFn;
if(!argc) {
return EXIT_USAGE;
}
res = GetFullPathName(argv[0],
ARRAYSIZE(InfFileName),
InfFileName,
&FilePart);
if ((!res) || (!FilePart)) {
FormatToStream(stdout,MSG_DPADD_INVALID_INF);
goto final;
}
setupapiMod = LoadLibrary(TEXT("setupapi.dll"));
if(!setupapiMod) {
goto final;
}
SUOIFn = (SetupUninstallOEMInfProto)GetProcAddress(setupapiMod,SETUPUNINSTALLOEMINF);
if(!SUOIFn)
{
goto final;
}
if (!SUOIFn(FilePart,
((Flags & DEVCON_FLAG_FORCE) ? 1 : 0),
NULL)) {
if (GetLastError() == ERROR_INF_IN_USE_BY_DEVICES) {
FormatToStream(stdout,MSG_DPDELETE_FAILED_IN_USE);
} else if (GetLastError() == ERROR_NOT_AN_INSTALLED_OEM_INF) {
FormatToStream(stdout,MSG_DPDELETE_FAILED_NOT_OEM_INF);
} else {
FormatToStream(stdout,MSG_DPDELETE_FAILED);
}
goto final;
}
//
// Successfully added the driver package to the machine.
//
FormatToStream(stdout,MSG_DPDELETE_SUCCESS,FilePart);
failcode = EXIT_OK;
final:
if (setupapiMod) {
FreeLibrary(setupapiMod);
}
return failcode;
}
int cmdDPEnumLegacy(__in LPCTSTR BaseName, __in LPCTSTR Machine, __in DWORD Flags, __in int argc, __in_ecount(argc) TCHAR* argv[])
/*++
Routine Description:
dp_enumLegacy
Enumerates installed Driver Packages on the machine pre Windows Longhorn
Arguments:
BaseName - name of executable
Machine - machine name, must be NULL
argc/argv - remaining parameters
Return Value:
EXIT_xxxx
--*/
{
int failcode = EXIT_FAIL;
TCHAR FindName[MAX_PATH];
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA wfd;
if(argc) {
return EXIT_USAGE;
}
if (!GetWindowsDirectory(FindName, ARRAYSIZE(FindName)) ||
FAILED(StringCchCat(FindName, ARRAYSIZE(FindName), TEXT("\\INF\\OEM*.INF")))) {
goto final;
}
hFind = FindFirstFile(FindName, &wfd);
if (hFind == INVALID_HANDLE_VALUE) {
//
// No OEM driver packages on this machine.
//
FormatToStream(stdout,MSG_DPENUM_NO_OEM_INF);
failcode = EXIT_OK;
goto final;
}
FormatToStream(stdout,MSG_DPENUM_LIST_HEADER);
do {
FormatToStream(stdout,MSG_DPENUM_LIST_ENTRY,wfd.cFileName);
DumpDriverPackageData(wfd.cFileName);
} while (FindNextFile(hFind, &wfd));
FindClose(hFind);
failcode = EXIT_OK;
final:
return failcode;
}
DispatchEntry DispatchTable[] = {
{ TEXT("classfilter"), cmdClassFilter, MSG_CLASSFILTER_SHORT, MSG_CLASSFILTER_LONG },
{ TEXT("classes"), cmdClasses, MSG_CLASSES_SHORT, MSG_CLASSES_LONG },
{ TEXT("disable"), cmdDisable, MSG_DISABLE_SHORT, MSG_DISABLE_LONG },
{ TEXT("driverfiles"), cmdDriverFiles, MSG_DRIVERFILES_SHORT, MSG_DRIVERFILES_LONG },
{ TEXT("drivernodes"), cmdDriverNodes, MSG_DRIVERNODES_SHORT, MSG_DRIVERNODES_LONG },
{ TEXT("enable"), cmdEnable, MSG_ENABLE_SHORT, MSG_ENABLE_LONG },
{ TEXT("find"), cmdFind, MSG_FIND_SHORT, MSG_FIND_LONG },
{ TEXT("findall"), cmdFindAll, MSG_FINDALL_SHORT, MSG_FINDALL_LONG },
{ TEXT("help"), cmdHelp, MSG_HELP_SHORT, 0 },
{ TEXT("hwids"), cmdHwIds, MSG_HWIDS_SHORT, MSG_HWIDS_LONG },
{ TEXT("install"), cmdInstall, MSG_INSTALL_SHORT, MSG_INSTALL_LONG },
{ TEXT("listclass"), cmdListClass, MSG_LISTCLASS_SHORT, MSG_LISTCLASS_LONG },
{ TEXT("reboot"), cmdReboot, MSG_REBOOT_SHORT, MSG_REBOOT_LONG },
{ TEXT("remove"), cmdRemove, MSG_REMOVE_SHORT, MSG_REMOVE_LONG },
{ TEXT("rescan"), cmdRescan, MSG_RESCAN_SHORT, MSG_RESCAN_LONG },
{ TEXT("resources"), cmdResources, MSG_RESOURCES_SHORT, MSG_RESOURCES_LONG },
{ TEXT("restart"), cmdRestart, MSG_RESTART_SHORT, MSG_RESTART_LONG },
{ TEXT("sethwid"), cmdSetHwid, MSG_SETHWID_SHORT, MSG_SETHWID_LONG },
{ TEXT("stack"), cmdStack, MSG_STACK_SHORT, MSG_STACK_LONG },
{ TEXT("status"), cmdStatus, MSG_STATUS_SHORT, MSG_STATUS_LONG },
{ TEXT("update"), cmdUpdate, MSG_UPDATE_SHORT, MSG_UPDATE_LONG },
{ TEXT("updateni"), cmdUpdateNI, MSG_UPDATENI_SHORT, MSG_UPDATENI_LONG },
{ TEXT("dp_add"), cmdDPAdd, MSG_DPADD_SHORT, MSG_DPADD_LONG },
{ TEXT("dp_delete"), cmdDPDelete, MSG_DPDELETE_SHORT, MSG_DPDELETE_LONG },
{ TEXT("dp_enum"), cmdDPEnumLegacy,MSG_DPENUM_SHORT, MSG_DPENUM_LONG },
{ TEXT("?"), cmdHelp, 0, 0 },
{ NULL,NULL }
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -