0

I'm reading a file's content into a buffer using

System::Alloc $Size
pop $Buffer
System::Call "Kernel32::ReadFile(i r0, i $Buffer, i $Size, t.,)"

But I can't figure out how to read from (or write into) $Buffer.

Is there a way do to so (preferably natively, but any suggestion would be appreciated).

Thanks

P.S.
I know about System::Copy but it only lets you write into a buffer and only from another buffer
(trying System::Copy 1 $Buffer "A" crashes the executable)

2
  • And by natively do you mean built-in NSIS commands or calling some WinAPI function?
    – Anders
    May 9, 2014 at 22:23
  • What are you trying to read? Text or raw bytes?
    – Anders
    May 9, 2014 at 22:26

1 Answer 1

2

The built-in NSIS functions FileRead/FileReadUTF16LE and FileReadByte can be used to read text and bytes but you can also call Windows functions directly. To read from a memory buffer you have to use the system struct syntax:

Section
InitPluginsDir
FileOpen $0 "$pluginsdir\test.txt" a

DetailPrint "NSIS:"
FileWrite $0 "Foo"
FileSeek $0 0 SET
FileRead $0 $1
DetailPrint |$1|


DetailPrint "System::Call:"
System::Alloc 100
Pop $1
System::Call '*$1(&m3 "Bar")' ; Write ASCII text into buffer using struct syntax
System::Call 'kernel32::WriteFile(i$0,i$1,i3,*i.r2,i0)i.r9'
DetailPrint "Write: OK=$9 ($2 bytes)"
System::Free $1
System::Call '*(&i100)i.r1' ; Alloc a 100 byte buffer using struct syntax
FileSeek $0 0 SET
System::Call 'kernel32::ReadFile(i$0,i$1,i6,*i.r2,i0)i.r9'
DetailPrint "Read: OK=$9 ($2 bytes)"
System::Call '*$1(&m${NSIS_MAX_STRLEN}.r2)' ; Read ASCII text into variable using struct syntax
System::Free $1
DetailPrint |$2|

FileClose $0
SectionEnd
1
  • Thanks, greatly appreciated.
    – Yuval
    May 10, 2014 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.