Check if EXE is running and optionally terminate it
By Sergey - Posted on March 27th, 2008
The WMI Win32_Process class represents a process on an operating system.
Note 1 Under Terminal Services/Citrix the code will enumerate/terminate processes for all users. The GetOwner Method of the Win32_Process Class can be used to retrieve the user name and domain name under which the process is running and comapre with the current user/domain.
| This is sample code. Add error handling and adjust to your requirements as necessary. |
lcExeName = "notepad.exe" && Is EXE running ? IsExeRunning(lcExeName) ... && Terminate EXE if it's running ? IsExeRunning(lcExeName, .T.) ... RETURN FUNCTION IsExeRunning(tcName, tlTerminate) LOCAL loLocator, loWMI, loProcesses, loProcess, llIsRunning loLocator = CREATEOBJECT('WBEMScripting.SWBEMLocator') loWMI = loLocator.ConnectServer() loWMI.Security_.ImpersonationLevel = 3 && Impersonate loProcesses = loWMI.ExecQuery([SELECT * FROM Win32_Process WHERE Name = '] + tcName + [']) llIsRunning = .F. IF loProcesses.Count > 0 FOR EACH loProcess in loProcesses llIsRunning = .T. IF tlTerminate loProcess.Terminate(0) ENDIF ENDFOR ENDIF RETURN llIsRunning
Amazing :-)
Where do we find some more examples?
Google for 'wmi scripts'. It shouldn't be hard to convert those scripts into VFP code.
Check http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=...