驱动安装和打包

最近一直在做驱动文件结构研究和安装包的制作,经过一段时间的资料收集和学习,目前已经基本清楚了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

这个安装程序,会在安装的时候执行我们写好的驱动安装程序,卸载的时候运行我们的驱动卸载程序。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
DPInst.exe: installs and uninstalls driver packages. DPInst.exe: 安装及删除驱动程序包。 By default, the tools searches the current directory and tries to install all driver packages found. 默认情况下,此工具会搜索当前目录并尝试安装找到的驱动程序包。 Usage: DPInst.exe [/U INF-file][/S | /Q][/LM][/P][/F][/SH][/SA][/A][/PATH Path][/EL][/L LanguageID][/C][/D][/LogTitle Title][/SW][/? | /h | /help] 用法:DPInst.exe [/U INF-file][/S | /Q][/LM][/P][/F][/SH][/SA][/A][/PATH Path][/EL][/L LanguageID][/C][/D][/LogTitle Title][/SW][/? | /h | /help] /U path to INF file Uninstall a driver package (INF-file). /U 指定 INF 文件 删除一个驱动程序包(INF-文件)。 /S | /Q Silent (Quiet) mode. Suppresses the Device Installation Wizard and any dialogs popped-up by the operating system. /LM Legacy mode. Accepts unsigned driver packages and packages with missing files. These packages won't install on the latest version of Windows. /LM 传统模式。接受未签名的驱动包(缺少.cat文件)以及包中有文件丢失。 /P Prompt if the driver package to be installed is not better than the current one. /P 如果驱动包已经安装以及比当前使用的版本要旧就发出提示。 /F Force install inf the driver package is not better than the current one. /F 强制安装比当前使用的版本要旧的驱动包。 /SH Scans hardware for matching devices and only copies and installs those drivers for which a device is present. Only valid for Plug and Play drivers. /SH 仅仅对已插入的匹配的硬件设备进行扫描。 /SA Suppress the Add/Remove Programs entry normally created for each driver package. /SA 禁止“添加或删除程序”创建卸载项。 /A Install all or none. /PATH Path Search for driver packages under the given path. /PATH 路径 在指定的路径下搜索驱动程序包。 /EL Enables all languages not explicitly listed in the XML file. /L LanguageID Tries to use the given language in all UI. Useful for localization tests. /SE Suppress the EULA. /SE 禁止最终用户许可协议。 /C Dump logging output to attached Console (Windows XP and above). /C 转存日志文件输出到附加控

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值