在安装软件时,如果特定条件为假,如何使用NSIS回退软件安装过程?

时间:2018-12-09 12:48:29

标签: nsis

我的要求是在使用NSIS安装程序安装软件时,应调用该功​​能并检查UPS是否已连接到计算机。

如果已连接UPS,它将完成安装过程。

如果未连接UPS电池,则会显示一个消息框,询问“请连接UPS,然后使用“取消”和“重试”按钮重试”。如果单击“取消”,则会回滚安装过程。

我可以使用下面的代码行获取是否已连接UPS:

 System::Call "$INSTDIR\drvutil.dll::IsUPSPresent() i.r0 ?e"

然后我使用“ If”条件,并检查UPS是否已连接

 ${If} $0 = 0

如果条件为假,则需要显示消息框,例如“请连接UPS,然后使用“取消”和“重试”按钮重试”。如果我单击“ Cancle”,它将回滚安装过程。如果我连接UPS(或条件为True),则安装应完成。

请帮助我如何实现此逻辑。

下面是我的代码段:

; The stuff to install
Section "UPSTest (required)"

  SectionIn RO

  DetailPrint "Testing UPS"

  SetOutPath $INSTDIR

  ; Give the dll path
  File E:\TestNullSoft\ValidateUPS.exe
  File E:\TestNullSoft\drvutil.dll
  File E:\TestNullSoft\UpsControl.dll
  File E:\TestNullSoft\UpsDevice.dll
  File E:\TestNullSoft\pdcdll.dll

  System::Call 'KERNEL32::AddDllDirectory(w "$INSTDIR")' ; Tell Windows we trust all .DLLs in this directory

  System::Call "$INSTDIR\drvutil.dll::IsUPSPresent() i.r0 ?e"
  Pop $1
  MessageBox MB_OK "Return value = $0, lasterr = $1"
  ; In the below line If the return value is 0("false"), That means UPS is not connected to the computer
   ${If} $0 = 0
   ; Here I need to show the Messagebox like "Please connect the UPS and try again with "Cancel" and "Retry" buttons"
   ; If I click on "Cancle", It should Roll back the installation process.

  ; Write the installation path into the registry
  WriteRegStr HKLM SOFTWARE\NSIS_UPSTest "Install_Dir" "$INSTDIR"

  ; Write the uninstall keys for Windows
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\UPSTest" "DisplayName" "NSIS UPSTest"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\UPSTest" "UninstallString" '"$INSTDIR\uninstall.exe"'
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\UPSTest" "NoModify" 1
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\UPSTest" "NoRepair" 1
  WriteUninstaller "uninstall.exe"

SectionEnd

请提供您对此的意见。

1 个答案:

答案 0 :(得分:0)

您可以将.DLL放到临时目录中,然后再开始安装文件,然后在其中进行检查:

!include LogicLib.nsh

Section
InitPluginsDir
SetOutPath $PluginsDir
File "something\mydll.dll"
SetOutPath $Temp ; Don't hold the lock on $PluginsDir
System::Call 'KERNEL32::AddDllDirectory(w "$PluginsDir")' 

retry:
System::Call '$PluginsDir\mydll.dll::IsUPSPresentDummyFunction(i 0 r0)i.r0?e' ; Fake a return FALSE system call for this example
Pop $1 ; LastError
${If} $0 = 0
    MessageBox MB_RETRYCANCEL|MB_ICONEXCLAMATION "Blah blah UPS blah blah error $1" IDRETRY retry
    Abort "UPS required!" ; You can replace this with Quit if you want
${EndIf}

SetOutPath $InstDir
WriteUninstaller "$InstDir\uninstall.exe"
File "something\myapp.exe"
...
SectionEnd

$ PluginsDir在安装程序退出时自动删除。