天天看点

使用InstallShield制作Windows阿里云Virtio驱动安装包(三)-- 安装和卸载驱动

    安装和卸载驱动,使用的是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

使用InstallShield制作Windows阿里云Virtio驱动安装包(三)-- 安装和卸载驱动

记录下这个id,我们可以在cmd中先尝试一下

之后发现在注册表的计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services目录下对应会建立一个注册表,这个注册表项在卸载的时候是要删除的,同时在C:\Windows\System32\drivers还会有一个对应的sys文件也应该在卸载时删除

使用InstallShield制作Windows阿里云Virtio驱动安装包(三)-- 安装和卸载驱动

    基本上原理这块我们都理清了之后就可以开始写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;
           

继续阅读