NSIS Beyond a Traditional Installation: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (better highlighting)
No edit summary
Line 247: Line 247:


[[Category:Code Examples]]
[[Category:Code Examples]]
[[Category:Tutorials]]

Revision as of 18:00, 3 January 2007

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