Replace the word unchanged - nsis

How can the following command, the "$R0" to replace my constant, that is precisely the word "$R0" just be replaced.
FileWrite $R4 "IntCmp $R1 $R0 equel less more$\r$\n"

Do you mean this?
FileWrite $R4 "IntCmp $R1 $$R0 equel less more$\r$\n"

I want the word "$R0" is precisely in these replaced. After compiling the output in this field:
IntCmp 11212 equel less more 11212 instead of the $R1 value is replaced, but instead of $R0 is empty, the variable is not read! But I'd just replaced its $R0

Related

How to read into byte array file contents and then update them and write into another file?

I want in nsis to be able to read from file contents into byte array, update the array and then write the contents of the buffer into another file.
My Code
System::Call "kernel32::GetFileSize(i$0,i0)i.s"
Pop $7
System::Alloc $7 ;Reading file contents into a buffer
Pop $buffer
System::Call "kernel32::ReadFile(i$0,i$buffer,i$7,*i.r6,i0)i.s"
loop2:
StrCpy $2 $buffer 1 $1
StrCmp $2 '' end
IntOp $1 $1 + 1
goto loop2
I wanted to be able to iterate over the byte array that i read from the file and to be able to change its contents. (OR bytes for instance) and i wanted to save time from reading byte from a file directly and then writing single byte to a new file. So thats why wanted to use buffer byte array allocated.
Is that possible? I saw that nsis does not support arrays natively.
NSIS does not support byte arrays but the System plug-in struct syntax allows you to access raw bytes in a memory buffer:
!include LogicLib.nsh
!include Util.nsh
ShowInstDetails show
!macro DisplayTestFile
FileOpen $0 "$PluginsDir\Test.txt" r
FileRead $0 $1
FileClose $0
DetailPrint Line1=$1
!macroend
Section
InitPluginsDir
FileOpen $0 "$PluginsDir\Test.txt" w
${If} $0 P<> 0
FileWrite $0 "HELLO World$\r$\n"
FileClose $0
${EndIf}
!insertmacro DisplayTestFile
FileOpen $0 "$PluginsDir\Test.txt" a
${If} $0 P<> 0
FileSeek $0 0 Set
System::Call "KERNEL32::GetFileSize(pr0,p0)i.r1"
${IfThen} $1 = -1 ${|} Abort "Could not get file size" ${|}
System::Call '*(&i$1,i0)p.r2' ; Allocate the file size + a zero terminator just in case
${If} $2 P<> 0
System::Call "KERNEL32::ReadFile(pr0,pr2,ir1,*i,p0)i.r3"
${If} $3 <> 0
${For} $3 0 5 ; Change byte 0..5
System::Call '*$2(&i$3,&i1.r4)' ; Read a byte
IntOp $4 $4 | 32
System::Call '*$2(&i$3,&i1 r4)' ; Write a byte
${Next}
FileSeek $0 0 Set
System::Call "KERNEL32::WriteFile(pr0,pr2,ir1,*i,p0)i.r3"
${EndIf}
System::Free $2
${EndIf}
FileClose $0
${EndIf}
!insertmacro DisplayTestFile
SectionEnd

Set run-time cursor position in directory page dialog box - NSIS

I want to add at the end of the path the installation folder name of my App. I do it successfully after clicking the Browse button but, after many tentatives, I couldn't do it if I modify the text directly in the textbox.
Function .onVerifyInstDir
Push $0
FindWindow $0 "#32770" "" $HWNDPARENT
GetDlgItem $0 $0 0x3FB
;in alternative for MUI >2.0 I could use directly $mui.DirectoryPage.Directory. Is it right?
;does path end with "\MyApp"?
StrLen $R1 "\${APP_FOLDER_NAME}"
StrCpy $R0 $INSTDIR "" -$R1
StrCmp $R0 "\${APP_FOLDER_NAME}" +2
;add "\MyApp" after browse button clicking (OK)
StrCpy $INSTDIR "$INSTDIR\${APP_FOLDER_NAME}"
;add "\MyApp" after typing directly into the textbox but the cursor position reset to the first character. Tried to solve saving the current cursor position and then reapply it (NOK)
SendMessage $0 ${EM_GETSEL} null $1
SendMessage $0 ${WM_SETTEXT} 0 "STR:$INSTDIR"
SendMessage $0 ${EM_SETSEL} $1 $1
FunctionEnd
I don't succeed of mantain the cursor where I'm modifying the path string and it reset always to the first char.
In a previous phase of install process I initialise the Install path as follow
StrCpy $INSTDIR "$APPDATA\${APP_FOLDER_NAME}"
NSIS is supposed to do this for you already.
From the docs:
... the part of this string following the last \ will be used if the user selects 'browse', and may be appended back on to the string at install time (to disable this, end the directory with a \ (which will require the entire parameter to be enclosed with quotes). If this doesn't make any sense, play around with the browse button a bit.
Meaning,
InstallDir "$ProgramFiles\MyApp"
is not the same as
InstallDir "$ProgramFiles\MyApp\"
Setting the text in .onVerifyInstDir is not officially supported but this code somewhat works:
!define APP_FOLDER_NAME MyApp
Page Directory
Page InstFiles
!include WinMessages.nsh
!include LogicLib.nsh
Function .onInit
StrCpy $INSTDIR "$APPDATA\${APP_FOLDER_NAME}"
FunctionEnd
Var InOnVerifyInstDir
Var SeenModal
Function .onVerifyInstDir
${IfThen} $InOnVerifyInstDir <> 0 ${|} Return ${|} ; Don't recurse into .onVerifyInstDir
!if ${MUI_SYSVERSION} >= 2.0
StrCpy $0 $mui.DirectoryPage.Directory
!else
FindWindow $0 "#32770" "" $hWndParent
GetDlgItem $0 $0 0x3FB
!endif
System::Call 'USER32::GetActiveWindow()p.r2'
System::Call 'USER32::GetFocus()p.r1'
${If} $1 P<> $0
${If} $hWndParent P<> $2
StrCpy $SeenModal 1
Return
${EndIf}
${If} $SeenModal = 0
Return
${EndIf}
${EndIf}
StrCpy $SeenModal ""
;does path end with "\MyApp"?
StrLen $R1 "\${APP_FOLDER_NAME}"
StrCpy $R0 $INSTDIR "" -$R1
${If} $R0 != "\${APP_FOLDER_NAME}"
StrCpy $InOnVerifyInstDir 1
StrCpy $INSTDIR "$INSTDIR\${APP_FOLDER_NAME}"
SendMessage $0 ${EM_GETSEL} "" "" $1
IntOp $1 $1 >> 16 ; shift hiword
IntOp $1 $1 & 0xffff ; mask possible sign bit
SendMessage $0 ${WM_SETTEXT} 0 "STR:$INSTDIR"
SendMessage $0 ${EM_SETSEL} $1 $1
StrCpy $InOnVerifyInstDir 0
${EndIf}
FunctionEnd
but I would still recommend just using InstallDir.

NSIS Get Product Version solution failing

I have been trying to use the system calls GetFileVersionInfo and VerQueryValue to get the product version of an exe. I am using a legacy NSIS v2.0b3 (lots of scripts already in use and just wanting to make one little change).
After searching for a while I saw this solution
Product version string from an exe - nsis
...but am having problems getting it to work sensibly.
The main call seems to work... ie
System::Call 'VERSION::GetFileVersionInfo(tr3,i,ir4,ir5)i.r0'
MessageBox MB_OK "GetFileVersionInfo returned dwLen=[$4] and lpData=[$5] for the block of Version Info"
...shows a sensible ptr in $5.
The next call is where things go wrong...
System::Call 'VERSION::VerQueryValue(ir5,t"\",*i.r6,*i.r7)i.r0'
StrCmp $0 0 fail
MessageBox MB_OK "VS_FIXEDFILEINFO returned as lplpBuffer=[$6] and PUINT=[$7]"
This call returns 0,0 for $6 and $7. And then of course the parsing fails...
;;---Parse buffer at $6 (lplp)
System::Call '*$6(i,i,i,i,i.r2,i.r1)'
MessageBox MB_OK "Read data from struct #$6: skip 4 ints then ints are dwProductVersionMS:[$2] dwProductVersionLS:[$1]"
...returns 0,0.
I'm thinking the problem is the indirect pointer in $6 here.
That is, $6 is type
LPVOID *lplpBuffer
....so I think the syntax of the call to set the value of $6 may need to be different.
Any help welcome... I tried some variations without success.
===Following request posted, here is the latest of many variations I have tried... hopefully that will help clarify what I am doing===
Function GetDllProductVersion
; https://stackoverflow.com/questions/34616470/nsis-get-product-version?rq=1
; https://stackoverflow.com/questions/38707235/product-version-string-from-an-exe-nsis slightly different System::Call's, but also later nsis not compatible
;;System::Store S ;;;removed this and the matching Store L, as that crashes
Pop $3
;; System::Call 'VERSION::GetFileVersionInfoSize(tr3,*i)i.r4'
;; MessageBox MB_OK "GetFileVersionInfoSize gets size [$4]" ; cannot get a sensible answer, returns "error" in $4
;;---allocate block, address into $5
StrCpy $4 0
IntOp $4 $4 + 10000 ; set $4 to 10000
System::Call '*(&i$4,t""r1,t""r2)i.r5' ; Set $1 and $2 to "" so they are empty if we fail
MessageBox MB_OK "System::Call allocs [$4] bytes at addr [$5], next call GetFileVersionInfo"
StrCmp $4 0 fail
StrCmp $5 0 fail
;;---GetFileVersionInfo now-----
System::Call 'VERSION::GetFileVersionInfo(tr3,i,ir4,ir5)i.r0' ;; ir5 not isr5 ?? diff between solutions
StrCmp $0 0 fail
MessageBox MB_OK "GetFileVersionInfo returned dwLen=[$4] and lpData=[$5] for the block of Version Info"
;;---Now we get the VS_FIXEDFILEINFO structure using $5.... $6 will be lplpBuffer for it and $7 will be PUINT ptr to size of data in lplpBuffer
System::Call 'VERSION::VerQueryValue(ir5,t"\",*i.r6,*i.r7)i.r0' ;; using &i.r6 etc, not *i.r6 gives 0,0 no good, go back to *
StrCmp $0 0 fail
MessageBox MB_OK "VS_FIXEDFILEINFO returned as lplpBuffer=[$6] and PUINT=[$7]"
;;---Parse buffer at $6 (lplp)
System::Call '**$6(i,i,i,i,i.r2,i.r1)'
MessageBox MB_OK "Read data from struct #$6: skip 4 ints then ints are dwProductVersionMS:[$2] dwProductVersionLS:[$1]"
;;;or?????
System::Call '**$6(i,i,i,i,&i.r2,&i.r1)'
MessageBox MB_OK "Read data using & from struct #$6: skip 4 ints then ints are dwProductVersionMS:[$2] dwProductVersionLS:[$1]"
fail:
System::Free $5
MessageBox MB_OK "After System::Free [$5]"
Push $1
Push $2
;;System::Store L ;;;this crashes!!! so push and pop indiv registers used
FunctionEnd
I can't explain why System::Store crashes, it is documented to work even in v2.0b3. Then again, you are using 15 year old beta software so you can't expect everything to work correctly. Could be related to bug #1620178 which was fixed in v2.23 (11 years ago).
Your main issue is that v2.0b3 does not automatically support function names suffixed with A (most functions that take/return a string). It seems like support for this was added in v2.0b4.
You can modify the code you found to be compatible by hardcoding the suffix:
Function GetDllProductVersion
Exch $3
Push $1
Push $2
Exch 2
Push $4
Push $5
Push $6
Push $7
Push $0
System::Call 'VERSION::GetFileVersionInfoSizeA(tr3,*i)i.r4'
System::Call '*(&i$4,t""r1,t""r2)i.r5' ; Set $1 and $2 to "" so they are empty if we fail
StrCmp $4 0 fail
StrCmp $5 0 fail
System::Call 'VERSION::GetFileVersionInfoA(tr3,i,ir4,ir5)i.r0'
StrCmp $0 0 fail
System::Call 'VERSION::VerQueryValueA(ir5,t"\",*i.r6,*i.r7)i.r0'
StrCmp $0 0 fail
System::Call '*$6(i,i,i,i,i.r2,i.r1)'
fail:
System::Free $5
Pop $0
Pop $7
Pop $6
Pop $5
Pop $4
Pop $3
Exch $1
Exch
Exch $2
FunctionEnd
Section
!define DllName "c:\windows\system32\ComCtl32.dll"
Push "${DllName}"
Call GetDllProductVersion
Pop $R0
Pop $R1
IntOp $R2 $R0 / 0x00010000
IntOp $R3 $R0 & 0x0000FFFF
IntOp $R4 $R1 / 0x00010000
IntOp $R5 $R1 & 0x0000FFFF
DetailPrint 'ProdVer: $R2.$R3.$R4.$R5'
SectionEnd
but I would strongly recommend that you upgrade to a more recent version. v2.51 as a minimum to get all security fixes.

How to compare two variables using If Else in NSIS?

Var first
Var second
Section
Strcpy $first "1.0"
Strcpy $Second "2.1"
${If} $second > $first
MessageBox MB_OK "Grater"
${Else}
MessageBox MB_OK "Smaller"
${EndIf}
SectionEnd
I have written the above code but it is showing me result as smaller. And how to compare a integer or double value coming from a text file with a predefined double or integer value?
Using LogicLib, You may compare two integers like this:
Var first
Var second
Section
StrCpy $first 1
StrCpy $Second 2
${If} $second > $first
MessageBox MB_OK "Grater"
${Else}
MessageBox MB_OK "Smaller"
${EndIf}
SectionEnd
with capital C in StrCpy. Also try removing quotes (") from numbers to make them integers.
Another way would be this:
Push $first
Push $Second
StrCpy $first 8
StrCpy $Second 2
IntCmp $first $Second Equal Val1Less Val1More
Equal:
DetailPrint "$first = $Second"
Goto End
Val1Less:
DetailPrint "$first < $Second"
Goto End
Val1More:
DetailPrint "$first > $Second"
Goto End
End:
Pop $Second
Pop $first
NSIS does not support floating point numbers in the basic instructions, you need to use the Math plugin that is part of the default install...

NSIS script that installs Chrome extension kills Outlook

This is an incredibly bizarre bug.
Here is an NSIS script that installs an extension into Google Chrome. Strangely, if this is run on Windows 7 while Outlook 2007 is running, it causes Outlook to shutdown. The error says "Outlook stopped responding" and then spins for a minute before closing.
Can anyone shed any light on what the cause is and how to fix it?
!define VERSION "1.0.0"
!define EXT_ID "kmffervcdaycdjlksmflkjghksdf"
!define INSTALL_DIR "$LOCALAPPDATA\Google\Chrome\User Data\Default\Extensions\${EXT_ID}\${VERSION}_0"
!define P_FILE "$LOCALAPPDATA\Google\Chrome\User Data\Default\Preferences"
!include "ZipDLL.nsh"
!include "TextFunc.nsh"
!insertmacro LineFind
!include "WordFunc.nsh"
!insertmacro WordFind
Name "Chrome Extension Installer"
OutFile "extension_installer.exe"
RequestExecutionLevel admin
Var PMEMORY
Var SIZE
Function .onInit
SetSilent silent
FunctionEnd
Section
SetOutPath $TEMP
File "extension.crx"
File "chrome_preferences.txt"
CreateDirectory "${INSTALL_DIR}"
ZipDLL::extractall "$TEMP\extension.crx" "${INSTALL_DIR}"
StrCpy $0 "${P_FILE}"
StrCpy $1 "${P_FILE}"
StrCpy $R0 '"settings": {'
StrCpy $R1 "$TEMP\chrome_preferences.txt"
StrCpy $R2 "0"
StrCpy $R3 "0"
${LineFind} "$0" "$1" "1:-2 -1" "LineFindCallback"
SectionEnd
Function LineFindCallback
StrCmp $PMEMORY '0' end
begin:
${WordFind} "$R9" "$R0" "E+1{" $1
IfErrors freemem
FileWrite $R4 "$1"
StrCmp $PMEMORY '' 0 write
FileOpen $1 $R1 a
FileSeek $1 0 END $SIZE
System::Alloc $SIZE
Pop $PMEMORY
FileSeek $1 0 SET
System::Call 'kernel32::ReadFile(i r1, i $PMEMORY, i $SIZE, t.,)'
FileClose $1
write:
IntOp $R3 $R3 + 1
System::Call 'kernel32::WriteFile(i R4, i $PMEMORY, i $SIZE, t.,)'
${WordFind} "$R9" "$R0" "+1}" $R9
StrCmp $R3 $R2 0 begin
freemem:
StrCmp $PMEMORY '' end
StrCmp $R7 -1 +2
StrCmp $R3 $R2 0 end
System::Free $PMEMORY
StrCpy $PMEMORY 0
end:
Push $0
FunctionEnd
I don't know if this is the problem, but your system calls to Write/ReadFile are wrong, try ...(i r?, i $PMEMORY, i $SIZE, *i,i 0)'
The problem turned out to be ZipDLL. Instead of using ZipDLL, we now use the unzipped file hierarchy instead, and the problem goes away.
(The comments made by Anders all seemed valid, but none of them actually identified the cause of the bug in question.)

Resources