NSIS utility functions

Contents:


General utility:
;------------------------------------------------------------------------------
; GetParent
; input, top of stack  (i.e. C:\Program Files\Poop)
; output, top of stack (replaces, with i.e. C:\Program Files)
; modifies no other variables.
;
; Usage:
;   Push "C:\Program Files\Directory\Whatever"
;   Call GetParent
;   Pop $0
;   ; at this point $0 will equal "C:\Program Files\Directory"


Function GetParent 
  Exch $0 ; old $0 is on top of stack
  Push $1
  Push $2
  StrCpy $1 -1
  loop:
    StrCpy $2 $0 1 $1
    StrCmp $2 "" exit
    StrCmp $2 "\" exit
    IntOp $1 $1 - 1
  Goto loop
  exit:
    StrCpy $0 $0 $1
    Pop $2
    Pop $1
    Exch $0 ; put $0 on top of stack, restore $0 to original value
FunctionEnd

;------------------------------------------------------------------------------
; TrimNewlines
; input, top of stack  (i.e. whatever$\r$\n)
; output, top of stack (replaces, with i.e. whatever)
; modifies no other variables.
;
Function TrimNewlines
  Exch $0
  Push $1
  Push $2
    StrCpy $1 0
    loop:
      IntOp $1 $1 - 1
      StrCpy $2 $0 1 $1
      StrCmp $2 "$\r" loop
      StrCmp $2 "$\n" loop
  IntOp $1 $1 + 1
    
  StrCpy $0 $0 $1
  Pop $2
  Pop $1
  Exch $0
FunctionEnd


System version checking:
;------------------------------------------------------------------------------
; GetWindows Version
;
; Based on Yazno's function, http://yazno.tripod.com/powerpimpit/
; Returns:
;
; $0 = Windows Version (95, 98, ME, NT x.x, 2000)
; or
; $0 = '' (Unknown Windows Version)
;

Function GetWindowsVersion
  Push $9
  ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion
  StrCmp $0 "" 0 lbl_winnt
  ; we are not NT.
  ReadRegStr $0 HKLM SOFTWARE\Microsoft\Windows\CurrentVersion VersionNumber
 
  StrCpy $9 $0 1
  StrCmp $9 '4' 0 lbl_error

  StrCpy $9 $0 3

  StrCmp $9 '4.0' lbl_win32_95
  StrCmp $9 '4.9' lbl_win32_ME lbl_win32_98

  lbl_win32_95:
    StrCpy $0 '95'
  Goto lbl_done

  lbl_win32_98:
    StrCpy $0 '98'
  Goto lbl_done

  lbl_win32_ME:
    StrCpy $0 'ME'
  Goto lbl_done

  lbl_winnt: 

    StrCpy $9 $0 1
    StrCmp $9 '3' lbl_winnt_x
    StrCmp $9 '4' lbl_winnt_x
    StrCmp $9 '5' lbl_winnt_5 lbl_error

    lbl_winnt_x:
      StrCpy $0 "NT $0" 6
    Goto lbl_done

    lbl_winnt_5:
      Strcpy $0 '2000'
    Goto lbl_done

  lbl_error:
    Strcpy $0 ''
  lbl_done:
  Pop $9
FunctionEnd

;------------------------------------------------------------------------------
; GetIEVersion
;
; Based on Yazno's function, http://yazno.tripod.com/powerpimpit/
; Returns:
;
; $0 = 1-6 (Installed IE Version)
; or 
; $0 = '' (IE is not installed)
;
; modifies no other variables

Function GetIEVersion
  ClearErrors
  ReadRegStr $0 HKLM "Software\Microsoft\Internet Explorer" "Version"
  IfErrors lbl_123 lbl_456

  lbl_456: ; ie 4+
    Strcpy $0 $0 1
  Goto lbl_done

  lbl_123: ; older ie version
    ClearErrors
    ReadRegStr $0 HKLM "Software\Microsoft\Internet Explorer" "IVer"
    IfErrors lbl_error

      StrCpy $0 $0 3
      StrCmp $0 '100' lbl_ie1
      StrCmp $0 '101' lbl_ie2
      StrCmp $0 '102' lbl_ie2

      StrCpy $0 '3' ; default to ie3 if not 100, 101, or 102.
      Goto lbl_done
        lbl_ie1: 
          StrCpy $0 '1'
        Goto lbl_done
        lbl_ie2:
          StrCpy $0 '2'
        Goto lbl_done
    lbl_error:
      StrCpy $0 ''
  lbl_done:
FunctionEnd


;------------------------------------------------------------------------------
; IsFlashInstalled
;
; By Yazno, http://yazno.tripod.com/powerpimpit/
; Returns:
;
; $0 = 0 (Flash is not installed)
; or
; $0 = 1 (Flash is installed)
;
; modifies no other variables

Function IsFlashInstalled
 ClearErrors
 ReadRegStr $0 HKCR "CLSID\{D27CDB6E-AE6D-11cf-96B8-444553540000}" ""
  IfErrors lbl_na lbl_ok
   lbl_na:
     StrCpy $0 '0'
     GoTo lbl_end
   lbl_ok:
     StrCpy $0 '1'
   lbl_end:
FunctionEnd



Winamp functions:
;------------------------------------------------------------------------------
; GetWinampInstPath
; 
; takes no parameters
; returns with the winamp install directory on the stack (it will be
; an empty string if winamp is not detected).
;
; modifies no other variables
;
; Usage:
;   Call GetWinampInstPath
;   Pop $0
;   MessageBox MB_OK "Winamp installed at: $0"

Function GetWinampInstPath
  Push $0
  Push $1
  Push $2
  ReadRegStr $0 HKLM \
     "Software\Microsoft\Windows\CurrentVersion\Uninstall\Winamp" \ 
     "UninstallString"
  StrCmp $0 "" fin

    StrCpy $1 $0 1 0 ; get firstchar
    StrCmp $1 '"' "" getparent 
      ; if first char is ", let's remove "'s first.
      StrCpy $0 $0 "" 1
      StrCpy $1 0
      rqloop:
        StrCpy $2 $0 1 $1
        StrCmp $2 '"' rqdone
        StrCmp $2 "" rqdone
        IntOp $1 $1 + 1
        Goto rqloop
      rqdone:
      StrCpy $0 $0 $1
    getparent:
    ; the uninstall string goes to an EXE, let's get the directory.
    StrCpy $1 -1
    gploop:
      StrCpy $2 $0 1 $1
      StrCmp $2 "" gpexit
      StrCmp $2 "\" gpexit
      IntOp $1 $1 - 1
      Goto gploop
    gpexit:
    StrCpy $0 $0 $1

    StrCmp $0 "" fin
    IfFileExists $0\winamp.exe fin
      StrCpy $0 ""
  fin:
  Pop $2
  Pop $1
  Exch $0
FunctionEnd


;------------------------------------------------------------------------------
; GetWinampVisPath
; 
; requires $INSTDIR to point to the Winamp directory.
; sets $OUTDIR with vis plug-in path
;
; modifies no other variables

Function GetWinampVisPath 
  ReadINIStr $OUTDIR $INSTDIR\winamp.ini Winamp VisDir 
  StrCmp $OUTDIR "" NoINISetting
    IfFileExists $OUTDIR Good
  NoINISetting:
    StrCpy $OUTDIR $INSTDIR\Plugins
  Good:
FunctionEnd 


;------------------------------------------------------------------------------
; GetWinampDSPPath
; 
; requires $INSTDIR to point to the Winamp directory.
; sets $OUTDIR with dsp plug-in path
;
; modifies no other variables

Function GetWinampDSPPath 
  ReadINIStr $OUTDIR $INSTDIR\winamp.ini Winamp DSPDir 
  StrCmp $OUTDIR "" NoINISetting
    IfFileExists $OUTDIR Good
  NoINISetting:
    StrCpy $OUTDIR $INSTDIR\Plugins
  Good:
FunctionEnd 

;------------------------------------------------------------------------------
; GetWinampSkinPath
; 
; requires $INSTDIR to point to the Winamp directory.
; sets $OUTDIR with skin plug-in path
;
; modifies no other variables

Function GetWinampSkinPath 
  ReadINIStr $OUTDIR $INSTDIR\winamp.ini Winamp SkinDir 
  StrCmp $OUTDIR "" NoINISetting
    IfFileExists $OUTDIR Good
  NoINISetting:
    StrCpy $OUTDIR $INSTDIR\Skins
  Good:
FunctionEnd 

;------------------------------------------------------------------------------
; CloseWinamp
; 
; Closes all running instances of Winamp 1.x/2.x
;
; modifies no other variables
Function CloseWinamp
  Push $0
  loop:
    FindWindow $0 "Winamp v1.x"
    IntCmp $0 0 done
     SendMessage $0 16 0 0
     Sleep 100
     Goto loop
  done:
  Pop $0
FunctionEnd



Netscape functions:
;------------------------------------------------------------------------------
; InstallNetscapePlugin
;
; Trashes $OUTDIR, $0, and $1.
;
; Replace 'mynetscapeplugin.dll' with the name of your DLL to extract.
;
; (pretty much untested, but should work)
;
; you may want to add is-netscape-running and error checking (if the file isn't writable)

Function InstallNetscapePlugin
  Push $OUTDIR
  Push $0
  Push $1
  StrCpy $0 0
  outer_loop:
    EnumRegKey $1 HKLM "Software\Netscape\Netscape Navigator" $0
    StrCmp $1 "" abort
    ReadRegStr $1 HKLM "Software\Netscape\Netscape Navigator\$1\Main" "Plugins Directory"    
    StrCmp $1 "" abort
    SetOutPath $1
    File mynetscapeplugin.dll
    IntOp $0 $0 + 1
  Goto outer_loop
  abort:
  Pop $1
  Pop $0
  Pop $OUTDIR
FunctionEnd

;------------------------------------------------------------------------------
; RemoveNetscapePlugin
;
; Replace 'mynetscapeplugin.dll' with the name of your DLL to delete.
;
; (pretty much untested, but should work)
;
; you may want to add is-netscape-running and error checking (if the delete doesn't work).


Function RemoveNetscapePlugin
  Push $0
  Push $1
  StrCpy $0 0
  outer_loop:
    EnumRegKey $1 HKLM "Software\Netscape\Netscape Navigator" $0
    StrCmp $1 "" abort
    ReadRegStr $1 HKLM "Software\Netscape\Netscape Navigator\$1\Main" "Plugins Directory"    
    StrCmp $1 "" abort
    Delete /REBOOTOK $1\mynetscapeplugin.dll
    IntOp $0 $0 + 1
  Goto outer_loop
  abort:
  Pop $1
  Pop $0
FunctionEnd