Allow only one installer instance: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Updated author and download links, and changed format of some pages.)
No edit summary
Line 13: Line 13:
</highlight-nsis>
</highlight-nsis>
'myMutex' should be replaced with a unique value.
'myMutex' should be replaced with a unique value.
== The advanced way ==
This code will bring the already running instance to front, instead of open a new one.
<highlight-nsis>
System::Call "kernel32::CreateMutexA(i 0, i 0, t '$(^Name)') i .r0 ?e"
Pop $0
StrCmp $0 0 launch
  StrLen $0 "$(^Name)"
loop:
  FindWindow $1 '#32770' '' 0 $1
  IntCmp $1 0 +4
  System::Call "user32.dll::GetWindowText(i r1, t .r2, i $0) i."
  StrCmp $2 "$(^Name)" 0 loop
  System::Call "user32::SetWindowPos(i r1, i -1, i 0, i 0, i 0, i 0, i 0x02|0x01) i."
  Abort
launch:
</highlight-nsis>


Page author: [[User:Vytautas|Vytautas]]
Page author: [[User:Vytautas|Vytautas]]

Revision as of 17:12, 23 April 2005

Description

The best way is to use a kernel mutex. You can call the Win32 API using the System plug-in. Put this code in the .onInit function:

The Script

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

'myMutex' should be replaced with a unique value.

The advanced way

This code will bring the already running instance to front, instead of open a new one.

 System::Call "kernel32::CreateMutexA(i 0, i 0, t '$(^Name)') i .r0 ?e"
 Pop $0
 StrCmp $0 0 launch
  StrLen $0 "$(^Name)"
 loop:
   FindWindow $1 '#32770' '' 0 $1
   IntCmp $1 0 +4
   System::Call "user32.dll::GetWindowText(i r1, t .r2, i $0) i."
   StrCmp $2 "$(^Name)" 0 loop
   System::Call "user32::SetWindowPos(i r1, i -1, i 0, i 0, i 0, i 0, i 0x02|0x01) i."
   Abort
 launch:

Page author: Vytautas