📄 devutils.cpp
字号:
if (rebootRequired) {
int res = ShowMsg(MB_CANCELTRYCONTINUE,
"Can't disable device %s %s %s.\n"
"Close application that use this device and Try Again.\n"
"Or Continue and then reboot system.\n",
pDevProperties->pLocation,
pDevProperties->pDevId,
pDevProperties->pPhObjName);
if (res != IDCONTINUE) {
if (!ChangeState(hDevInfo, pDevInfoData, DICS_ENABLE))
return IDCANCEL;
Trace("Enabled %s %s %s\n",
pDevProperties->pLocation,
pDevProperties->pDevId,
pDevProperties->pPhObjName);
return res;
}
*pRebootRequired = TRUE;
}
}
return IDCONTINUE;
}
///////////////////////////////////////////////////////////////
static int DisableDevice(HDEVINFO hDevInfo, PSP_DEVINFO_DATA pDevInfoData, PDevParams pDevParams)
{
int res = DisableDevice(hDevInfo, pDevInfoData, &pDevParams->devProperties, pDevParams->pEnumParams->pRebootRequired);
if (res != IDCONTINUE)
pDevParams->pEnumParams->count++;
return res;
}
BOOL DisableDevices(
InfFile &infFile,
PCDevProperties pDevProperties,
BOOL *pRebootRequired)
{
EnumParams enumParams;
int res;
enumParams.pRebootRequired = pRebootRequired;
if (pDevProperties)
enumParams.devProperties = *pDevProperties;
do {
res = EnumDevices(infFile, DIGCF_PRESENT, DisableDevice, &enumParams);
} while (res == IDTRYAGAIN);
if (res != IDCONTINUE)
return FALSE;
Sleep(1000);
return TRUE;
}
///////////////////////////////////////////////////////////////
static int RestartDevice(HDEVINFO hDevInfo, PSP_DEVINFO_DATA pDevInfoData, PDevParams pDevParams)
{
if (!ChangeState(hDevInfo, pDevInfoData, DICS_PROPCHANGE))
return IDCANCEL;
if (pDevParams->pEnumParams->pRebootRequired && !*pDevParams->pEnumParams->pRebootRequired) {
BOOL rebootRequired = FALSE;
if (!UpdateRebootRequired(hDevInfo, pDevInfoData, &rebootRequired))
return IDCANCEL;
if (rebootRequired) {
int res = ShowMsg(MB_CANCELTRYCONTINUE,
"Can't reastart device %s %s %s.\n"
"Close application that use this device and Try Again.\n"
"Or Continue and then reboot system.\n",
pDevParams->devProperties.pLocation,
pDevParams->devProperties.pDevId,
pDevParams->devProperties.pPhObjName);
if (res != IDCONTINUE) {
if (!ChangeState(hDevInfo, pDevInfoData, DICS_ENABLE))
return IDCANCEL;
return res;
}
*pDevParams->pEnumParams->pRebootRequired = TRUE;
}
}
if (!pDevParams->pEnumParams->pRebootRequired || !*pDevParams->pEnumParams->pRebootRequired) {
Trace("Restarted %s %s %s\n",
pDevParams->devProperties.pLocation,
pDevParams->devProperties.pDevId,
pDevParams->devProperties.pPhObjName);
pDevParams->pEnumParams->count++;
}
return IDCONTINUE;
}
BOOL RestartDevices(
InfFile &infFile,
PCDevProperties pDevProperties,
BOOL *pRebootRequired)
{
EnumParams enumParams;
int res;
enumParams.pRebootRequired = pRebootRequired;
if (pDevProperties)
enumParams.devProperties = *pDevProperties;
do {
res = EnumDevices(infFile, DIGCF_PRESENT, RestartDevice, &enumParams);
} while (res == IDTRYAGAIN);
if (res != IDCONTINUE)
return FALSE;
return TRUE;
}
///////////////////////////////////////////////////////////////
BOOL RemoveDevice(
HDEVINFO hDevInfo,
PSP_DEVINFO_DATA pDevInfoData,
PCDevProperties pDevProperties,
BOOL *pRebootRequired)
{
if (!SetupDiCallClassInstaller(DIF_REMOVE, hDevInfo, pDevInfoData)) {
ShowLastError(MB_OK|MB_ICONSTOP, "SetupDiCallClassInstaller(DIF_REMOVE, %s, %s)",
pDevProperties->pDevId, pDevProperties->pPhObjName);
return FALSE;
}
Trace("Removed %s %s %s\n", pDevProperties->pLocation, pDevProperties->pDevId, pDevProperties->pPhObjName);
return UpdateRebootRequired(hDevInfo, pDevInfoData, pRebootRequired);
}
///////////////////////////////////////////////////////////////
static int RemoveDevice(HDEVINFO hDevInfo, PSP_DEVINFO_DATA pDevInfoData, PDevParams pDevParams)
{
if (!RemoveDevice(hDevInfo, pDevInfoData, &pDevParams->devProperties, pDevParams->pEnumParams->pRebootRequired))
return IDCANCEL;
pDevParams->pEnumParams->count++;
return IDCONTINUE;
}
BOOL RemoveDevices(
InfFile &infFile,
PCDevProperties pDevProperties,
BOOL *pRebootRequired)
{
EnumParams enumParams;
int res;
enumParams.pRebootRequired = pRebootRequired;
if (pDevProperties)
enumParams.devProperties = *pDevProperties;
do {
res = EnumDevices(infFile, 0, RemoveDevice, &enumParams);
} while (res == IDTRYAGAIN);
if (res != IDCONTINUE)
return FALSE;
if (!enumParams.count)
Trace("No devices found\n");
return TRUE;
}
///////////////////////////////////////////////////////////////
BOOL InstallDevice(
InfFile &infFile,
const char *pDevId,
PDEVCALLBACK pDevCallBack,
void *pCallBackParam)
{
GUID classGUID;
char className[32];
if (!SetupDiGetINFClass(infFile.Path(), &classGUID, className, sizeof(className), 0)) {
ShowLastError(MB_OK|MB_ICONSTOP, "SetupDiGetINFClass(%s)", infFile.Path());
return FALSE;
}
//Trace("className=%s\n", className);
HDEVINFO hDevInfo;
hDevInfo = SetupDiCreateDeviceInfoList(&classGUID, 0);
if (hDevInfo == INVALID_HANDLE_VALUE) {
ShowLastError(MB_OK|MB_ICONSTOP, "SetupDiCreateDeviceInfoList()");
return FALSE;
}
BOOL res;
SP_DEVINFO_DATA devInfoData;
devInfoData.cbSize = sizeof(devInfoData);
res = SetupDiCreateDeviceInfo(hDevInfo, className, &classGUID, NULL, 0,
DICD_GENERATE_ID, &devInfoData);
if (!res) {
ShowLastError(MB_OK|MB_ICONSTOP, "SetupDiCreateDeviceInfo()");
goto err;
}
char hardwareId[MAX_DEVICE_ID_LEN + 1 + 1];
memset(hardwareId, 0, sizeof(hardwareId));
lstrcpyn(hardwareId, pDevId, sizeof(hardwareId) - 1 - 1);
int hardwareIdSize;
hardwareIdSize = (lstrlen(hardwareId) + 1 + 1) * sizeof(hardwareId[0]);
res = SetupDiSetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_HARDWAREID,
(LPBYTE)hardwareId, hardwareIdSize);
if (!res) {
ShowLastError(MB_OK|MB_ICONSTOP, "SetupDiSetDeviceRegistryProperty()");
goto err;
}
res = SetupDiCallClassInstaller(DIF_REGISTERDEVICE, hDevInfo, &devInfoData);
if (!res) {
ShowLastError(MB_OK|MB_ICONSTOP, "SetupDiCallClassInstaller()");
goto err;
}
if (pDevCallBack) {
DevProperties devProperties;
devProperties.pDevId = pDevId;
res = pDevCallBack(hDevInfo, &devInfoData, &devProperties, NULL, pCallBackParam);
if (!res)
goto err1;
}
BOOL rebootRequired;
res = UpdateDriverForPlugAndPlayDevices(0, pDevId, infFile.Path(), INSTALLFLAG_FORCE, &rebootRequired);
if (!res) {
ShowLastError(MB_OK|MB_ICONSTOP, "UpdateDriverForPlugAndPlayDevices()");
err1:
if (!SetupDiCallClassInstaller(DIF_REMOVE, hDevInfo, &devInfoData))
ShowLastError(MB_OK|MB_ICONWARNING, "SetupDiCallClassInstaller()");
}
err:
SetupDiDestroyDeviceInfoList(hDevInfo);
return res;
}
///////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -