?
Solved

Check that a specific executable runs in Windows

Posted on 2003-02-22
7
Medium Priority
?
156 Views
Last Modified: 2010-04-04
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
Comment
Question by:Flupmans
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 7

Expert Comment

by:Motaz
ID: 7998431
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
 

Expert Comment

by:-Yoshi-
ID: 7998524
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
 

Accepted Solution

by:
-Yoshi- earned 200 total points
ID: 7998542
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
Technology Partners: 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!

 

Expert Comment

by:-Yoshi-
ID: 7998615
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
 

Expert Comment

by:-Yoshi-
ID: 7998662
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
 
LVL 34

Expert Comment

by:Slick812
ID: 7999706
0
 

Author Comment

by:Flupmans
ID: 8002349
For me indeed, checking that myapp.exe is locked is enough as I start it from another .exe
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question