Consultancy and Programming
Advice, tips, techniques and downloads for Visual Foxpro developers.
In this article, we will show you a generic function which determines if a given application is running on the user's system. Why would you want to know that? We can think three situations in which this might be useful - you will probably come up with some others of your own:
In all three cases, the IsRunning() function, shown below, can help you out. You pass this function a parameter containing part or all of the window title of the application you want to check. If the application is running, the function will return a positive integer, otherwise it will return zero. The positive integer is in fact a window handle which you can subsequently use in calls to Windows API functions if you wish.
As an example, suppose you want to launch the Windows Calculator, but only if it is not already running. If it is running, you will simply bring it to the front (that is, you will activate it). Here's how you could do that:
* Declare the BringWindowToTop() function * (this is a Windows API call) DECLARE INTEGER BringWindowToTop IN Win32API ; INTEGER hWnd lnHand = IsRunning("Calculator") IF lnHand = 0 * Calculator not already running, * so launch it now RUN /n calc.exe ELSE * Calculator is running, so activate it BringWindowToTop(lnHand) ENDIF
BringWindowToTop() is a Windows API function which brings to the front of the screen the window whose handle you pass as a parameter. As with all API functions, you need to declare it before calling it. That's what the above DECLARE statement does.
Now let's look at the code for the IsRuning() function. The function uses three API calls, so you need to declare these before the function is executed for the first time. To do so, place the following statements at the top of your application, or at any other suitable point in your code:
DECLARE INTEGER GetActiveWindow ; IN Win32API DECLARE INTEGER GetWindow IN Win32API ; INTEGER hWnd, INTEGER nType DECLARE INTEGER GetWindowText IN Win32API ; INTEGER hWnd, STRING @cText, INTEGER nType
The function itself is listed below. You can paste this into your normal procedure library or any suitable PRG file.
FUNCTION IsRunning * Generic routine to check if a given * application is running on the user's system. * Parameter is all or part of the window's title. LPARAMETER tcTitle hNext = GetActiveWindow() && current app's window * iterate through the open windows DO WHILE hNext<>0 cText = REPLICATE(CHR(0),80) GetWindowText(hNext,@cText,80) && get window title IF UPPER(ALLTRIM(tcTitle)) $ UPPER(cText) * parameter text is present in window title RETURN hNext ENDIF hNext = GetWindow(hNext,2) && next window ENDDO * required window not found RETURN 0 ENDFUNC
And that's all there is to it. We have found many uses for this handy bit of code, and we hope you will to.
Mike Lewis Consultants Ltd. March 2000.
FoxStuff is maintained by Mike Lewis Consultants Ltd. as a service to the VFP community. Feel free to download and use any code or components, and to pass around copies of the articles (but please do not remove our copyright notices or disclaimers).
The information given on this site has been carefully checked and is believed to be correct, but no legal liability can be accepted for its use. Do not use code, components or techniques unless you are satisfied that they will work correctly in your applications.
© Copyright Mike Lewis Consultants Ltd.