Archive: Writing to ini problem


Writing to ini problem
Ok I have my installer built and I have a custom page where a user enters his or her username and password and then the installer writes their input to an ini file which that part works however when I first run the installer it does not write to the ini file I have to run it a second time in order for it to write the input from the username and password fields, Here is the contents of the nsi:

;--------------------------------
;Include Modern UI

!include "MUI.nsh"

;--------------------------------
;General

;Name and file
Name "Test Installer"
OutFile "Basic.exe"

;Default installation folder
InstallDir "$PROGRAMFILES\Test Directory"

;--------------------------------
;Interface Settings

!define MUI_ABORTWARNING

;--------------------------------
;Pages

!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "${NSISDIR}\Docs\Modern UI\License.txt"
Page custom CustomPageA
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

!insertmacro MUI_UNPAGE_WELCOME
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_UNPAGE_FINISH

;--------------------------------
;Languages

!insertmacro MUI_LANGUAGE "English"

;--------------------------------
;Reserve Files

;These files should be inserted before other files in the data block
;Keep these lines before any File command
;Only for solid compression (by default, solid compression is enabled for BZIP2 and LZMA)

ReserveFile "post.ini"
!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS

;--------------------------------
;Installer Sections

Section "Main Files" SecDummy

SetOutPath "$INSTDIR"

File "my.ini"

;Create uninstaller
WriteUninstaller "$INSTDIR\Uninstall.exe"

SectionEnd

;--------------------------------
;Installer Functions

Function .onInit

;Extract InstallOptions INI files
!insertmacro MUI_INSTALLOPTIONS_EXTRACT "post.ini"

FunctionEnd

LangString TEXT_IO_TITLE ${LANG_ENGLISH} "User Information"
LangString TEXT_IO_SUBTITLE ${LANG_ENGLISH} "Please enter your login information below"

Function CustomPageA

!insertmacro MUI_HEADER_TEXT "$(TEXT_IO_TITLE)" "$(TEXT_IO_SUBTITLE)"
!insertmacro MUI_INSTALLOPTIONS_DISPLAY "post.ini"
!insertmacro MUI_INSTALLOPTIONS_READ $0 "post.ini" "Field 1" "State"
!insertmacro MUI_INSTALLOPTIONS_READ $1 "post.ini" "Field 2" "State"
WriteINIStr "$INSTDIR\my.ini" "Data" "username" "$0"
WriteINIStr "$INSTDIR\my.ini" "Data" "password" "$1"
WriteINIStr "$INSTDIR\my.ini" "Data" "Directory" "$INSTDIR"


FunctionEnd

;--------------------------------
;Descriptions

;Language strings
LangString DESC_SecDummy ${LANG_ENGLISH} "A test section."

;Assign language strings to sections
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
!insertmacro MUI_DESCRIPTION_TEXT ${SecDummy} $(DESC_SecDummy)
!insertmacro MUI_FUNCTION_DESCRIPTION_END

;--------------------------------
;Uninstaller Section

Section "Uninstall"

;ADD YOUR OWN FILES HERE...

Delete "$INSTDIR\Uninstall.exe"
Delete "$INSTDIR\my.ini"

RMDir "$INSTDIR"

SectionEnd


Try changing these parts:

page custom CustomPageA to:
page custom CustomPageA CustomPageB

;this function is only supposed to display the page
;(a pre function).
Function CustomPageA

!insertmacro MUI_HEADER_TEXT "$(TEXT_IO_TITLE)" "$(TEXT_IO_SUBTITLE)"
!insertmacro MUI_INSTALLOPTIONS_DISPLAY "post.ini"

FunctionEnd

;this function is executed after the user clicks the next
;button while on the custom page (a leave function).
;this is when you write the data to the file.
Function CustomPageB

!insertmacro MUI_INSTALLOPTIONS_READ $0 "post.ini" "Field 1" "State"
!insertmacro MUI_INSTALLOPTIONS_READ $1 "post.ini" "Field 2" "State"
WriteINIStr "$INSTDIR\my.ini" "Data" "username" "$0"
WriteINIStr "$INSTDIR\my.ini" "Data" "password" "$1"
WriteINIStr "$INSTDIR\my.ini" "Data" "Directory" "$INSTDIR"

FunctionEnd


Ok after editing it, Now it doesn't write to the ini file the first or second time when I run the installer....?


Add the script as an attachment and I will fix it for you (if possible).


Ok thank you.


Oops, I made a stupid mistake. I should have put the code in CustomPageB in a section instead. Here is the fixed nsi.


Cool, Thank you so much for your help.


Yes thank you JasonFriday13 it has been very useful to me too ! ;)


No problem. It's nice to hear from people that benefit from other problems that have already been fixed.

[edit] One note I forgot to mention is that $INSTDIR is not initialized until the instfiles page, which is why the strings were not written in the leave page function, but do work in a section.[/edit]