Useful Scripts

Previous | Contents | Next

- Appendix C: Useful Scripts

- Get Internet Explorer version
- Is .NET Framework installed?
- Is Macromedia Flash Player installed?
- Connect to the Internet
- Prevent Multiple Instances
- More

# Appendix C: Useful Scripts

## C.1 Get Internet Explorer version

    ; GetIEVersion
    ;
    ; Returns 1-11 (IE Version) or '' (IE is not installed) on top of the stack
    ;
    ; Usage:
    ;   Call GetIEVersion
    ;   Pop $R0 ; $R0 is "5" etc.

    Function GetIEVersion
      Push $R0
      ReadRegStr $R0 HKLM "Software\Microsoft\Internet Explorer" "svcVersion" ; IE v10+
      StrCpy $R0 $R0 2
      IntCmp $R0 9 "" "" lbl_done
      ClearErrors
      ReadRegStr $R0 HKLM "Software\Microsoft\Internet Explorer" "Version" ; IE v4..9
      IfErrors lbl_123
        StrCpy $R0 $R0 1 ; Note: This truncates 5.50 to 5 etc.
        Goto lbl_done
      lbl_123:
      !if "${NSIS_PTR_SIZE}" > 4
      StrCpy $R0 ""
      !else
      ReadRegStr $R0 HKLM "Software\Microsoft\Internet Explorer" "IVer" ; IE v1..3
      IntCmp $R0 99 "" "" +3
        StrCpy $R0 ""
        Goto lbl_done
      IntOp $R0 $R0 & 3 ; 100..103->0..3
      IntCmp $R0 2 +2 "" +2
        IntOp $R0 $R0 + 1 ; Bump 100->v1 and 101->v2 (Is 101 v1.5 or 2.0?)
      !endif
      lbl_done:
      Exch $R0
    FunctionEnd

## C.2 Is .NET Framework installed?

    ; IsDotNETInstalled
    ;
    ; NOTE: This is only able to detect .NET v1.x and v2.x!
    ;
    ; Based on GetDotNETVersion
    ;   https://nsis.sourceforge.io/Get_.NET_Version
    ;
    ; Usage:
    ;   Call IsDotNETInstalled
    ;   Pop $0 ; 0 or 1
    ;   StrCmp $0 1 found_dotNETFramework_v1_or_v2 no_dotNETFramework

    Function IsDotNETInstalled
      Push $0
      System::Call '"$SysDir\MSCOREE.dll"::GetCORVersion(w,i${NSIS_MAX_STRLEN},*i)i.r0?u'
      IntOp $0 $0 ! ; HRESULT (S_OK) -> BOOL
      Exch $0
    FunctionEnd

## C.3 Is Macromedia Flash Player installed?

    ; IsFlashInstalled
    ;
    ; Usage:
    ;   Call IsFlashInstalled
    ;   Pop $R0 ; 1 or ""

    Function IsFlashInstalled
    Push $R0
    ReadRegStr $R0 HKCR "CLSID\{D27CDB6E-AE6D-11cf-96B8-444553540000}" ""
    StrCmp $R0 "" +2
      StrCpy $R0 "1"
    Exch $R0
    FunctionEnd

## C.4 Connect to the Internet

     ; ConnectInternet (uses Dialer plug-in) - Written by Joost Verburg
     ;
     ; This function attempts to make a connection to the internet if there is no
     ; connection available. If you are not sure that a system using the installer
     ; has an active internet connection, call this function before downloading
     ; files with NSISdl.
     ;
     ; The function requires Internet Explorer 3, but asks to connect manually if
     ; IE3 is not installed.

     Function ConnectInternet

       Push $R0

         ClearErrors
         Dialer::AttemptConnect
         IfErrors noie3

         Pop $R0
         StrCmp $R0 "online" connected
           MessageBox MB_OK|MB_ICONSTOP "Cannot connect to the internet."
           Quit ; This will quit the installer. You might want to add your own error handling.

         noie3:

         ; IE3 not installed
         MessageBox MB_OK|MB_ICONINFORMATION "Please connect to the internet now."

         connected:

       Pop $R0

     FunctionEnd

## C.5 Prevent Multiple Instances

Put the following code in your .onInit function:

     System::Call 'kernel32::CreateMutex(p 0, i 0, t "myMutex") p .r1 ?e'
     Pop $R0

     StrCmp $R0 0 +3
       MessageBox MB_OK|MB_ICONEXCLAMATION "The installer is already running."
       Abort

'myMutex' must be replaced by a unique string or GUID!

## C.6 More

You can find more useful scripts on the NSIS Wiki, the NSIS forum and the NSIS development page.

Previous | Contents | Next