最近一直在做驱动文件结构研究和安装包的制作,经过一段时间的资料收集和学习,目前已经基本清楚了Inf文件的结构,也制作了驱动的安装包。由于本人文笔不好,下面直接说一下如何做驱动安装包。
win7安装没有经过签名的驱动需要先让OS进入“测试模式”:首先以管理员身份运行cmd;然后输入bcdedit/set testsigning on(有的也输入bcdedit.exe/set testsigning on);最后重启电脑
我采用的是用VS2008写安装卸载程序,用NSIS打包的方式实现的(貌似可以不用VS写程序,直接用NSIS就能搞定,不过我没有去研究)
驱动安装程序代码:DWORD ReturnCode = DriverPackageInstall(inf_path,DRIVER_PACKAGE_LEGACY_MODE | DRIVER_PACKAGE_FORCE,NULL,&bReboot);不用其它的操作,直接用这一句代码就行,其中inf_path是你要安装的驱动包的inf文件的全路径。如果ReturnCode为ERROR_IN_WOW64,则当前操作系统为64位的,需要重新指定64位驱动文件的路径。函数中的DRIVER_PACKAGE_LEGACY_MODE标志位表示函数不会去检查驱动是否签名(不检查不代表没有对应的.cat文件,这个还是要有的),如果不设置这个标识,对于没有经过签名的驱动是不能安装成功的,会返回错误码:TRUST_E_NOSIGNATURE。
这个安装程序运行成功后,如果你是将手机连接上PC安装的驱动,也就是所谓的硬件预安装,能够使得这个设备直接就可用了,不需要重新插拔设备(在设备管理器中能看到该设备安装成功,没有问号也没有感叹号);如果你的驱动没有经过签名,而安装驱动的时候你的设备也没有连接到PC(即软件预安装),那么当发现新设备的时候还是会弹出新硬件提示框。
驱动卸载程序:
char szFullPath[256];
BOOL bReboot = FALSE;
std::vector<CString>::iterator iter = vecList.begin();
for(;iter != vecList.end();++iter)
{
DWORD Flags = DRIVER_PACKAGE_FORCE;
DWORD ReturnCode = ERROR_SUCCESS;
CString strPath = strTmp + (*iter);
memset(szFullPath,0,256);
DWORD dwOutLength = 256;
if (DriverPackageGetPath(strPath.GetBuffer(),szFullPath,&dwOutLength) == ERROR_SUCCESS)
{
DriverPackageUninstall(szFullPath,Flags,NULL,&bReboot);
}
}
下面再说一下NSIS打包:直接贴一个例子在这,方便大家一起探讨学习。
!define VERSION "1.0.0.2"
; The name of the installer
Name "DriverInstall(${VERSION})"
; The file to write
OutFile "DriverInstall_${VERSION}(64bits).exe"
; The default installation directory
InstallDir $PROGRAMFILES\DriverInstall
; Registry key to check for directory (so if you install again, it will
; overwrite the old one automatically)
InstallDirRegKey HKLM "Software\DriverInstall" "Install_Dir"
; Request application privileges for Windows Vista
RequestExecutionLevel admin
;--------------------------------
; Pages
XPStyle on
Page instfiles
UninstPage uninstConfirm
UninstPage instfiles
;--------------------------------
; The stuff to install
Section "DriverInstall"
SectionIn RO
; Set output path to the installation directory.
SetOutPath $INSTDIR
File /r DriverApplications
; exec file
;ExecWait '"Install.exe" /S /D=$INSTDIR'
ExecWait '"$INSTDIR\DriverApplications\QualcommDriverInstall.exe"'
; Write the installation path into the registry
WriteRegStr HKLM SOFTWARE\DriverInstall "Install_Dir" "$INSTDIR"
; Write the uninstall keys for Windows
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\DriverInstall" "DisplayName" "DriverInstall"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\DriverInstall" "UninstallString" '"$INSTDIR\uninstall.exe"'
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\DriverInstall" "NoModify" 1
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\DriverInstall" "NoRepair" 1
WriteUninstaller "uninstall.exe"
SectionEnd
; Optional section (can be disabled by the user)
Section "Start Menu Shortcuts"
CreateDirectory "$SMPROGRAMS\DriverInstall"
CreateShortCut "$SMPROGRAMS\DriverInstall\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
SectionEnd
;--------------------------------
; Uninstaller
Section "Uninstall"
; Remove registry keys
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\DriverInstall"
DeleteRegKey HKLM SOFTWARE\DriverInstall
ExecWait '"$INSTDIR\DriverApplications\DriverUnInstall.exe"'
; Remove files and uninstaller
Delete $INSTDIR\Install.exe
Delete $INSTDIR\uninstall.exe
; Remove shortcuts, if any
Delete "$SMPROGRAMS\DriverInstall\*.*"
; Remove directories used
RMDir /r "$SMPROGRAMS\DriverInstall"
RMDir /r "$INSTDIR"
SectionEnd
这个安装程序,会在安装的时候执行我们写好的驱动安装程序,卸载的时候运行我们的驱动卸载程序。