*/***************************************************************************
*# is a window open to Window Operation system
*/Program : App_running
*/System : FoxPro Library
*/Purpose : Checks the Windows Operation System to see if a program is running by the Windows Name.
*/Syntax : logical = App_Running("WindowName",[version])
*/Returns : logical
*/Parameter : WindowName - String - All or part of the window name to look for
*/Defaults : none
*/Requires :
*/Changes :
*/Calls :
*/Version : 3.0
*/Dated : <<DATE>>
*/Written By: David W. Grewe
*/***************************************************************************
*&Application HAndling
*/***************************************************************************
*/ Record Of Change
*/ 1.0 Added Win32API call
*/ 2.0 Added bring Window to top of screen
*/ 3.0 Expanded change 2 so it was nore reliable
*/***************************************************************************
parameter pcWindName, pnVersion
*******************************************************************************
IF PARAMETERS()<1 OR VARTYPE(pcWindName)<>"C" OR EMPTY(pcWindName)
RETURN .f.
ENDIF
IF PARAMETERS()<2 OR VARTYPE(pnVersion)<>"N" OR EMPTY(pnVersion)
pnVersion = 1
ENDIF
*
DO CASE
CASE pnVersion = 1
SET LIBRARY TO SYS(2004)+"foxtools.fll" ADDITIVE
* FindWindow() takes two parameters and returns the handle of the window, HWND, if it was found.
GetWind = RegFn("FindWindow", "CC", "I")
*Set the first parameter of the call to getwind to 0, null.
RETURN IIF(CallFn(GetWind , 0 , pcWindName)> 0 , .t. , .f.)
*
CASE pnVersion = 2
declare integer BringWindowToTop in Win32API integer lnWnd
declare integer GetActiveWindow in Win32API
declare integer GetWindow in Win32API integer lnWnd, integer lnType
declare integer GetWindowText in Win32API integer lnWnd, string @lcText, integer lnType
lnNext = GetActiveWindow() && current app's window
DO WHILE lnNext <> 0
lcText = REPLICATE(CHR(0) , 80)
= GetWindowText(lnNext , @lcText , 80) && get window title
IF UPPER(ALLTRIM(pcWindName)) $ UPPER(lcText)
= BringWindowToTop(lnNext)
RETURN .t.
ENDIF
lnNext = GetWindow(lnNext , 2) && next window
ENDDO
RETURN .f.
*
CASE pnVersion = 3
IF Single_Instance_App_By_Mutex('pcWindName')
QUIT
ENDIF
RETURN .f.
*
OTHERWISE
DECLARE INTEGER FindWindow IN Win32API String @wClass, String @WinName
RETURN IIF(FindWindow("pcWindName") > 0 , .T. , .F.)
*
ENDCASE
*
*
*
****************************************************************
FUNCTION Single_Instance_App_By_Mutex
****************************************************************
*Descrip: This function only allows one instance of an app to be run at a time
*Returns: (T/F)Whether another instance of the app is running
LPARAMETERS tcMutexName
*parameter listing
*tcMutexName - Name of Apps assigned mutex
*API Functions
declare integer IsIconic in win32api integer
declare integer SetForegroundWindow in win32api integer
declare integer GetForegroundWindow in win32api
declare integer SetProp in win32api integer,string @,integer
declare integer showwindow in win32api integer,integer
LOCAL lhAppWnd
LOCAL llQuit
*get the apps window handle
lhAppWnd = Find_App_By_Mutex(tcMutexName)
IF lhAppWnd <> -1
IF lhAppWnd <> GetForegroundWindow() && necessary for testing only
llQuit=.t.
ENDIF
IF IsIconic(lhAppWnd)>0
showwindow(lhAppWnd,3) && activate the window and show it
ENDIF
SetForegroundWindow(lhAppWnd)
ELSE
* this assumes our application window is currently active
lhAppWnd=GetForegroundWindow()
* To help identify the application window we are adding
* a property to it - which we can later search for in Find_App_By_Mutex
SetProp(lhAppWnd,@tcMutexName,1)
ENDIF && lhAppWnd <> -1
RETURN llQuit
ENDFUNC
*
*
*
****************************************************************
FUNCTION Find_App_By_Mutex
****************************************************************
LPARAMETERS tcMutexName
*Descrip: This function locates the app window based on the mutex
*Returns: The apps window handle, -1 if not found
*paramter listing
*tcMutexName - Name of apps assigned mutex
*API Function
declare integer GetWindow in win32api integer,integer
declare integer GetDesktopWindow in win32api
declare integer GetProp in win32api integer,string @
*variable declaration
LOCAL lhWnd,lhFoundWnd
lhFoundWnd = -1
IF Set_Mutex(tcMutexName) = -2
* Mutex Already Exist - locate the original application
lhWnd=GetDesktopWindow() && handle to the desktop
lhWnd=GetWindow(lhWnd,5) && get the first child of the desktop
DO WHILE lhWnd>0 AND lhFoundWnd = -1 && loop until we find the window or run out of windows
IF GetProp(lhWnd,@tcMutexName)==1
lhFoundWnd = lhWnd && set the flag
ENDIF
lhWnd=GetWindow(lhWnd,2) && get handle to the next child of the desktop
ENDDO
ENDIF
RETURN lhFoundWnd
ENDFUNC
*
*
*
****************************************************************
FUNCTION Set_Mutex
****************************************************************
*Descrip: This function attempts to set the mutex for the current app
*Returns: -2 if mutex already exists
* -1 if set_mutex fails
* 0 if successful
*
LPARAMETERS tcMutexName
*parameter listing
*tcMutexName - Name of apps assigned mutex
*
*API Functions
declare integer CreateMutex in win32api integer,integer,string @
declare integer GetLastError in win32api
declare integer CloseHandle in win32api integer
*
tcMutexName = tcMutexName+chr(0) && must be null terminated
lhMutex=CreateMutex(0,1,@tcMutexName)
*
DO CASE
CASE lhMutex = .null.
* CreateMutex failed
lhMutex = -1
CASE GetLastError()=183
* Mutex Already Exist
lhMutex = -2
CloseHandle(lhMutex) && close the new handle
OTHERWISE
* Mutex was created
lhMutex = 0
ENDCASE
RETURN lhMutex
ENDFUNC