Ocs Generic installer

From NSIS Wiki

Author: bibi92 (talk, contrib)


Description

This is a code example used to create Silent installers for Ocs Inventory deployement

Just fill !Define And compile.

The Script

##################################################################
## Generic installer
## Copyleft Emmanuel GUILLORY 2008
## Web : http://ocsinventory-ng.org
##################################################################
#
setcompressor /SOLID lzma
#************************* JUST  Fill THESES options  *************
# Project name that users can see (if no silent install)
!define Project_Name "Flash_player_9" ; This is just an example
#
# full exe path if not in same folder that this .nsi
!define Original_Prog "install_flash_player.exe"
#
# Original exe or msi options (for unattended) /quiet or /passive are frequently used
!define Original_Prog_options "/S" ; /S is often used by NSIS Installers
#
# Min required disk size (Mo) (Use 4x than real needed size)
!define Min_size_required "10"
# Message to display if free disk size limit is reached
!define Message_size "$\r$\nInstall aborted! Not enough disk space. $\r$\n $\r$\n ${Min_size_required} Mo needed.$\r$\n $\r$\n$2 Mo free space. "
#
# Waiting mesage
!define Message_wait "Please wait..." # "Please wait..."
#
# Process killer (nothing or exe name)
!define Process_to_kill "firefox.exe"  ;This is just an example
#
# Normal or Silent
!define Silent_Install "Normal" # or "Silent"
#
# Message to display if not admin right
!define Message_Not_Admin_Right "You do not have admin privilege." #You are not connected as administrator
#
#******************************************************************
 
!define sysGetDiskFreeSpaceEx 'kernel32::GetDiskFreeSpaceExA(t, *l, *l, *l) i'
!include "MUI.nsh"
!insertmacro MUI_LANGUAGE "French"
 
ShowInstDetails hide
 
Name "${Project_Name} - ${Message_wait}"
ReserveFile "${NSISDIR}\Plugins\BGImage.dll"
OutFile "${Project_Name}.exe"
BrandingText "${Project_Name}"
BGGradient 55ffff 8888FF  cc2020
silentinstall ${Silent_Install}
;--------------------------------
;Version Information
Function .onInit
        ;Prevent Multiple Instances
        System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${Project_Name}") i .r1 ?e'
        Pop $R0
        StrCmp $R0 0 not_running
        ;MessageBox MB_OK|MB_ICONEXCLAMATION "The installer is already running."
        quit
not_running:
       StrCpy $0 ${Min_size_required} ; kb u need
       StrCpy $1 'c:' ; check drive c: for space
       StrCpy $2 0 ; 0 = ignore quotas
       Call CheckSpaceFunc
       IntCmp $3 1 okay
       MessageBox MB_OK|MB_TOPMOST|MB_ICONSTOP "${Message_size}"
       quit
  okay:
       InitPluginsDir
       File /oname=$PLUGINSDIR\prog.exe  "${Original_Prog}"
FunctionEnd
 
Function KillProcess
       KillProcDLL::KillProc "${Process_to_kill}"
       sleep 200
FunctionEnd
 
Section
       hidewindow
       setautoclose true
       Call IsUserAdmin
       Pop "$R0"
       StrCmp $R0 "true" Okadmin 0
       messagebox MB_ICONSTOP "${Message_Not_Admin_Right}"
       Abort
Okadmin:
       call KillProcess
       push $R0
       setoutpath $PLUGINSDIR
       execwait '$PLUGINSDIR\prog.exe ${Original_Prog_options}' $0
       detailprint "Install result: $0"
       seterrorlevel $0
       pop $R0
       sleep 1000
SectionEnd
 
; $0 - space required in kb
; $1 - path to check
; $2 - 0 = ignore quotas, 1 = obey quotas
; result in $3 : 0=NOK
 
function CheckSpaceFunc
         IntCmp $2 0 ignorequota
         ; obey quota
         System::Call '${sysGetDiskFreeSpaceEx}(r1,.r2,,.)'
         goto converttokb
         ; ignore quota
         ignorequota:
         System::Call '${sysGetDiskFreeSpaceEx}(r1,.,,.r2)'
         converttokb:
         ; convert the large integer byte values into managable Mb
         System::Int64Op $2 / 1000000
         Pop $2
         ; check space
         System::Int64Op $2 > $0
         Pop $3
functionend
 
#####################################################################
# This function try to find if logged in user has admin rights
#####################################################################
; Author: Lilla (lilla@earthlink.net) 2003-06-13
; function IsUserAdmin uses plugin \NSIS\PlusgIns\UserInfo.dll
; This function is based upon code in \NSIS\Contrib\UserInfo\UserInfo.nsi
; This function was tested under NSIS 2 beta 4 (latest CVS as of this writing).
;
; Usage:
;   Call IsUserAdmin
;   Pop $R0   ; at this point $R0 is "true" or "false"
Function IsUserAdmin
  Push $R0
  Push $R1
  Push $R2
  ClearErrors
  UserInfo::GetName
  IfErrors IsUserAdmin_Win9x
  ; Assuming Windows NT
  Pop $R1
  UserInfo::GetAccountType
  Pop $R2
  StrCmp $R2 "Admin" 0 IsUserAdmin_Continue
; Observation: I get here when running Win98SE. (Lilla)
; The functions UserInfo.dll looks for are there on Win98 too,
; but just don't work. So UserInfo.dll, knowing that admin isn't required
; on Win98, returns admin anyway. (per kichik)
; MessageBox MB_OK 'User "$R1" is in the Administrators group'
  StrCpy $R0 "true"
  Goto IsUserAdmin_end
IsUserAdmin_Continue:
; You should still check for an empty string because the functions
; UserInfo.dll looks for may not be present on Windows 95. (per kichik)
  StrCmp $R2 "" IsUserAdmin_Win9x
  StrCpy $R0 "false"
;MessageBox MB_OK 'User "$R1" is in the "$R2" group'
  Goto IsUserAdmin_end
IsUserAdmin_Win9x:
; comment/message below is by UserInfo.nsi author:
; This one means you don't need to care about admin or
; not admin because Windows 9x doesn't either
;MessageBox MB_OK "Error! This DLL can't run under Windows 9x!"
  StrCpy $R0 "true"
IsUserAdmin_end:
 ;MessageBox MB_OK 'User= "$R1"  AccountType= "$R2"  IsUserAdmin= "$R0"'
  Pop $R2
  Pop $R1
  Exch $R0
FunctionEnd

Credits

This is not all my code. Some parts come from the NSIS Wiki.

Personal tools
donate
ads