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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Terminating Process

Status
Not open for further replies.

adholioshake

Programmer
Feb 9, 2003
136
I have a problem with terminating 2 processes. The program I have wrote so far is as follows:
Code:
#include "stdafx.h"

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  HANDLE proc1;
  HANDLE proc2;
  // Open processes...
  proc1 = OpenProcess(PROCESS_TERMINATE, TRUE, 0x3c4);
  proc2 = OpenProcess(PROCESS_TERMINATE, TRUE, 0x494);
  // ...then Kill them (!)
  TerminateProcess(proc1, 1);
  TerminateProcess(proc2, 1);
  return(0);
}
This program works when the process number is known. I want to find out the name of the process to check whether to terminate it with the 'EnumProcesses' function scanning through all processes. This is because the process number can change, but the name of the process will not. Can anyone help me here ??

Thanks, Adonai.
 
'0x3c4' and '0x494' are the hex values for the processes I want to destroy at that particular instant. These values I copied from a process viewer I have with MS Visual C++, but because these values change for each new process I have to recompile all the time. To stop the need for recompiling, I have to be able to compare the name of the process, which is where I have come a bit stuck. I have researched a lttle on Google, and found 'Pdh...' functions might be handy, though I don't know how to use these. Any suggestions (or code) ?

Thanx.
 
The processes that are running... are they programs you written, or are they someone else's programs?

If you wrote the process, you might want to use a named event to signal the processes to kill themselves. TerminateProcess is a very dangerous API, from MSDN:

"The TerminateProcess function is used to unconditionally cause a process to exit. Use it only in extreme circumstances. The state of global data maintained by dynamic-link libraries (DLLs) may be compromised if TerminateProcess is used rather than ExitProcess."

Or If someone else wrote the process, you might want to use FindWindow to get a handle to the application main window (assuming they run in a window) and then send WM_CLOSE to them.

I REALLY hope that helps.
Will
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top