Check if EXE is running and optionally terminate it
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 Classcan 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. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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
Comments
Thanks!
Where do we find some more examples?
Google for 'wmi scripts'
TechNet Script Center Sample Scripts download
What if I just want open files in the application be closed?
So, how do you close a file currently opened in an application?
Closing a Window
You may create an exe in
Then Call/Run the created exe from your VFP application
Terminate one instance of similar programs
Terminate one instance of similar programs
thanks
You are a great man
Check if EXE is running and optionally terminate it
This may be just what I need - I want to close WMPlayer - but I don't know what name to use. Is it possible anyway?
I want to delete the MP3 file after it has played but wmplayer has a stranglehold on it. I've modified your code to list all processes - wmiprvse.exe seemed probable but after terminating it I still can't erase the file.
if NOT running open
I cameacross this after I posted my problem in UT. This is exactly what I was looking for - brilliant0!!! I am looking for opposite action however.
How would I modify this code to read:
if notepad NOT running, OPEN notepad.
thanks
Re: if NOT running open