DevCon update命令实现
2022-02-09
68
0
int cmdUpdate(_In_ LPCTSTR BaseName, _In_opt_ LPCTSTR Machine, _In_ DWORD Flags, _In_ int argc, _In_reads_(argc) PTSTR argv[])
/*++
Routine Description:
UPDATE
update driver for existing device(s)
Arguments:
BaseName - name of executable
Machine - machine name, must be NULL
argc/argv - remaining parameters
Return Value:
EXIT_xxxx
--*/
{
HMODULE newdevMod = NULL;
int failcode = EXIT_FAIL;
UpdateDriverForPlugAndPlayDevicesProto UpdateFn;
BOOL reboot = FALSE;
LPCTSTR hwid = NULL;
LPCTSTR inf = NULL;
DWORD flags = 0;
DWORD res;
TCHAR InfPath[MAX_PATH];
UNREFERENCED_PARAMETER(BaseName);
UNREFERENCED_PARAMETER(Flags);
if(Machine) {
//
// must be local machine
//
return EXIT_USAGE;
}
if(argc<2) {
//
// at least HWID required
//
return EXIT_USAGE;
}
inf = argv[0];
if(!inf[0]) {
return EXIT_USAGE;
}
hwid = argv[1];
if(!hwid[0]) {
return EXIT_USAGE;
}
//
// Inf must be a full pathname
//
res = GetFullPathName(inf,MAX_PATH,InfPath,NULL);
if((res >= MAX_PATH) || (res == 0)) {
//
// inf pathname too long
//
return EXIT_FAIL;
}
if(GetFileAttributes(InfPath)==(DWORD)(-1)) {
//
// inf doesn't exist
//
return EXIT_FAIL;
}
inf = InfPath;
flags |= INSTALLFLAG_FORCE;
//
// make use of UpdateDriverForPlugAndPlayDevices
//
newdevMod = LoadLibrary(TEXT("newdev.dll"));
if(!newdevMod) {
goto final;
}
UpdateFn = (UpdateDriverForPlugAndPlayDevicesProto)GetProcAddress(newdevMod,UPDATEDRIVERFORPLUGANDPLAYDEVICES);
if(!UpdateFn)
{
goto final;
}
FormatToStream(stdout,inf ? MSG_UPDATE_INF : MSG_UPDATE,hwid,inf);
if(!UpdateFn(NULL,hwid,inf,flags,&reboot)) {
goto final;
}
FormatToStream(stdout,MSG_UPDATE_OK);
failcode = reboot ? EXIT_REBOOT : EXIT_OK;
final:
if(newdevMod) {
FreeLibrary(newdevMod);
}
return failcode;
}