Talk:Check whether your application is running: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Reverted edits by 199.168.142.166 to last version by Kichik)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Hello...
Sometimes, the Window class or title are not unique.
Then, you have to loop over the found handles and check the window titles one by one.
You cannot use the message <code>WM_GETTEXT</code> to retrieve the window title, because it does not return a window title if the window has been created by another process than the owner of the current process. In this case, you have to use <code>GetWindowText</code>. [1]


First of all Thanks for the plug in.
Example:
I want to check my application is running or not when user runs installer.The plug in worked for me
 
But today i found a strange bug in this plug in..
Suppose my executable name is MyApp.exe and when I use the plugin to find MyApp.exe is running or not. It was okay. But the problem if another application MyApp Daily.exe is running the the FindProcess API suceeds.
 
Thanks,
Harikrishnan
 
 
----
 
I found the following to be much more useful for situations like this or if the title of your window is not static:
 
'''1) Add the KillProc.dll (found here: [http://nsis.sourceforge.net/KillProc_plug-in]) to your nsis plug-ins folder'''
 
 
'''2) Paste the following macro in your script:'''
<highlight-nsis>
!macro CheckAppRunning APP_PROCESS_NAME
  StrCpy $0 "${APP_PROCESS_NAME}"
  DetailPrint "Searching for processes called '$0'"
  KillProc::FindProcesses
  StrCmp $1 "-1" wooops
  Goto AppRunning
 
  AppRunning:
    DetailPrint "-> Found $0 processes running"
    StrCmp $0 "0" AppNotRunning
    MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "${APP_PROCESS_NAME} is currently running. $\n$\nDo you want to close it and continue installation?" IDYES KillApp
    DetailPrint "Installation Aborted!"
    Abort
  KillApp:
    StrCpy $0 "${APP_PROCESS_NAME}"
    DetailPrint "Killing all processes called '$0'"
    KillProc::KillProcesses
    StrCmp $1 "-1" wooops
    DetailPrint "-> Killed $0 processes, failed to kill $1 processes"
    sleep 1500
    Goto AppNotRunning
  wooops:
    DetailPrint "-> Error: Something went wrong :-("
    Abort
  AppNotRunning:
    DetailPrint "No Application Instances Found"
!macroend
</highlight-nsis>
 
'''3) In the OnInit function of the installer add the following code:'''
<highlight-nsis>
<highlight-nsis>
;Check if app is running
loop:
  !insertmacro CheckAppRunning "MyApp.exe"
  ; find first windows of class "SunAwtFrame" and store handle in $2
  FindWindow $2 "SunAwtFrame" "" 0 $1
  ; If nothing is found skip following check
  IntCmp $2 0 nothingFound
      ; try to retrieve the window title of window $2, store in $3
      System::Call /NOUNLOAD 'user32::GetWindowText(i r2, t .r3, i ${NSIS_MAX_STRLEN})'
      ; Copy the first 6 characters of $3 in $4
      StrCpy $4 $3 6
      ; Compare the 6 characters with "Matlab"
      StrCmp $4 "Matlab" foundMatlab loop
foundMatlab:
  MessageBox MB_OK "Found Matlab window, handle=$2, title=$3"
nothingFound:
</highlight-nsis>
</highlight-nsis>


'''4) Done!'''
[1] http://msdn.microsoft.com/en-us/library/aa928060.aspx
 
Cheers,
-Ross
22:57, 6 January 2009 (UTC)

Latest revision as of 20:36, 8 December 2011

Sometimes, the Window class or title are not unique. Then, you have to loop over the found handles and check the window titles one by one. You cannot use the message WM_GETTEXT to retrieve the window title, because it does not return a window title if the window has been created by another process than the owner of the current process. In this case, you have to use GetWindowText. [1]

Example:

loop:
   ; find first windows of class "SunAwtFrame" and store handle in $2
   FindWindow $2 "SunAwtFrame" "" 0 $1
   ; If nothing is found skip following check
   IntCmp $2 0 nothingFound
      ; try to retrieve the window title of window $2, store in $3
      System::Call /NOUNLOAD 'user32::GetWindowText(i r2, t .r3, i ${NSIS_MAX_STRLEN})'
      ; Copy the first 6 characters of $3 in $4
      StrCpy $4 $3 6
      ; Compare the 6 characters with "Matlab"
      StrCmp $4 "Matlab" foundMatlab loop
foundMatlab:
   MessageBox MB_OK "Found Matlab window, handle=$2, title=$3"
nothingFound:

[1] http://msdn.microsoft.com/en-us/library/aa928060.aspx