Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
End a process in Win2K/WinXP
Message
General information
Forum:
Visual FoxPro
Category:
Windows API functions
Miscellaneous
Thread ID:
00708380
Message ID:
00708483
Views:
16
Here is a way to end another process, even if you don't have
a windows title or handle. This is basically what you did from
the task manager.

Download "Enumerate Processes" File# 9651 from the files area
lparameters cModuleName  && Full path of the process
*-- termiproc.prg
*--
*-- Ex. Do termiproc with "C:\WINNT\SYSTEM32\NOTEPAD.EXE"

#define PROCESS_TERMINATE         (0x0001)

declare INTEGER OpenProcList IN enumproc.dll
declare INTEGER GetFirstProc  IN enumproc.dll ;
        INTEGER hhandle, INTEGER@ lpdwprocid, SHORT@ lpwprocid16, STRING@ lpzprocname
declare INTEGER GetNextProc  IN enumproc.dll ;
        INTEGER hhandle, INTEGER@ lpdwprocid, SHORT@ lpwprocid16, STRING@ lpzprocname
declare INTEGER CloseProcList IN enumproc.dll ;
        INTEGER hHandle

declare INTEGER OpenProcess IN WIN32API INTEGER access, INTEGER inher, INTEGER procid
declare INTEGER TerminateProcess IN WIN32API INTEGER prochnd, INTEGER exitcode

Local cprocname, nprocid, nprocid16, nhandle, nprochnd, lfound

If Type("cModuleName")#"C" .or. EMPTY(cModuleName)
   ? "Must pass the full path of the process to terminate..."
Endif

cprocname = Replicate(chr(0), 260)
nprocid = 0
nprocid16 = 0
lfound = .f.

nHandle = OpenProcList()
If nHandle = 0
   ? 'Error getting procedure list...'
   Return
Endif

If GetFirstProc(nHandle, @nprocid, @nprocid16, @cprocname) != 0
   If upper(left(cprocname, at(chr(0), cprocname)-1))== upper(cModuleName)
      lFound = .t.
   Else
      Do while GetNextProc(nHandle, @nprocid, @nprocid16, @cprocname) != 0
         If upper(left(cprocname, at(chr(0), cprocname)-1))== upper(cModuleName)
            lFound = .t.
            Exit
         Endif
      Enddo
   Endif
Else
   ? 'Error getting first process...'
Endif

CloseProcList(nHandle)

If lFound
   nprochnd = OpenProcess(PROCESS_TERMINATE, 0, nprocid)
   TerminateProcess(nprochnd, 0)
Else
   ? "Process name not found..."
Endif

clear dlls

Return
>I am using an ActiveX control in my program that doesn't terminate properly when released. It's not a VFP problem - the same thing happens using the control under VB6 using the vendor's own sample files. So far no help from them other than that they designed it this way. I CAN end it by going into Task Manager and ending the process. What I want to do is incorporate this in my program so that when my program ends it also kills this rogue task. Which API call should be used for this, and can it be used under VFP?
>
> Thanks,
> Randy

Just an opinion... Not a fact.
Previous
Reply
Map
View

Click here to load this message in the networking platform