最近一直在做驅動檔案結構研究和安裝包的製作,經過一段時間的資料收集和學習,目前已經基本清楚了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
這個安裝程式,會在安裝的時候執行我們寫好的驅動安裝程式,卸載的時候運行我們的驅動卸載程式。