Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to terminate other windows process 1

Status
Not open for further replies.

aaraluce

Programmer
Oct 8, 2003
3
0
0
MX
Hi, I'm trying to make an app that kill other apps, I'm trying to use the Terminate process winapi but when I tried to kill for example the windows messenger, I kill the foxpro app, what i'm trying to do is read the windows app title and see if the user are running apps not permited in my company like chat apps, winamp, etc and kill the apps.
I'm manage to get the titles on the windows title bar and identify what i want but I don't know how to get the process id for the app that i want to kill and passes this id to the terminateprocess function.
Can someone help me please?
Thanks a lot
 
Some code that I picked up somewhere that Doug Hennig wrote will do what you want...

Code:
********************************
FUNCTION KillProcess
*==============================================================================
* Program:			KillProcess
* Purpose:			Terminate the specified application
* Author:			Doug Hennig
* Copyright:		(c) 2001 Stonefield Systems Group Inc.
* Last revision:	02/02/2001
* Parameters:		tcCaption - the caption for the application to terminate
* Returns:			.T. if it succeeded
* Environment in:	none
* Environment out:	if successful, the application has been terminated
*==============================================================================

lparameters tcCaption
local lnhWnd, ;
	llReturn, ;
	lnProcessID, ;
	lnHandle

* Declare the Win32API functions we need.

#define WM_DESTROY 0x0002
declare integer FindWindow in Win32API ;
	string @cClassName, string @cWindowName
declare integer SendMessage in Win32API ;
	integer hWnd, integer uMsg, integer wParam, integer lParam
declare Sleep in Win32API ;
	integer nMilliseconds
declare integer GetWindowThreadProcessId in Win32API ;
	integer hWnd, integer @lpdwProcessId
declare integer OpenProcess in Win32API ;
	integer dwDesiredAccess, integer bInheritHandle, integer dwProcessID
declare integer TerminateProcess in Win32API ;
	integer hProcess, integer uExitCode

* Get a handle to the window by its caption.

lnhWnd   = FindWindow(0, tcCaption)
llReturn = lnhWnd = 0

* If we found the window, send a "destroy" message to it, then wait for it to
* be gone. If it didn't, let's use the big hammer: we'll terminate its process.

if not llReturn
	SendMessage(lnhWnd, WM_DESTROY, 0, 0)
	llReturn = WaitForAppTermination(tcCaption)
	if not llReturn
		lnProcessID = 0
		GetWindowThreadProcessId(lnhWnd, @lnProcessID)
		lnHandle = OpenProcess(1, 1, lnProcessID)
		llReturn = TerminateProcess(lnHandle, 0) > 0
	endif not llReturn
endif not llReturn
return llReturn
ENDFUNC

* For up to five times, wait for a second, then see if the specified application
* is still running. Return .T. if the application has terminated.

********************************
FUNCTION WaitForAppTermination
********************************
lparameters tcCaption
local lnCounter, llReturn
m.lnCounter = 0
m.llReturn  = .F.
do while ! m.llReturn and lnCounter < 5
	Sleep(1000)
	m.lnCounter = lnCounter + 1
	m.llReturn  = FindWindow(0, m.tcCaption) = 0
ENDDO
return m.llReturn
ENDFUNC

boyd.gif

SweetPotato Software Website
My Blog
 
Thanks a lot for you help craigsboyd, this is very helpful
 
* You can use WMI, for example close all Calculator instances
oWMI = GETOBJECT('winmgmts://')
cQuery = "select * from win32_process where name='calc.exe'"
oResult = oWMI.ExecQuery(cQuery)
? oResult.Count
FOR EACH oProcess IN oResult
? oProcess.Name
oProcess.Terminate(0)
NEXT

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top