Showing posts with label foxpro check running process terminate end task program WBEMScripting SWBEMLocator. Show all posts
Showing posts with label foxpro check running process terminate end task program WBEMScripting SWBEMLocator. Show all posts

Thursday, September 29, 2011

Check and optionally end a process

Method 1

In the Main program of the project, this method check the executable program name in the window process.


LPARAMETERS xExe, xTerminate
Private xLocator, xWMI, xProcesses, xProcess, xIsRunning


xLocator = CREATEOBJECT('WBEMScripting.SWBEMLocator')
xWMI     = xLocator.ConnectServer()
xWMI.Security_.ImpersonationLevel = 3


xProcesses = xWMI.ExecQuery([SELECT * FROM Win32_Process WHERE Name = '] + xExe + ['])
xIsRunning = .F.
if xProcesses.Count > 0
For EACH xProcess IN xProcesses
xIsRunning = .T.
if xTerminate
xProcess.Terminate(0)
ENDIF
NEXT
ENDIF
Return xIsRunning



Method 2
In the Main program of the project, this method checks the title of the window instead of the process name.



Private xHandle, xTitle
Application.Visible = .F.
Set Exclusive ON


Declare Integer FindWindow  In Win32API String,  String
Declare Integer IsIconic    In Win32API Integer
Declare Integer ShowWindow  In user32   Integer, Integer
Declare SetForegroundWindow IN WIN32API Integer


xTitle = 'Home Budget'


xHandle = FindWindow(null, xTitle)
if xHandle <> 0
if IsIconic (xHandle) = 1
ShowWindow(xHandle, 4)
ELSE
DO FORM main
ENDIF
SetForegroundWindow(xHandle)
ELSE
OPEN DATABASE Budget
DO FORM MAIN
Read Events
ENDIF