Appendix C: Useful Scripts

C.1 Get Internet Explorer version

  1. ; GetIEVersion
  2. ;
  3. ; Based on Yazno's function, http://yazno.tripod.com/powerpimpit/
  4. ; Returns 1-6 (IE Version) or '' (IE is not installed) on top of the stack
  5. ;
  6. ; Usage:
  7. ; Call GetIEVersion
  8. ; Pop $R0 ; at this point $R0 is "5" or whatnot
  9.  
  10. Function GetIEVersion
  11. Push $R0
  12. ClearErrors
  13. ReadRegStr $R0 HKLM "Software\Microsoft\Internet Explorer" "Version"
  14. IfErrors lbl_123 lbl_456
  15.  
  16. lbl_456: ; ie 4+
  17. Strcpy $R0 $R0 1
  18. Goto lbl_done
  19.  
  20. lbl_123: ; older ie version
  21. ClearErrors
  22. ReadRegStr $R0 HKLM "Software\Microsoft\Internet Explorer" "IVer"
  23. IfErrors lbl_error
  24.  
  25. StrCpy $R0 $R0 3
  26. StrCmp $R0 '100' lbl_ie1
  27. StrCmp $R0 '101' lbl_ie2
  28. StrCmp $R0 '102' lbl_ie2
  29.  
  30. StrCpy $R0 '3' ; default to ie3 if not 100, 101, or 102.
  31. Goto lbl_done
  32. lbl_ie1:
  33. StrCpy $R0 '1'
  34. Goto lbl_done
  35. lbl_ie2:
  36. StrCpy $R0 '2'
  37. Goto lbl_done
  38. lbl_error:
  39. StrCpy $R0 ''
  40. lbl_done:
  41. Exch $R0
  42. FunctionEnd

C.2 Is .NET Framework installed?

  1. ; IsDotNETInstalled
  2. ;
  3. ; Based on GetDotNETVersion
  4. ; http://nsis.sourceforge.net/Get_.NET_Version
  5. ;
  6. ; Usage:
  7. ; Call IsDotNETInstalled
  8. ; Pop $0
  9. ; StrCmp $0 1 found_dotNETFramework no_dotNETFramework
  10.  
  11. Function IsDotNETInstalled
  12. Push $0
  13. Push $1
  14.  
  15. StrCpy $0 1
  16. System::Call "mscoree::GetCORVersion(w, i ${NSIS_MAX_STRLEN}, *i) i .r1"
  17. StrCmp $1 0 +2
  18. StrCpy $0 0
  19.  
  20. Pop $1
  21. Exch $0
  22. FunctionEnd

C.3 Is Macromedia Flash Player installed?

  1. ; IsFlashInstalled
  2. ;
  3. ; By Yazno, http://yazno.tripod.com/powerpimpit/
  4. ; Returns the result on top of the stack
  5. ;
  6. ; Usage:
  7. ; Call IsFlashInstalled
  8. ; Pop $R0 ; $R0 is "1" or "0" at this point
  9.  
  10. Function IsFlashInstalled
  11. Push $R0
  12. ClearErrors
  13. ReadRegStr $R0 HKCR "CLSID\{D27CDB6E-AE6D-11cf-96B8-444553540000}" ""
  14. IfErrors lbl_na
  15. StrCpy $R0 1
  16. Goto lbl_end
  17. lbl_na:
  18. StrCpy $R0 0
  19. lbl_end:
  20. Exch $R0
  21. FunctionEnd

C.4 Connect to the Internet

  1. ; ConnectInternet (uses Dialer plug-in)
  2. ; Written by Joost Verburg
  3. ;
  4. ; This function attempts to make a connection to the internet if there is no
  5. ; connection available. If you are not sure that a system using the installer
  6. ; has an active internet connection, call this function before downloading
  7. ; files with NSISdl.
  8. ;
  9. ; The function requires Internet Explorer 3, but asks to connect manually if
  10. ; IE3 is not installed.
  11.  
  12. Function ConnectInternet
  13.  
  14. Push $R0
  15.  
  16. ClearErrors
  17. Dialer::AttemptConnect
  18. IfErrors noie3
  19.  
  20. Pop $R0
  21. StrCmp $R0 "online" connected
  22. MessageBox MB_OK|MB_ICONSTOP "Cannot connect to the internet."
  23. Quit ;This will quit the installer. You might want to add your own error handling.
  24.  
  25. noie3:
  26.  
  27. ; IE3 not installed
  28. MessageBox MB_OK|MB_ICONINFORMATION "Please connect to the internet now."
  29.  
  30. connected:
  31.  
  32. Pop $R0
  33.  
  34. FunctionEnd

C.5 Get Installer Filename

  1. System::Call 'kernel32::GetModuleFileName(p 0, t .R0, i ${NSIS_MAX_STRLEN}) i.r1'
  2. ;$R0 will contain the installer filename

C.6 Prevent Multiple Instances

Put the following code in your .onInit function:

  1. System::Call 'kernel32::CreateMutex(p 0, i 0, t "myMutex") p .r1 ?e'
  2. Pop $R0
  3.  
  4. StrCmp $R0 0 +3
  5. MessageBox MB_OK|MB_ICONEXCLAMATION "The installer is already running."
  6. Abort

'myMutex' must be replaced by a unique value!

C.7 More

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