NSIS Beyond a Traditional Installation

From NSIS Wiki
Jump to navigationJump to search
Author: Red Wine (talk, contrib)


Links

Description

The purpose of this project is to demonstrate how NSIS coding could be useful beyond the traditional creation of an installation.
We use here some of the useful nsis Functions & Macros and the md5 plugin to create a fancy stand alone gui that implements md5dll and generates a md5 value from contents of specified file.

  • Applies to NSIS 2.15

Working Example

!define TEMP1 $R0
!define LOG "$EXEDIR\MD5_Builder.Log"
 
!include 'TextFunc.nsh'
!include 'FileFunc.nsh'
!include 'WinMessages.nsh'
!include 'LogicLib.nsh'
!include 'StrFunc.nsh'
 
${StrIOToNSIS}
 
!insertmacro FileJoin
!insertmacro GetTime
!insertmacro GetFileName
 
var md5_result
var FILE_NAME
var file
var day
var month
var year
var weekday
var hour
var min
var sec
var ctlcolor
 
Name "MD5 CheckSum Builder"
Caption "MD5 CheckSum Builder"
OutFile "Builder.exe"
BrandingText " "
XPstyle on
 
ReserveFile "${NSISDIR}\Plugins\InstallOptions.dll"
ReserveFile "builder.ini"
 
Page custom SetCustom ValidateCustom ": Select a file"
PageEx instfiles
Caption ": Building MD5 CheckSum"
PageCallbacks "" "" InstFilesLeave
PageExEnd
 
 
Section "builder"
   HideWindow
 
   ReadINIStr '${TEMP1}' "$PLUGINSDIR\builder.ini" "Field 2" "State"
   StrCpy '$FILE_NAME' '${TEMP1}'
 
${If} ${FileExists} "$FILE_NAME"
    ReadINIStr ${TEMP1} "$PLUGINSDIR\builder.ini" "Field 5" "State"
 ${Unless} ${TEMP1} == "1"
   ${GetFileName} "$FILE_NAME" $file
   Call Get_Time
   ReadINIStr ${TEMP1} "$PLUGINSDIR\builder.ini" "Field 4" "State"
   ClearErrors
   Call Build_md5
   IfErrors _errors
  ${Select} "${TEMP1}"
   ${case} ""
    WriteIniStr "$PLUGINSDIR\builder.ini" "Field 4" "State" "MD5 checksum created at \
    $weekday $day-$month-$year $hour:$min:$sec \r\n-----------------------------------\
    -------------------------------------\r\nMD5 checksum for $file is: \r\n$md5_result"
    WriteIniStr "$PLUGINSDIR\builder.ini" "Field 3" "Flags" ""
    WriteIniStr "$PLUGINSDIR\builder.ini" "Field 5" "Flags" ""
    StrCpy $ctlcolor "1"
   ${caseElse}
    WriteIniStr "$PLUGINSDIR\builder.ini" "Field 4" "State" "${TEMP1} \r\n\r\nMD5 \
    checksum for $file is: \r\n$md5_result"
    StrCpy $ctlcolor "1"
  ${EndSelect}
 ${EndUnless}
${EndIf}
 
    ReadINIStr ${TEMP1} "$PLUGINSDIR\builder.ini" "Field 5" "State"
 
${If} ${TEMP1} == '1'
  ${AndIf} ${FileExists} "${LOG}"
    ClearErrors
    GetTempFileName $R7 $PLUGINSDIR
    FileOpen $R8 $R7 a
    ReadIniStr "${TEMP1}" "$PLUGINSDIR\builder.ini" "Field 4" "State"
    ${StrIOToNSIS} $R0 "${TEMP1}"
    FileWrite $R8 "$R0 $\r$\n$\r$\n$\r$\n"
    FileClose $R8
    IfErrors _errors
    ${FileJoin} "${LOG}" "$R7" ""
    IfErrors _errors
    Exec '"notepad.exe" "${LOG}"'
    WriteIniStr "$PLUGINSDIR\builder.ini" "Field 3" "Flags" "DISABLED"
    WriteIniStr "$PLUGINSDIR\builder.ini" "Field 5" "Flags" "DISABLED"
    WriteIniStr "$PLUGINSDIR\builder.ini" "Field 4" "State" ""
 
 ${ElseIf} ${TEMP1} == '1'
    ClearErrors
    GetTempFileName $R7 $PLUGINSDIR
    FileOpen $R8 $R7 a
    ReadIniStr "${TEMP1}" "$PLUGINSDIR\builder.ini" "Field 4" "State"
    ${StrIOToNSIS} $R0 "${TEMP1}"
    FileWrite $R8 "$R0 $\r$\n$\r$\n$\r$\n"
    FileClose $R8
    IfErrors _errors
    Rename $R7 "${LOG}"
    Exec '"notepad.exe" "${LOG}"'
    WriteIniStr "$PLUGINSDIR\builder.ini" "Field 3" "Flags" "DISABLED"
    WriteIniStr "$PLUGINSDIR\builder.ini" "Field 5" "Flags" "DISABLED"
    WriteIniStr "$PLUGINSDIR\builder.ini" "Field 4" "State" ""
${EndIf}
 
 
${If} ${Errors}
  _errors:
   WriteIniStr "$PLUGINSDIR\builder.ini" "Field 4" "State" "MD5 checksum created at \
   $weekday $day-$month-$year $hour:$min:$sec\r\n-------------------------------------\
   -----------------------------------\r\nError creating MD5 checksum for $file"
   MessageBox MB_OK|MB_ICONSTOP 'Error creating MD5 checksum for $file'
${EndIf}
 
   WriteIniStr "$PLUGINSDIR\builder.ini" "Field 2" "State" ""
   WriteIniStr "$PLUGINSDIR\builder.ini" "Field 5" "State" ""
SectionEnd
 
 
Function .onInit
    InitPluginsDir
    File /oname=$PLUGINSDIR\builder.ini "builder.ini"
FunctionEnd
 
 
Function Get_Time
    ${GetTime} "" "L" $day $month $year $weekday $hour $min $sec
FunctionEnd
 
 
Function Build_md5
    md5dll::GetMD5File "$FILE_NAME"
    Pop $0
    StrCpy "$md5_result" "$0"
    Push $0
FunctionEnd
 
 
Function SetCustom
${If} $ctlcolor == "1"
    Push ${TEMP1}
    Push $6
    InstallOptions::initDialog /NOUNLOAD "$PLUGINSDIR\builder.ini"
    Pop ${TEMP1}
    ReadINIStr $6 "$PLUGINSDIR\builder.ini" "Field 3" "HWND"
    SetCtlColors $6 0x3E55B0 transparent
    ReadINIStr $6 "$PLUGINSDIR\builder.ini" "Field 4" "HWND"
    SetCtlColors $6 0x000000 0xFFFFFF
    InstallOptions::show
    Pop ${TEMP1}
    Pop $6
 ${Else}
    Push ${TEMP1}
    InstallOptions::dialog "$PLUGINSDIR\builder.ini"
    Pop ${TEMP1}
    Pop ${TEMP1}
${EndIf}
FunctionEnd
 
 
Function ValidateCustom
    ReadINIStr '${TEMP1}' "$PLUGINSDIR\builder.ini" "Field 5" "State"
${If} ${TEMP1} <> "1"
    ReadINIStr '${TEMP1}' "$PLUGINSDIR\builder.ini" "Field 2" "State"
${EndIf}
${If} ${TEMP1} == ""
    MessageBox MB_ICONEXCLAMATION|MB_OK "You must select a valid file!"
    Abort
${EndIf}
    StrCpy $ctlcolor ""
FunctionEnd
 
Function InstFilesLeave
    Bringtofront
    SendMessage $HWNDPARENT "0x408" "-1" "0"
    Abort
FunctionEnd

InstallOptions INI

[Settings]
NumFields=5
NextButtonText=Build
CancelButtonText=Close
 
[Field 1]
Type=GroupBox
Left=0
Right=-1
Top=0
Bottom=-1
Text=" Browse for file to create the MD5 checksum... "
 
[Field 2]
Type=FileRequest
Left=11
Right=-11
Top=25
Bottom=38
Filter=Program Files|*.exe|DLL Files|*.dll|All files|*.*
Flags=FILE_MUST_EXIST|FILE_EXPLORER
 
[Field 3]
Type=Label
Left=14
Right=-120
Top=45
Bottom=55
Text="MD5 Checksum Builder Log"
Flags=DISABLED
 
[Field 4]
Type=Text
Flags=VSCROLL|MULTILINE|READONLY
State=
Left=11
Right=-12
Top=56
Bottom=112
 
[Field 5]
Type=CheckBox
Text=" Dump Log Window to file"
Left=12
Right=-120
Top=116
Bottom=124
State=
Flags=DISABLED

License

This example script is provided 'as-is', without any express or implied warranty. In no event will the author be held liable for any damages arising from the use of this script.

Permission is granted to anyone to use this script for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

  1. The origin of this script must not be misrepresented; you must not claim that you wrote the original code. If you use this script in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  2. Altered versions must be plainly marked as such, and must not be misrepresented as being the original script.
  3. This notice may not be removed or altered from any distribution.