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 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
Thanks!
Amazing :-)
Where do we find some more examples?
Google for 'wmi scripts'
Google for 'wmi scripts'. It shouldn't be hard to convert those scripts into VFP code.
TechNet Script Center Sample Scripts download
Check http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=...
What if I just want open files in the application be closed?
I have a VFP application that creates a PDF and launches Adobe Reader to view the created PDF. There are situations when I need to close a PDF already opened with Adobe Reader because the PDF is a file I need to recreate. Simply closing the Adobe Reader is not the best approach because it triggers an error whenever the PDF reader is also in use by an internet browser.
So, how do you close a file currently opened in an application?
Closing a Window
You can use FindWindow() to get a window handle and then send WM_CLOSE message to it
Terminate one instance of similar programs
What about Terminate one instance of similar programs, e.g 2 notepad.exe, ist it possible?
Terminate one instance of similar programs
You can use FindWindow() and WM_CLOSE message in this case as well
thanks
Thank you it works.