DevCon install命令实现
2022-02-09
70
0
先创建设备,然后再执行的是devcon update命令,即调用cmdUpdate函数
int cmdInstall(_In_ LPCTSTR BaseName, _In_opt_ LPCTSTR Machine, _In_ DWORD Flags, _In_ int argc, _In_reads_(argc) PTSTR argv[])
/*++
Routine Description:
CREATE
Creates a root enumerated devnode and installs drivers on it
Arguments:
BaseName - name of executable
Machine - machine name, must be NULL
argc/argv - remaining parameters
Return Value:
EXIT_xxxx
--*/
{
HDEVINFO DeviceInfoSet = INVALID_HANDLE_VALUE;
SP_DEVINFO_DATA DeviceInfoData;
GUID ClassGUID;
TCHAR ClassName[MAX_CLASS_NAME_LEN];
TCHAR hwIdList[LINE_LEN+4];
TCHAR InfPath[MAX_PATH];
int failcode = EXIT_FAIL;
LPCTSTR hwid = NULL;
LPCTSTR inf = NULL;
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
//
if(GetFullPathName(inf,MAX_PATH,InfPath,NULL) >= MAX_PATH) {
//
// inf pathname too long
//
return EXIT_FAIL;
}
//
// List of hardware ID's must be double zero-terminated
//
ZeroMemory(hwIdList,sizeof(hwIdList));
if (FAILED(StringCchCopy(hwIdList,LINE_LEN,hwid))) {
goto final;
}
//
// Use the INF File to extract the Class GUID.
//
if (!SetupDiGetINFClass(InfPath,&ClassGUID,ClassName,sizeof(ClassName)/sizeof(ClassName[0]),0))
{
goto final;
}
//
// Create the container for the to-be-created Device Information Element.
//
DeviceInfoSet = SetupDiCreateDeviceInfoList(&ClassGUID,0);
if(DeviceInfoSet == INVALID_HANDLE_VALUE)
{
goto final;
}
//
// Now create the element.
// Use the Class GUID and Name from the INF file.
//
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
if (!SetupDiCreateDeviceInfo(DeviceInfoSet,
ClassName,
&ClassGUID,
NULL,
0,
DICD_GENERATE_ID,
&DeviceInfoData))
{
goto final;
}
//
// Add the HardwareID to the Device's HardwareID property.
//
if(!SetupDiSetDeviceRegistryProperty(DeviceInfoSet,
&DeviceInfoData,
SPDRP_HARDWAREID,
(LPBYTE)hwIdList,
((DWORD)_tcslen(hwIdList)+1+1)*sizeof(TCHAR)))
{
goto final;
}
//
// Transform the registry element into an actual devnode
// in the PnP HW tree.
//
if (!SetupDiCallClassInstaller(DIF_REGISTERDEVICE,
DeviceInfoSet,
&DeviceInfoData))
{
goto final;
}
FormatToStream(stdout,MSG_INSTALL_UPDATE);
//
// update the driver for the device we just created
//
failcode = cmdUpdate(BaseName,Machine,Flags,argc,argv);
final:
if (DeviceInfoSet != INVALID_HANDLE_VALUE) {
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
}
return failcode;
}