Skip to content
⌘ NSIS Forum Archive

Read data from end of installer, append it to installed file

4 posts

svens#

Read data from end of installer, append it to installed file

Hello everyone,

In order to pass some parameters from a website to an exe file, I dynamically append 68 bytes of data to it whenever a user downloads it.

Now I'd like to have an installer for this application, but with a setup file I obviously can't append this data directly to the executable. From asking on IRC, I know that it won't break the installer when data is appended to it. So what I tried to do is append the data to the installer and then write it to the installed exe file during installation. I tried to adapt the ReadCustomerData script from the wiki, but my script fails to do the job.

This is the relevant part of my script, I added some error checking to find out what's going wrong:
  # Backup variables
  Push $1    # sbar.exe file handle
  Push $2    # installer file handle
  Push $3    # current byte
  Push $4    # read count
  Push $5    # write count
  IntOp $4 $4 & 0
  IntOp $5 $5 & 0
  ClearErrors
  
  # Open sbar.exe to write (seek to file end)
  FileOpen $1 "$INSTDIR\sbar.exe" a
  FileSeek $1 0 END
  DetailPrint $1
  
  # Open installer to read
  FileOpen $2 "$EXEPATH" r
  FileSeek $2 -68 END
  DetailPrint $2
  
  IfErrors error
  
  loop:
  # Read and write one byte
  FileReadByte $1 $3
  DetailPrint $3
  IfErrors done
  IntOp $4 $4 + 1
  FileWriteByte $2 $3
  IfErrors error
  IntOp $5 $5 + 1
  Goto loop
  
  error:
  MessageBox MB_OK "There was an error processing the file (read=$4, write=$5)."
  Abort
  
  done:
  MessageBox MB_OK "Read $4, wrote $5 bytes."
  # Close files
  FileClose $1
  FileClose $2
  
  # Restore variables
  Pop $4
  Pop $3
  Pop $2
  Pop $1 
When I run the installer on my Win7 x64 as admin, a message box pops up, saying that zero bytes have been read and written (i.E. 'done' label). If I interpret that correctly the files could both be opened but the reading operation fails.

This is my first script (and nsis installer), please tell me if there's some stupid mistake.

Thanks in advance.
svens#
Originally Posted by Anders View Post
Solved on IRC, the reading and writing handles ($1 and $2) have to be swapped
Yep, thanks for that.

Btw. is there a better way to initialize a variable to zero other than 'IntOp $4 $4 & 0'?
Anders#
Originally Posted by svens View Post
Btw. is there a better way to initialize a variable to zero other than 'IntOp $4 $4 & 0'?
StrCpy $4 0