NSIS silent install ( started via code )

◇◆丶佛笑我妖孽 提交于 2019-12-04 20:13:32

StrCmp $0 1 0 +3 is wrong, it should be +2 (+3 in this example is probably undefined behavior since you are skipping the hidden return instruction).

Using relative jumps is error prone, you should use a label or rewrite it using the logic lib:

!include LogicLib.nsh
...
Function .onInit
  Call GetDotNet

  ${If} ${Silent}
    call SilentInstall
  ${EndIf}
FunctionEnd

But putting the silent install logic in a separate function doubles your work when the normal install logic is almost equal. You should be able to remove the SilentInstall function and just use a hidden section to execute during silent installs:

Section "${_AppName} (required)"
#shared install code
SectionEnd

Section "Start Menu Shortcuts"
#shared startmenu code
SectionEnd

Section
${If} ${Silent}
    Exec ${_AppExe}
${EndIf}
Section

It is hard to say why your files are not updated, but if you use shared code you can run it without /S and check the detail log. My only guess is that InstallDirRegKey is picking up a non default installdir and you are looking at the wrong files. You can Process Monitor to monitor the install.


Your code has some other unrelated issues:

  • RequestExecutionLevel admin is not enough, you need to deal with NT5 and NT6 with UAC off (UserInfo::GetAccountType)
  • You don't have to specify the shortcut icon when the icon is the same as the target program
  • You should quote the path when calling Exec: Exec '"$instdir\${_AppExe}"'
  • Mixing UAC/runas/RequestExecutionLevel admin with Exec is problematic since you can end up running the program as the wrong user.

This is a tricky one. I am using the same approach to update an application of mine, calling an NSIS installer silently. In my case, the problem did not lie in the NSIS script, it was that I was using the WebClient class to download a zip file containing my installer to the temp directory, and subsequently extracting it there, and running it from there. The problem is that the ZipFile class generates an exception if the extracted setup file exists, and hence the old installer persists, and nothing except the uninstall executable is updated in the program directory.

For me, the solution was to clean up the old zip and exe files before downloading the new ones (in VB.NET):

    If File.Exists(Path.Combine(Path.GetTempPath(), "Setup.zip")) Then
        File.Delete(Path.Combine(Path.GetTempPath(), "Setup.zip"))
    End If
    If File.Exists(Path.Combine(Path.GetTempPath(), "Setup.exe")) Then
        File.Delete(Path.Combine(Path.GetTempPath(), "Setup.exe"))
    End If
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!