Topic 4.8

Code Snippets

A Collection of Useful Snippets

NSIS Code Snippets

Here you can find a small little library of NSIS macros, functions, and other code snippets. This is a on going project so come back to see new content. If you've got something you think and deem invaluable to this collection, don't hesitate to get in touch with me by leaving a comment in the comment section below. I'm always open to add to this section.

Useful Snippets

Admin Check

Discription
This little snippet will set the variable $Admin to equal true if the host machine is running with elevated privileges. You can then check against this variable at a later time for it's boolean value.
Var Admin
System::Call `kernel32::GetModuleHandle(t 'shell32.dll') i .s`
System::Call `kernel32::GetProcAddress(i s, i 680) i .r0`
System::Call `::$0() i .r0`
StrCmpS $0 1 "" +2
StrCpy $Admin true

;=#
;= USAGE
;
${If} $Admin == true
	...
${EndIf}

PAF Specifications

Discription
Here's some code that will be useful for those of you who would like to grab the PAFs specifications (product version or company name, etc.) from AppInfo.ini without having to edit PortableApps.comLauncher.nsi manually. This is especially useful if using FGs builds. By using these the values in AppInfo.ini must be there otherwise an error will occur when trying to compile. To remedy this just comment out the ones you won't use; most likely the trademark one.
;=#
;=
; This one will read AppInfo.ini for the value of AppID
; Then set ${APP} and ${APP64} accordingly.
!searchparse /file ${PACKAGE}\App\AppInfo\appinfo.ini `AppID=` APPNAME `` ;= Reads value
!searchreplace APP "${APPNAME}" "Portable" "" ;= Removes "Portable" from string. | = AppName
!searchreplace APP64 "${APPNAME}" "Portable" "64" ;= Replaces "Portable" with "64" | = AppName64

;=
; This one will read AppInfo.ini for the value of Name and sets it to ${PORTABLEAPPNAME}
; Then set ${FULLNAME} after removing "Portable"
!searchparse /file ${PACKAGE}\App\AppInfo\appinfo.ini `Name=` PORTABLEAPPNAME `` ;= App Name Portable
!searchreplace FULLNAME "${PORTABLEAPPNAME}" " Portable" "" ;= App Name

;=
; This one will read AppInfo.ini for the value of Publisher and sets it to ${PUBLISHER}
!searchparse /file ${PACKAGE}\App\AppInfo\appinfo.ini `Publisher=` PUBLISHER ``

;=
; This one will read AppInfo.ini for the value of Publisher and sets it to ${PACKAGE_VERSION}
!searchparse /file ${PACKAGE}\App\AppInfo\appinfo.ini `PackageVersion=` PACKAGE_VERSION ``

;=
; This one will read AppInfo.ini for the value of Trademarks and sets it to ${TRADEMARK}
!searchparse /file ${PACKAGE}\App\AppInfo\appinfo.ini `Trademarks=` TRADEMARK ``

Dynamically Set Year

Discription
This very simple snippet will give you the means to set it and forget it. Basically, it'll grab the current year, put it in a define and then you can output it in the application's version details.
;=#
; Sets ${YEAR} to hold the current year.
;
!define /date YEAR		`%Y`

;=#
; And now you may use this like so:
;
VIAddVersionKey /LANG=${LANG_ENGLISH} LegalCopyright   `Copyright © Company ${YEAR}`
Useful Macros

Win10 Integrity Check

Discription
With this operating system specific macro for Windows 10 you can either enable or disable driver signature enforcement. When this switch is off it will allow you to install unsigned drivers. Be sure to re-enable this setting as this can be a security risk. How it works is it will first check the registry to see if running on Windows 10 and if so will then proceed with the command depending on the given switch parameter.
;=
; DESCRIPTION:
;
; Enable or disable driver signature enforcement on Windows 10.
; Will first check the registry to see if running on Windows 10 
; and if so will then proceed with the command depending on the
; given switch parameter.
;
;=
; USAGE:
;
; ${Win10::IntegrityCheck} /DISABLEFSR "SWITCH" $0 $1
;
;    IntegrityCheck = Enables/disables driver signature enforcement. 
;    /DISABLEFSR    = Disables redirection if x64. Use "" to skip.
;    SWITCH         = On/Off Switch
;    $0             = Return after call
;    $1              =   ''    ''    ''
;
!define Win10::IntegrityCheck `!insertmacro _Win10::IntegrityCheck`
!macro _Win10::IntegrityCheck _FSR _SWITCH _ERR1 _ERR2
	!define BCD `bcdedit.exe`
	ReadRegStr $R0 HKLM `SOFTWARE\Microsoft\Windows NT\CurrentVersion` `CurrentMajorVersionNumber`
	IfErrors +14
	IntCmp $R0 10 0 +13
	StrCmp "${_FSR}" /DISABLEFSR 0 +6
	StrCmp "${_SWITCH}" "ON" 0 +3
	ExecDos::Exec /TOSTACK /DISABLEFSR `"${BCD}" /set nointegritychecks off`
	Goto +7
	ExecDos::Exec /TOSTACK /DISABLEFSR `"${BCD}" /set nointegritychecks on`
	Goto +5
	StrCmp "${_SWITCH}" "ON" 0 +3
	ExecDos::Exec /TOSTACK `"${BCD}" /set nointegritychecks off`
	Goto +2
	ExecDos::Exec /TOSTACK `"${BCD}" /set nointegritychecks on`
	Pop ${_ERR1}
	Pop ${_ERR2}
!macroend

Useful Functions

GetJRE

Discription
This function will return the full absolute path of a valid java.exe. This will run a check in the following order: within the App directory installed with the application and if not found there it will then check the %JAVA_HOME% envirornment variable. If that's empty it will then try to look for the corresponding registry entry for it's location. If that fails to yield a location it will look in the current working directory or hope it finds it in the %PATH% envirornment variable.
;=#
; 
; USAGE:
; Call GetJRE
; Pop $0
;
Function GetJRE
	Push $R0
	Push $R1
	!define JAVAEXE "javaw.exe"
	ClearErrors
	StrCpy $R0 "${APPDIR}\jre\bin\${JAVAEXE}"
	IfFileExists $R0 JreFound
	StrCpy $R0 ""
	ClearErrors
	ReadEnvStr $R0 "JAVA_HOME"
	StrCpy $R0 "$R0\bin\${JAVAEXE}"
	IfErrors 0 JreFound
	ClearErrors
	ReadRegStr $R1 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" "CurrentVersion"
	ReadRegStr $R0 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment\$R1" "JavaHome"
	StrCpy $R0 "$R0\bin\${JAVAEXE}"
	IfErrors 0 JreFound
	StrCpy $R0 "${JAVAEXE}"
	JreFound:
	Pop $R1
	Exch $R0
FunctionEnd