views:

47

answers:

1

Hi,

I am using innosetup to create installation for my windows application. Before starting installation i need to check whether application is already running or not. I have used the following code, which is not working properly.

const
WM_CLOSE = 16;

Function InitializeSetup : Boolean;
var winHwnd: longint;
    retVal : boolean;
    strProg: string;
begin
  Result := true;
  try
    strProg := 'myApp.exe';
    winHwnd := FindWindowByWindowName(strProg);
    Log('winHwnd: ' + inttostr(winHwnd));
    if winHwnd <> 0 then
      retVal:=postmessage(winHwnd,WM_CLOSE,0,0);
      if retVal then begin
        MsgBox('Window is  not running', mbInformation, MB_OK);
        Result := True
        end
      else begin
       MsgBox('Window still open', mbInformation, MB_OK);
        Result := False;
 end;
  except
  end;

end;

Here winHwnd is always comes as 0. Please let me know what's wrong with this code.

Thanks, Manju

+1  A: 

InnoSetup has a built-in check to see if your application is running, look at the AppMutex setting in the [Setup] section. All you need to do is create a named mutex in your application, and specify the name of that mutex in your innosetup script. InnoSetup will then do the check and display a message itself.

Otherside
Thanks for the quick reply. My windows application is developed in .net, I am not sure how to create mutex from my application.So i thought i will use FindWindowByWindowName option. Thanks, Manju
manju
You can also easily create a named mutex in .NET see http://msdn.microsoft.com/en-us/library/aa332347%28VS.71%29.aspx Using a Mutex is also better for this kind of check since the named mutex will also be found if the installer is running in a different session than your application (for instance when using fast user switching.
Otherside
Thanks, Its working fine. I am using mutex to see whether application is running before starting installation. I am installing Database script, if it successful then i am proceeding with application installation. The problem is, i don't want "Cancel" should be show in the message box when application is in use.Only "OK" button should be shown. Is there is any way to do this.Thanks, Manju
manju