********************************
FUNCTION KillProcess
*==============================================================================
* Program: KillProcess
* Purpose: Terminate the specified application
* Author: Doug Hennig
* Copyright: (c) 2001 Stonefield Systems Group Inc.
* Last revision: 02/02/2001
* Parameters: tcCaption - the caption for the application to terminate
* Returns: .T. if it succeeded
* Environment in: none
* Environment out: if successful, the application has been terminated
*==============================================================================
lparameters tcCaption
local lnhWnd, ;
llReturn, ;
lnProcessID, ;
lnHandle
* Declare the Win32API functions we need.
#define WM_DESTROY 0x0002
declare integer FindWindow in Win32API ;
string @cClassName, string @cWindowName
declare integer SendMessage in Win32API ;
integer hWnd, integer uMsg, integer wParam, integer lParam
declare Sleep in Win32API ;
integer nMilliseconds
declare integer GetWindowThreadProcessId in Win32API ;
integer hWnd, integer @lpdwProcessId
declare integer OpenProcess in Win32API ;
integer dwDesiredAccess, integer bInheritHandle, integer dwProcessID
declare integer TerminateProcess in Win32API ;
integer hProcess, integer uExitCode
* Get a handle to the window by its caption.
lnhWnd = FindWindow(0, tcCaption)
llReturn = lnhWnd = 0
* If we found the window, send a "destroy" message to it, then wait for it to
* be gone. If it didn't, let's use the big hammer: we'll terminate its process.
if not llReturn
SendMessage(lnhWnd, WM_DESTROY, 0, 0)
llReturn = WaitForAppTermination(tcCaption)
if not llReturn
lnProcessID = 0
GetWindowThreadProcessId(lnhWnd, @lnProcessID)
lnHandle = OpenProcess(1, 1, lnProcessID)
llReturn = TerminateProcess(lnHandle, 0) > 0
endif not llReturn
endif not llReturn
return llReturn
ENDFUNC
* For up to five times, wait for a second, then see if the specified application
* is still running. Return .T. if the application has terminated.
********************************
FUNCTION WaitForAppTermination
********************************
lparameters tcCaption
local lnCounter, llReturn
m.lnCounter = 0
m.llReturn = .F.
do while ! m.llReturn and lnCounter < 5
Sleep(1000)
m.lnCounter = lnCounter + 1
m.llReturn = FindWindow(0, m.tcCaption) = 0
ENDDO
return m.llReturn
ENDFUNC