Archive: Bug in "Allow only one installer instance" sample?


Bug in "Allow only one installer instance" sample?
  One of the examples on the NSIS home page is Allow only one installer instance. This contains the following code snippet:

System::Call 'kernel32::CreateMutexA(i 0, i 0, t "myMutex") i .r1 ?e'

Pop $R0

StrCmp $R0 0 +3
MessageBox MB_OK "The installer is already running."
Abort
Seems to me like this code contains errors. The output of the system call is saved in $1, not in $R0 (which is later used for Pop and StrCmp). Am I right, or am I missing something?

The code is correct. The "?e" flag makes System push the error code returned by GetLastError on the stack. If GetLastError returns ERROR_ALREADY_EXISTS, $R0 won't be 0 and the message box will pop-up. $1 contains the handle to the mutex which is of no interest to the script.


Thanks for the quick response. This definitely explains why the second sample snippet uses $1 instead of $R0 (as that example does need the handle).

However, having only one installer running, when I use snippet 1 in my code, my installer claims there is another installer running, while when I use snippet 2, it doesn't. This would mean that GetLastError does return an error code. I'll search some more for the cause of that.

edit:
After some more study, I've realised that the first and second sample are identical, at least to the point where I'm using them: both put the output of the CreateMutex call in some variable (but this does not get used, so my observation above, now italic, is incorrect), both push the GetLastError value on the stack, pop it in a variable and test if it is 0. So, now I'm even more puzzled as to why snippet 1 does not and snippet 2 does produce the correct outcome. My search continues...


This works


  ;create mutex

System
::Call 'kernel32::CreateMutexA(i 0, i 0, t "myMutex") i .r1 ?e'
Pop $R0
StrCmp $R0 0+3
MessageBox MB_OK|MB_ICONEXCLAMATION "The installer is already running."
Abort
>

Thanks, indeed that works.

I've got a non-reproducable error here. Code that have worked before resulted in an error, but only one machine. On other machines, it worked fine. Doesn't seem to be a NSIS-issue.


specs?


We have re-tested it today on the same computer, but it didn't reproduce the error. We consider it a non-issue. Thanks for your help anyway.


-I attempted to use this code (last example in this thread) in my rather large installer (approx. 80MB) script, but it made the installer spend a whole lot of time extracting all the files on initialisation (I use solid lzma compression), which with all my files took a lot of time(it counted from 0 to 100%).

As soon as I removed this code, the resulting installer opened immediately, no files extracted.

-What file would I have to include/Reserve in order to avoid the lengthy file extraction on startup? Kernel32.dll?

I have .onInit on top of my list of Functions, btw.


The code only needs to extract the System plug-in, so you need to reserve just that. You can also move .onInit to the top of the script.