Skip to content

Instantly share code, notes, and snippets.

@bogdibota
Last active September 25, 2023 04:25
Show Gist options
  • Save bogdibota/062919938e1ed388b3db5ea31f52955c to your computer and use it in GitHub Desktop.
Save bogdibota/062919938e1ed388b3db5ea31f52955c to your computer and use it in GitHub Desktop.
electron-builder nsis install c++ redist 2017-2019
!include LogicLib.nsh
!macro customInit
Var /GLOBAL VCRedistDownload
${If} ${RunningX64}
;HKCR\Installer\Dependencies\VC,redist.x64,amd64,14.21,bundle\Dependents\{f4220b74-9edd-4ded-bc8b-0342c1e164d8}
;HKCR\Installer\Dependencies\VC,redist.x64,amd64,14.22,bundle\Dependents\{6361b579-2795-4886-b2a8-53d5239b6452}
;HKCR\Installer\Dependencies\VC,redist.x64,amd64,14.23,bundle\Dependents\{852adda4-4c78-4a38-b583-c0b360a329d6}
;HKCR\Installer\Dependencies\VC,redist.x64,amd64,14.24,bundle\Dependents\{282975d8-55fe-4991-bbbb-06a72581ce58}
;HKCR\Installer\Dependencies\VC,redist.x64,amd64,14.25,bundle\Dependents\{6913e92a-b64e-41c9-a5e6-cef39207fe89}
ReadRegStr $R0 HKCR "Installer\Dependencies\VC,redist.x64,amd64,14.21,bundle" "Version"
IfErrors 0 VSRedistInstalled
ReadRegStr $R0 HKCR "Installer\Dependencies\VC,redist.x64,amd64,14.22,bundle" "Version"
IfErrors 0 VSRedistInstalled
ReadRegStr $R0 HKCR "Installer\Dependencies\VC,redist.x64,amd64,14.23,bundle" "Version"
IfErrors 0 VSRedistInstalled
ReadRegStr $R0 HKCR "Installer\Dependencies\VC,redist.x64,amd64,14.24,bundle" "Version"
IfErrors 0 VSRedistInstalled
ReadRegStr $R0 HKCR "Installer\Dependencies\VC,redist.x64,amd64,14.25,bundle" "Version"
IfErrors 0 VSRedistInstalled
MessageBox MB_YESNO "This application requires$\r$\n\
'Microsoft Visual C++ Redistributable for Visual Studio 2015 - 2019 x64'$\r$\n\
to function properly.$\r$\n$\r$\n\
Download and install now?" /SD IDYES IDNO VSRedistInstalled
StrCpy $VCRedistDownload "https://aka.ms/vs/16/release/vc_redist.x64.exe"
${Else}
;HKCR\Installer\Dependencies\VC,redist.x86,x86,14.21,bundle\Dependents\{49697869-be8e-427d-81a0-c334d1d14950}
;HKCR\Installer\Dependencies\VC,redist.x86,x86,14.22,bundle\Dependents\{5bfc1380-fd35-4b85-9715-7351535d077e}
;HKCR\Installer\Dependencies\VC,redist.x86,x86,14.23,bundle\Dependents\{45231ab4-69fd-486a-859d-7a59fcd11013}
;HKCR\Installer\Dependencies\VC,redist.x86,x86,14.24,bundle\Dependents\{e31cb1a4-76b5-46a5-a084-3fa419e82201}
;HKCR\Installer\Dependencies\VC,redist.x86,x86,14.25,bundle\Dependents\{65e650ff-30be-469d-b63a-418d71ea1765}
ReadRegStr $R0 HKCR "Installer\Dependencies\VC,redist.x86,x86,14.21,bundle" "Version"
IfErrors 0 VSRedistInstalled
ReadRegStr $R0 HKCR "Installer\Dependencies\VC,redist.x86,x86,14.22,bundle" "Version"
IfErrors 0 VSRedistInstalled
ReadRegStr $R0 HKCR "Installer\Dependencies\VC,redist.x86,x86,14.23,bundle" "Version"
IfErrors 0 VSRedistInstalled
ReadRegStr $R0 HKCR "Installer\Dependencies\VC,redist.x86,x86,14.24,bundle" "Version"
IfErrors 0 VSRedistInstalled
ReadRegStr $R0 HKCR "Installer\Dependencies\VC,redist.x86,x86,14.25,bundle" "Version"
IfErrors 0 VSRedistInstalled
MessageBox MB_YESNO "This application requires$\r$\n\
'Microsoft Visual C++ Redistributable for Visual Studio 2015 - 2019 x86'$\r$\n\
to function properly.$\r$\n$\r$\n\
Download and install now?" /SD IDYES IDNO VSRedistInstalled
StrCpy $VCRedistDownload "https://aka.ms/vs/16/release/vc_redist.x86.exe"
${EndIf}
;if no goto executed, install vcredist
;create temp dir
CreateDirectory $TEMP\app-name-setup
;download installer
inetc::get "$VCRedistDownload" $TEMP\app-name-setup\vcppredist.exe
;exec installer
ExecWait "$TEMP\app-name-setup\vcppredist.exe"
VSRedistInstalled:
;nothing to do here
!macroend
@bogdibota
Copy link
Author

@camhart I'm glad you found this helpful!

I haven't managed to find a solution without hardcoding the versions :(
If you manage, to find something better, please let me know.

And regarding http being vulnerable, you are totally right. I was not aware that NSISdl::download does not support https 🙈

@danielehrhardt
Copy link

Amazing Thank you!

@tcardlab
Copy link

subtle variant I made a while back in case others find it useful:
https://github.com/GitCommons/cpp-redist-nsis/blob/main/installer.nsh

@mikelpr
Copy link

mikelpr commented Sep 25, 2023

I made one based on @tcardlab's that also checks if the install was successful https://gist.github.com/mikelpr/88cc99e8c760965249922108493102c1

It's only for x64 but should be easily adaptable to work with x86 (and arm64 too, I'd think)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment