SoFunction
Updated on 2025-03-06

Bat script instance implementation only allows one instance to be run (installer, create shortcut scripts)


;My first install script
!include ""
!define DIR "D:\workspace\nsis\files"

Name "Installation Program"
Icon "${DIR}\"
OutFile ""
InstallDir "$PROGRAMFILES\kaserv"
RequestExecutionLevel admin

!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES

!insertmacro MUI_LANGUAGE "SimpChinese"

Function .onInit
 System::Call 'kernel32::CreateMutexA(i 0,i 0,t "SMAPLE_MUTEX") i .r1 ?e'
 Pop $R0
 StrCmp $R0 0 +3
MessageBox MB_OK|MB_ICONSTOP "Only one instance is allowed to run"
 Abort
FunctionEnd

;Installation Program Block
Section
SetOutPath $INSTDIR
File "${DIR}\New text document (2).txt"
File "${DIR}\New text document.txt"
SetOutPath "$INSTDIR\office"
File "${DIR}\office\*"
;Shortcut
CreateShortCut "$DESKTOP\" "$INSTDIR\"
CreateShortCut "$SMPROGRAMS\" "$INSTDIR\"
WriteUninstaller "$INSTDIR\"
SectionEnd

;Uninstall the program block
Section "Uninstall"
RMDir /r "$INSTDIR"
Delete "$DESKTOP\"
Delete "$SMPROGRAMS\"
SectionEnd