Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

Check that a specific executable runs in Windows

I wrote a Delphi application, says myapp.exe that should preferrably test when starting that there is no other instance running and, if so, halt. In a first stage, I used a counter in the registry that I put on 1 when starting and 0 when terminating and I tested it at startup. But if for some reason the program aborts, it can't be started because the flag is on 1 and I do not want to ask the user something like 'it looks like this is already running, do you want to continue ?'. I prefer to detect the other really running. Is there some API do check this ?
0
Flupmans
Asked:
Flupmans
1 Solution
 
MotazCommented:
you can use FindWindow API by one of two methods:

1. You didn't know main application form class name such as TfmMain, and you are sure of the main application
caption such as (My Application Caption)

if FindWindow(nil, 'My Application Caption') > 0 then
  ShowMessage('Application is running')
else
  ShowMessage('Application is not running');

2. You know main form's class name:

if FindWindow('TfmMainApp', nil) > 0
  ShowMessage('Application is running')
else
  ShowMessage('Application is not running');

You should do that in an external application, for example a gaurd for your main application
Motaz
0
 
-Yoshi-Commented:
Since you could relay on that windows locks the executable for writing as soon as you start it the first time you could simply test that.. I'm not about the best way to do this but this is one way..

function IsOpen(AAppName: String): Boolean;
var
  FApp: TFileStream;
begin
  try
    FApp:= TFileStream.Create(AAppName, fmOpenWrite);
    Result:= False;
    FApp.Free;
  except
    Result:= True;
  end;
end;
0
 
-Yoshi-Commented:
Sorry.. didn't think.. you can't use that one on startup since it always will return true :D

In case you wanna use it to check other apps not that AAppName should be the name of the execuable such as 'myapp.exe'..
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
-Yoshi-Commented:
This should work though....


function TForm1.AlreadyOpen: Boolean;
var
  FTmpHandle: HWND;
  FTitle: String;
begin
  // Searching all next windows
  FTmpHandle:= GetNextWindow(Handle, GW_HWNDNEXT);
  while (GetNextWindow(FTmpHandle, GW_HWNDNEXT) <> 0) do
  begin
    FTmpHandle:= GetNextWindow(FTmpHandle, GW_HWNDNEXT);
    SetString(FTitle, PChar(nil), GetWindowTextLength(FTmpHandle));
    GetWindowText(FTmpHandle, PChar(FTitle), MaxInt);
    if (FTitle=Application.Title) then
    begin
      Result:= True;
      Exit;
    end;
  end;

  // Searching all previous windows
  FTmpHandle:= GetNextWindow(Handle, GW_HWNDPREV);
  while (GetNextWindow(FTmpHandle, GW_HWNDPREV) <> 0) do
  begin
    FTmpHandle:= GetNextWindow(FTmpHandle, GW_HWNDPREV);
    SetString(FTitle, PChar(nil), GetWindowTextLength(FTmpHandle));
    GetWindowText(FTmpHandle, PChar(FTitle), MaxInt);
    if (FTitle=Application.Title) then
    begin
      Result:= True;
      Exit;
    end;
  end;

  Result:= False;
end;
0
 
-Yoshi-Commented:
Had some fun rewriting the function... Enjoy!

function TForm1.AlreadyOpen: Boolean;
  function GetTitle(AHandle: HWND): String;
  begin
    SetString(Result, PChar(nil), GetWindowTextLength(AHandle));
    GetWindowText(AHandle, PChar(Result), MaxInt);
  end;

  function Search(AHandle: HWND; AFlag: Cardinal): Boolean;
  begin
    AHandle:= GetNextWindow(AHandle, AFlag);
    if (AHandle <> 0) then
      if (GetTitle(AHandle)=Application.Title) then
        Result:= True
      else
        Result:= Search(AHandle, AFlag)    { Recursive call }
    else
      Result:= False;
  end;
begin
  Result:= Search(GetNextWindow(Handle, GW_HWNDNEXT), GW_HWNDNEXT) or
           Search(GetNextWindow(Handle, GW_HWNDPREV), GW_HWNDPREV)
end;
0
 
FlupmansAuthor Commented:
For me indeed, checking that myapp.exe is locked is enough as I start it from another .exe
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now