Check if EXE is running and optionally terminate it

topic: 

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.

 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

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=B4CB2678-DAFB-4E30-B2DA-B8814FE2DA5A

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?

You can use FindWindow() to get a window handle and then send WM_CLOSE message to it
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#DEFINE WM_CLOSE 0x10
DECLARE Long FindWindow IN WIN32API String ClassName, String WindowTitle
DECLARE Long SendMessage IN WIN32API Long hWnd, Long Msg, Long wParam, Long lParam

lcWindowTitle = "..."

hWnd = FindWindow( Null, lcWindowTitle )
IF hWnd = 0
	? "Window not found"
	RETURN
ENDIF	
	
= SendMessage( hWnd, WM_CLOSE, 0, 0 )

You may create an exe in Autoit to close the application i.e. Adobe Reader/PDF file.
Then Call/Run the created exe from your VFP application

What about Terminate one instance of similar programs, e.g 2 notepad.exe, ist it possible?

You can use FindWindow() and WM_CLOSE message in this case as well

Thank you it works.

Sergey, thanks so much for posting this, easiest code snippet I've ever used. You are a steely eyed missle man. Again, thanks...

Sergey,

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.

Hi Sergey
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

Use the code to determine if application running. See how it can be launched at https://www.berezniker.com/content/pages/visual-foxpro/launching-windows-explorer-specified-drive-folder-or-file