Get installer filename

From NSIS Wiki
Jump to navigationJump to search
Author: Afrow UK (talk, contrib)


METHOD 1: USING THE WINDOWS API (BEST)

Upgrade to NSIS 2.26 or above and use $EXEPATH.

Usage

DetailPrint $EXEPATH

METHOD 2: USING THE WINDOWS API (RECOMMENDED FOR OLD VERSIONS)

The Script

System::Call 'kernel32::GetModuleFileNameA(i 0, t .R0, i 1024) i r1'
;$R0 will contain the installer filename

METHOD 3: GET FROM THE COMMAND LINE

Note: If the installer is executed using the console, the command line might not contain the current folder or the .exe extension.

Usage

Call GetAppName
Pop $R0 ;Installer filename

The Function

Function GetAppName
 
  Push $R0
  Push $R1
  Push $R2
  Push $R3
 
  StrCpy $R2 1
  StrLen $R3 $CMDLINE
 
  ;Check for quote or space
  StrCpy $R0 $CMDLINE $R2
  StrCmp $R0 '"' 0 +3
    StrCpy $R1 '"'
    Goto loop
  StrCpy $R1 " "
 
  loop:
    IntOp $R2 $R2 + 1
    StrCpy $R0 $CMDLINE 1 $R2
    StrCmp $R0 $R1 get
    StrCmp $R2 $R3 get
    Goto loop
 
  get:
    StrCmp $R1 '"' 0 +4
      IntOp $R2 $R2 - 1
      StrCpy $R0 $CMDLINE $R2 1
      Goto exit
    StrCpy $R0 $CMDLINE $R2
 
  exit:
 
  Pop $R3
  Pop $R2
  Pop $R1
  Exch $R0
 
FunctionEnd