Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
 From: Jun Tangunan
  Where is Jun Tangunan?
 Cabanatuan
 Philippines
 Jun Tangunan
 To: Pete Sass
  Where is Pete Sass?
 Marathon, Ontario
 Canada
 Pete Sass
 Tags
Subject: RE: Kill EXE API
Thread ID: 416387 Message ID: 416396 # Views: 63 # Ratings: 1
Version: Visual FoxPro 9 SP2 Category: Win32API programming
Date: Monday, December 29, 2014 7:13:26 AM         
   


> >
> >
KillProcess("YourApp.exe")
> > 
> > **************
> > Function KillProcess("cProcess")
> > **************
> > Local loWMI, lcQuery, loResult, loProcess
> > loWMI = Getobject("winmgmts://")
> > lcQuery = '"select * from win32_process where name='+"'"+Alltrim(m.cProcess)+"'"+ ' "'
> > lcSQL=&lcQuery
> > loResult = loWMI.ExecQuery(m.lcSQL)
> > If loResult.Count > 0
> > 	For Each loProcess In loResult
> > 		loProcess.Terminate(0)
> > 	Next
> > Endif
> > Endfunc

> >
> >
> > https://vfpx.codeplex.com/releases/view/99045
> > http://sandstorm36.blogspot.com
>
>
> Hi Jun,
>
> I cannot use WMI have to use API calls.
>
> Pete "the IceMan", from the Great White North of Canada.
> www.marathongriffincomputers.com

Combining codes from Carlos Alloatti's sample and with TerminateProcess, check if this works for you. This works on me but I need to close and open VFP so it will work again on my end when I am testing to terminate a process. I believe I am still missing something:

KillProcessAPI('winrar.exe')

**************
Procedure KillProcessAPI(cApplication)
***********
Local lcApplication, nSize, lnBytesCopied, lcBuffer, lnResult, lcProcessArray, lnPID, ;
	lnPH, lnLoop

lcApplication = Upper(Alltrim(m.cApplication))

Declare Integer EnumProcesses In psapi.Dll As EnumProcesses ;
	String  @lpidProcess,;
	Integer cb,;
	Integer @cbNeeded,;

Declare Integer OpenProcess In win32api  ;
	Integer dwDesiredAccess,;
	Integer bInheritHandle,;
	Integer dwProcessId

Declare Integer GetModuleFileNameEx In psapi.Dll ;
	Integer hProcess,;
	Integer hModule,;
	String  @lpFilename,;
	Integer nSize,;

Declare Integer CloseHandle In win32api ;
	Integer hObject

Declare SHORT GetExitCodeProcess In WIN32API ;
	INTEGER hProcess, ;
	@Integer lpdwExitCode

Declare TerminateProcess In WIN32API ;
	INTEGER hProcess, ;
	INTEGER fuExitCode

nSize = 4096 * 4
lnBytesCopied = 0
lcBuffer = Replicate(Chr(0), m.nSize)
lnResult = EnumProcesses(@m.lcBuffer,  m.nSize, @m.lnBytesCopied)
lcProcessArray = Left(m.lcBuffer, m.lnBytesCopied)

For lnLoop = 1 To m.lnBytesCopied - 4
	lnPID = CToBin(Substr(m.lcProcessArray, m.lnLoop, 4), "4rs")
	lnPH = OpenProcess(2035711, 0, m.lnPID)
	nSize = 260
	lcBuffer = Replicate(Chr(0), m.nSize)
	lnResult = GetModuleFileNameEx(m.lnPH, 0, @m.lcBuffer, m.nSize)
	If m.lcApplication = Upper(Justfname(Strtran(Left(m.lcBuffer, m.lnResult), "\??\", "")))
		lnExitCode = 0
		GetExitCodeProcess(m.lnPH, @lnExitCode)
		TerminateProcess(m.lnPH,m.lnExitCode)
	Endif
	CloseHandle(m.lnPH)
Next
Clear Dlls 
Endproc


https://vfpx.codeplex.com/releases/view/99045
http://sandstorm36.blogspot.com

ENTIRE THREAD

Kill EXE API Posted by Pete Sass @ 12/29/2014 1:40:46 AM
RE: Kill EXE API Posted by Chuanbing Chen @ 12/29/2014 1:55:13 AM
RE: Kill EXE API Posted by Pete Sass @ 12/29/2014 2:09:06 AM
RE: Kill EXE API Posted by Jun Tangunan @ 12/29/2014 2:21:49 AM
RE: Kill EXE API Posted by Pete Sass @ 12/29/2014 3:11:45 AM
RE: Kill EXE API Posted by Jun Tangunan @ 12/29/2014 7:13:26 AM
RE: Kill EXE API Posted by Mike Gagnon @ 12/29/2014 3:12:39 AM
RE: Kill EXE API Posted by Jun Tangunan @ 12/29/2014 7:16:19 AM
RE: Kill EXE API Posted by Pete Sass @ 12/29/2014 12:07:30 PM
RE: Kill EXE API Posted by Mike Gagnon @ 12/29/2014 12:28:36 PM
RE: Kill EXE API Posted by Pete Sass @ 12/29/2014 1:46:26 PM
RE: Kill EXE API Posted by Greg Green @ 12/29/2014 3:44:35 AM
RE: Kill EXE API Posted by Pete Sass @ 12/29/2014 1:22:22 PM
RE: Kill EXE API Posted by Jun Tangunan @ 12/30/2014 1:30:04 AM
RE: Kill EXE API Posted by Mike Gagnon @ 12/30/2014 5:48:41 PM
RE: Kill EXE API Posted by Pete Sass @ 12/30/2014 6:20:59 PM
RE: Kill EXE API Posted by Victor Espina @ 12/29/2014 3:38:47 PM