Archive: Wiki-code for only one instance doesn’t work?


Wiki-code for only one instance doesn’t work?
Hey guys,

I came across the problem within my NSIS-script which happens whenever the user launches the (un)installer twice.
Thanks to the Wiki, I found the solution (http://nsis.sourceforge.net/Allow_on...aller_instance), but even the “even more advanced” example

BringToFront

# Check if already running
# If so don't open another but bring to front
System::Call "kernel32::CreateMutexA(i 0, i 0, t '$(^Name)') i .r0 ?e"
Pop $0
StrCmp $0 0 launch
StrLen $0 "$(^Name)"
IntOp $0 $0 + 1
loop:
FindWindow $1 '#32770' '' 0 $1
IntCmp $1 0 +3
System::Call "user32::GetWindowText(i r1, t .r2, i r0) i."
StrCmp $2 "$(^Name)" 0 loop
System::Call "user32::ShowWindow(i r1, i 9) i." ; If minimized then maximize
System::Call "user32::SetForegroundWindow(i r1) i." ; Bring to front
Abort
launch:
doesn’t work — no matter if I modify “IntCmp $1 0 +5” to “+4” (or even “+3”) or not.

By “doesn’t work”, I mean that there’s only _one_ installer launched, but that one _never_ gets brought to the front or maximized (when previously minimized, of course).

The test bed consists of Vista Business x64 with myself having administrative rights, several entries at the taskbar and HM NIS Edit 2.0.3 as the IDE.

If somebody knows something: please, share your knowledge and/or ideas! :)

/Martin..

the code worked fine when I tested (XP SP2), but I would make a couple of changes:


StrCpy $1 "" ;make sure we start at the top of the z-order
BringToFront
# Check if already running
# If so don't open another but bring to front
System::Call "kernel32::CreateMutexA(i 0, i 0, t '$(^Name)') i .r0 ?e"
Pop $0
StrCmp $0 0 launch
StrLen $0 "$(^Name)"
IntOp $0 $0 + 1
loop:
FindWindow $1 '#32770' '' 0 $1
StrCmp $1 0 +3
System::Call "user32::GetWindowText(i r1, t .r2, i r0) i."
StrCmp $2 "$(^Name)" 0 loop
System::Call "user32::ShowWindow(i r1, i 9) i." ; If minimized then maximize
System::Call "user32::SetForegroundWindow(i r1) i." ; Bring to front
Abort
launch:


if there is no code before this in .oninit, you don't need to reset $1

There could be several reasons why it does not work, if you set a custom caption or something like that so it can't find the window. Try adding MessageBox mb_ok "$1" after the StrCmp $2 "$(^Name)" 0 loop line and make sure its not 0.

The caption remains something like “My-App 1.2.3.4” throughout the whole time.. :)

Regarding the reset, I added that line, although “BringToFront” is the very first command after “Function .onInit” — just to make sure.. ;)

Though, the message box does indeed show “0”.. :(

[-EDIT-]
Okay, I probably found the reason — and it’s almost a shame I didn’t notice it earlier.. :rolleyes:

The search focuses on what is specified via “Name "foobar"”, but Name and Caption aren’t the same within my script..
After fixing that issue, everything worked as expected! :)

So, thank you _very_ much for pointing at the right spot! ;)

/Martin


BringToFront does not modify $1, so you don't need to reset $1

and “My-App 1.2.3.4” is the same as the string you used in the name instruction? Try switching out StrCmp $2 "$(^Name)" 0 loop
with StrCmp $2 "My-App 1.2.3.4" 0 loop


Thanks for that hint! ;)

Regarding your suggestion: as I said in the “[-EDIT-]”-paragraph, it was not — but now it is and everything works _very_ well! :)