main logo
Subject: RE: Where Do Files Go When Sent to the Vista Windows Folder?
Author: "Christof Wollenhaupt"
Posted: 2007/10/31 00:54:30
 
View Entire Thread
New Search


Hi Ken,

> I've never seen an example of code that could find a running process and
terminate it without relying on the window title search.

If you store _VFP.ProcessID at program start in some file, you can use the
following code to terminate the process. Just replace _VFP.ProcessID with
the handle read from that file:

Local lnHandle
Declare Long OpenProcess in Win32API ;
Long dwDesiredAccess, ;
Long bInheritHandle, ;
Long dwProcessId
Declare TerminateProcess in Win32API ;
Long hProcess, ;
Long uExitCode
lnHandle = OpenProcess( 1, 0, _VFP.ProcessID )
TerminateProcess( m.lnHandle, 1 )

If you need to do this based on the EXE name on all platforms including
Windows NT 4, it's a little bit more work. Here's an article with C++ code
to do this:

http://www.codeproject.com/threads/killprocess.asp

You need to slightly modify this as it uses a callback function. Here's my
code of one part that is in there that creates the list of all running
processes. It uses my struct class
(http://www.foxpert.com/download/struct.zip). This code doesn't work on
Windows NT 4 and Windows 3.x. You can modify it to also return the process
ID in the array.

*=====================================================
* AProcess.PRG
*
* Returns a list of all running EXE files
*=====================================================
LParameter tacProcess

External Array tacProcess

*--------------------------------------------------
* Declare WIN-API functions
*--------------------------------------------------
Declare Integer CreateToolhelp32Snapshot In win32api Integer,
Integer
Declare Integer CloseHandle In WIn32Api Integer
Declare Integer Process32First In WIn32APi Integer, String@
Declare Integer Process32Next In WIn32APi Integer, String (AT)

*--------------------------------------------------
* load struct component
*--------------------------------------------------
Local oProcessEntry
Set Class to Struct D.O.T VCX Additive
oProcessEntry = CreateObject("PROCESSENTRY32")

*--------------------------------------------------
* get processes
*--------------------------------------------------
Local lnHandle, lnCount, lnRet, lcString, lcStringEmpty
lnCount = 0
lnHandle = CreateToolhelp32Snapshot(2,0)
lcStringEmpty = oProcessEntry.GetString()
If lnHandle > 0
lcString = m.lcStringEmpty
lnRet = Process32First(lnHandle,@lcString)
oProcessEntry.SetString(lcString)
Do While lnRet > 0
lnCount = lnCount+1
Dimension tacProcess[lnCount]
tacProcess[lnCount] = oProcessEntry.cExeFile
lcString = m.lcStringEmpty
lnRet = Process32Next(lnHandle, (AT) lcString)
oProcessEntry D.O.T SetString(lcString)
Enddo
CloseHandle(lnHandle)
Endif

*--------------------------------------------------
* cleanup
*--------------------------------------------------
oProcessEntry.Release

Return lnCount

*-----------------------------------------------------
* PROCESSENTRY32 Struktur
*-----------------------------------------------------
Define Class PROCESSENTRY32 as Struct
nSize = 296
nCntUsage = 0
nProcessID = 0
nDefaultHeap = 0
nModuleID = 0
nCntThreads = 0
nParentProcessID = 0
nPriClassBase = 0
nFlags = 0
cExeFile = ""
cMembers = "l:nSize,l:nCntUsage,l:nProcessId,"+;
"l:nDefaultHeap,l:nModuleID,l:nCntThreads,"+;
"l:nParentProcessID,L:nPriClassbase,"+;
"l:nFlags,0c260:cExeFile"
Enddefine


--
Christof




 
©2007 Christof Wollenhaupt
<-- Prior Message New Search Next Message -->