Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Any way to find out if my EXE is alreardy running progammatically? 3

Status
Not open for further replies.

Charterhouse

Programmer
Sep 25, 2006
12
0
0
GB
Is there anyway to find out whether an EXE is already running programmatically? My users are prone to starting the EXE (once it is already running and minimised on the taskbar). Although the creation of a phyical flag file maybe a solution, it is prone other problems)

Anyway of querying the Windows Task Manager to see if the EXE is already running (via Windows API)?
 
There are several ways of doing this. The approach I take is as follows.

Start by making the application invisible, and setting the caption of your main window to something other than the name of the application - it doesn't matter what.

Next, loop through all the open windows on the user's desktop to see if a running application has the same name as your application. If it is, quit. If not, change the window name to that of your application, and make the application visible.

To loop through all the open windows, you need to make some API calls. You can find more details, including an example, in my article Find out what applications are running on the user's system.

Hope this helps.

Mike




__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Charterhouse,

As Mike says, there are lots of ways of doing this. See for instance faq184-839.

I'm sure others will chip in with their suggestions, but I use this code, which uses some of the ideas that Mike outlined, to check whether the exe is already running:
Code:
LOCAL ;
  PreviousSafety , ; 
  RetVal , ;
  hNext , ;
  cText , ;
  lnTextLen , ;
  QQ___QQ && a placeholder for declaring local variables with F6 macro

RetVal = .f.
DECLARE INTEGER GetActiveWindow IN WIN32API && returns a handle to the active window
DECLARE INTEGER GetWindow IN Win32API ;
  INTEGER hWnd, INTEGER nType
DECLARE INTEGER GetWindowText IN Win32API ;
  INTEGER hWnd, STRING @cText, INTEGER nBuffSize && returns title bar of a window according to the window handle (hWnd) passed
DECLARE INTEGER BringWindowToTop IN Win32API ;
  INTEGER hWnd
hNext = GetActiveWindow()    && get current top-most window
DO WHILE hNext<>0 
  cText = REPLICATE(CHR(0),80)
  lnTextLen = GetWindowText(hNext,@cText,80) && passing cText by reference
  IF lnTextLen > 0 AND LEFT(cText, 15) = [Foundation Main]
    BringWindowtoTop(hNext)
    RetVal = .t.
    EXIT
  ENDIF
  hNext = GetWindow(hNext,2)
ENDDO
RETURN RetVal

This is looking at the caption in the title bar of the exe (in this case Foundation Main) and if it finds it, brings it to the top.

I call this routine as the first line in the main program in my application like this:
Code:
IF FindFoundation()
  CANCEL
ENDIF
By calling this before the application has a window, there is no need to change the caption.


Hope that helps,

Stewart
PS If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Code:
*** SomeWhere in you Main Program:
my_app_href = 0

IF _VFP.StartMode # 0 .AND.;
   IsRunning("MyApplicationNameOrSomeOtherUniqueString")
   MessageBox("You already have the application started",16,"Error")
   QUIT
ENDIF





FUNCTION IsRunning(tcSemaphoreName)
    LOCAL lpszSemName
    #Define ERROR_ALREADY_EXISTS 183
    Declare Integer GetLastError In win32API
    Declare Integer CreateSemaphore In WIN32API ;
            string @ lpSemaphoreAttributes, ;
            LONG lInitialCount, ;
            LONG lMaximumCount, ;
            string @ lpName

    my_app_href = CreateSemaphore(0,0,1,UPPER(tcSemaphoreName))

RETURN (my_app_href # 0 .AND.;
        GetLastError() == ERROR_ALREADY_EXISTS)



Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Thanks Guys. Just what I wanted!! Code implemented. EXE compiled & copy to network ready for 150 users to get when they next logon
 
Message for Mike Lewis:

Your code related to "finding out if an EXE is already running" on works well if the 1st instance of the EXE is maximised or restored down, however, if the 1st instance of the EXE has been minimised the BringWindowToTop(FileHandle) doesn't show the window.

Is there another API call which first maximises the window so that the BringWindowToTop(FileHandle) can then display. Tried SetActiveWindow(FileHandle) and searched for more API's.
 
Charterhouse,

f the 1st instance of the EXE has been minimised the BringWindowToTop(FileHandle) doesn't show the window.

That's a good point.

Instead of BringWindowToTop(), you can use ShowWindow(). This takes two parameters: the window handle, and the window state. For example, if the second param is 1, the window will be normal (not maximised or minimised).

Here is the declaration:

Code:
DECLARE INTEGER ShowWindow IN Win32API ;
  INTEGER hWnd, INTEGER CmdShow

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Mike,

I'm finding that ShowWindow doesn't return a minimised window to the "normal" state. It selects it in the taskbar, but that's all.

Stewart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top