BOOL MonitorNDASKeyChange(HANDLE hKeyChangeEvent, HANDLE hStopEvent) { LONG lResult; BOOL fSuccess; HKEY hNDASKey; lResult = ::RegOpenKeyEx( HKEY_LOCAL_MACHINE, REGSTR_PATH_SYSTEMENUM _T("\\NDAS"), 0, KEY_NOTIFY | KEY_READ | KEY_ENUMERATE_SUB_KEYS, &hNDASKey); if (ERROR_SUCCESS != lResult) { // maybe Enum\NDAS is not created yet! // wait for ENUM to be changed! return MonitorEnumKeyChange(hKeyChangeEvent, hStopEvent); } ::ResetEvent(hKeyChangeEvent); lResult = ::RegNotifyChangeKeyValue( hNDASKey, TRUE, // watch for sub-tree REG_NOTIFY_CHANGE_LAST_SET | REG_NOTIFY_CHANGE_NAME, hKeyChangeEvent, TRUE); if (ERROR_SUCCESS != lResult) { DebugPrintErr(lResult, _T("RegNotifyChangeKeyValue(%s) failed"), REGSTR_PATH_SYSTEMENUM _T("\\NDAS")); ::RegCloseKey(hNDASKey); return FALSE; } HANDLE hWaitingEvents[] = { hStopEvent, hKeyChangeEvent }; DWORD dwWaitResult = ::WaitForMultipleObjects( RTL_NUMBER_OF(hWaitingEvents), hWaitingEvents, FALSE, INFINITE); if (WAIT_OBJECT_0 != dwWaitResult && WAIT_OBJECT_0 + 1 != dwWaitResult) { DebugPrintLastErr(_T("Wait failed!")); ::RegCloseKey(hNDASKey); return FALSE; } DebugPrint(1, _T("Changes in %s notified.\n"), REGSTR_PATH_SYSTEMENUM _T("\\NDAS")); ::RegCloseKey(hNDASKey); return TRUE; }
BOOL __stdcall NdasDiInstallDeviceDriver( CONST GUID* classGUID, LPCTSTR devInstID, LPCTSTR szDriverPath) { HDEVINFO hDevInfoSet = ::SetupDiCreateDeviceInfoList(classGUID, NULL); if (INVALID_HANDLE_VALUE == hDevInfoSet) { DebugPrintLastErr(_T("SetupDiGetClassDevs failed: ")); return FALSE; } BOOL fSuccess = pInstallDeviceDriver(hDevInfoSet, devInstID, szDriverPath); (VOID) ::SetupDiDestroyDeviceInfoList(hDevInfoSet); return fSuccess; }
BOOL pInstallDeviceDriver( HDEVINFO hDevInfoSet, LPCTSTR devInstID, LPCTSTR szDriverPath) { SP_DEVINFO_DATA devInfoData = {0}; devInfoData.cbSize = sizeof(SP_DEVINFO_DATA); BOOL fSuccess = ::SetupDiOpenDeviceInfo( hDevInfoSet, devInstID, NULL, 0, &devInfoData); if (!fSuccess) { DebugPrintLastErr(_T("SetupDiOpenDeviceInfo failed: ")); return FALSE; } SP_DRVINFO_DATA drvInfoData = {0}; drvInfoData.cbSize = sizeof(drvInfoData); SP_DEVINSTALL_PARAMS devInstParams = {0}; devInstParams.cbSize = sizeof(devInstParams); fSuccess = ::SetupDiGetDeviceInstallParams( hDevInfoSet, &devInfoData, &devInstParams); if (!fSuccess) { DebugPrintLastErr(_T("SetupDiGetDeviceInstallParams failed: ")); return FALSE; } devInstParams.Flags |= ( DI_ENUMSINGLEINF | DI_QUIETINSTALL ); ::lstrcpy(devInstParams.DriverPath, szDriverPath); fSuccess = ::SetupDiSetDeviceInstallParams( hDevInfoSet, &devInfoData, &devInstParams); if (!fSuccess) { DebugPrintLastErr(_T("SetupDiSetDeviceInstallParams failed: ")); return FALSE; } fSuccess = ::SetupDiBuildDriverInfoList( hDevInfoSet, &devInfoData, SPDIT_COMPATDRIVER); if (!fSuccess) { DebugPrintLastErr(_T("SetupDiBuildDriverInfoList failed: ")); return FALSE; } fSuccess = ::SetupDiCallClassInstaller( DIF_SELECTBESTCOMPATDRV, hDevInfoSet, &devInfoData); if (!fSuccess) { DebugPrintLastErr(_T("SetupDiCallClassInstaller DIF_SELECTBESTCOMPATDRV failed: ")); return FALSE; } fSuccess = ::SetupDiCallClassInstaller( DIF_INSTALLDEVICE, hDevInfoSet, &devInfoData); if (!fSuccess) { DebugPrintLastErr(_T("SetupDiCallClassInstaller DIF_INSTALLDEVICE failed: ")); return FALSE; } DebugPrint(1, _T("pInstallDeviceDriver done successfully.\n")); return TRUE; }