安裝和卸載驅動,使用的是Windows的devcon.exe,具體安裝和卸載的原理可以參考微軟官方的MSDN文檔。
以下兩條devcon命令的說明轉載自微軟的MSDN:
1.devcon install: https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon-install
2.devcon remove: https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon-remove
接下來我說一下在實際使用中如何使用這兩個命令,以balloon.sys爲例,首先找到balloon.inf,以記事本的方式打開,這裏我們可以看到我們所需要的設備ID
記錄下這個id,我們可以在cmd中先嚐試一下
之後發現在註冊表的計算機\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services目錄下對應會建立一個註冊表,這個註冊表項在卸載的時候是要刪除的,同時在C:\Windows\System32\drivers還會有一個對應的sys文件也應該在卸載時刪除
基本上原理這塊我們都理清了之後就可以開始寫InstallScript代碼了,這裏貼出兩個主要函數的代碼:
//////////////////////////////////////////////////////////////////////////////////
//
// Function: InstallVirtioCloudDriver
//
// Purpose: Install the virtio cloud driver
//
//////////////////////////////////////////////////////////////////////////////////
function InstallVirtioCloudDriver(szInfFile, szDriver)
string szProgram, szCmdLine;
number nvResult;
begin
if Is(FILE_EXISTS,TARGETDIR^szInfFile) = TRUE then
szProgram = "\"" + TARGETDIR^"devcon.exe\"";
szCmdLine = "install" + " " + "\"" + TARGETDIR^szInfFile +"\"" + " " + "\"" + szDriver + "\"";
nvResult = LaunchAppAndWait(szProgram, szCmdLine, nOptions);
if nvResult != 0 then
MessageBox(@MSG_INSTALLDEV_ABORT, SEVERE);
UninstallAllDrivers();
abort;
endif;
endif;
end;
///////////////////////////////////////////////////////////////////////////////////
//
// Function: UninstallVirtioCloudDriver
//
// Purpose: Uninstall the virtio cloud driver
//
//////////////////////////////////////////////////////////////////////////////////
function UninstallVirtioCloudDriver(szInfFile, szDriver, szRegKey, szDriverName)
string szProgram, szCmdLine, szKey;
begin
szProgram = "\"" + TARGETDIR^"devcon.exe\"";
szCmdLine = "remove" + " " + "\"" + TARGETDIR^szInfFile + "\"" + " " + "\"" + szDriver + "\"";
LaunchAppAndWait(szProgram, szCmdLine, nOptions);
szKey = SERVICE_REG_KEY + szRegKey;
RegDBDeleteKey(szKey);
if Is(FILE_EXISTS,WINSYSDIR^"drivers"^szDriverName) = TRUE then
DeleteFile(WINSYSDIR^"drivers"^szDriverName);
endif;
if Is(FILE_EXISTS,WINSYSDIR64^"drivers"^szDriverName) = TRUE then
DeleteFile(WINSYSDIR64^"drivers"^szDriverName);
endif;
if Is(FILE_EXISTS,WINDIR^"System32"^"drivers"^szDriverName) = TRUE then
DeleteFile(WINDIR^"System32"^"drivers"^szDriverName);
endif;
if Is(FILE_EXISTS,WINDIR^"system32"^"drivers"^szDriverName) = TRUE then
DeleteFile(WINDIR^"system32"^"drivers"^szDriverName);
endif;
end;