Click here to Skip to main content
12,826,820 members (40,177 online)
Click here to Skip to main content
Add your own
alternative version


1 download
97 bookmarked
Posted 22 Nov 1999

Creating a process, and then killing it

, 22 Nov 1999
Rate this:
Please Sign up or sign in to vote.
Creating a process, and then killing it.


While I was creating an app that has the capability of starting a process, and then be able to kill it later on, I looked all over the net, and found some crazy ways of doing this. Searching, searching, searching, for a couple days, I did nothing but try to locate info on setting this up correctly (Since I never tried doing this before). Most of them I could not get to work, in their entirety, as they were written. I finally found a way that worked, without too much mish-mash. What is here, is a conglomerate of all. And needless to say, the simplest way was the key. I am sure there are better ways of doing this, but this way works for this application.

Please note that some of the techniques used in this article are based on the article Sending a message to the Main Frame Window of Another Application, given only the Process Handle by Martin-Pierre Frenette.

The basic method used is to start a process, store its process ID, and then when it comes time to kill the process, you enumerate all open windows and close the window that is associated with your process ID.

TerminateProcess() is easier, but doesn't unload the DLLs from memory, which means it doesn't let the DLLs know the process is terminated, and leaves them floating around in memory, lost, causing problems. It should only be used in worst-case situations. system(), WinExec(), ShellExecute(), and ShellExecueEx() are easier, but you don't get as much information about the process being started. Since CreateProcess() is very powerful, and seems rather intimidating at first, I decided to write this little article.

PostMessage() needs a window handle to send WM_CLOSE, so we have to use EnumWindows() to ramble through the windows, checking to see if our PID is the same. If it is, we have a match, and have the window handle to send it.

In CSomeDlg.h

static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
DWORD processPid;

In CSomeDlg.cpp

void CSomeDlg::startProcess()
  BOOL bWorked;
  CString m_Process = "C:\\wherever\\process\\we\\want\\to\\start.exe";
  char *vip =  "C:\\wherever\\process\\we\\want\\to\\start.exe " 
                    "whatever command line arguments here";

  memset (&suInfo, 0, sizeof(suInfo));
  suInfo.cb = sizeof(suInfo);

  bWorked = ::CreateProcess(m_Process,
             vip,      // can also be NULL

procInfo has these members
    HANDLE hProcess;   // process handle
    HANDLE hThread;    // primary thread handle
    DWORD dwProcessId; // process PID
    DWORD dwThreadId;  // thread ID

  if (procInfo.dwThreadId = NULL)

  playerPid = procInfo.dwProcessId;

void CSomeDlg::killProcess()
                                        FALSE, processPid);
  // processPid = procInfo.dwProcessId;

  // This function enumerates all widows,
  // using the EnumWindowsProc callback
  // function, passing the PID of the process
  // you started earlier.
  EnumWindows(EnumWindowsProc, processPid);

  CloseHandle(ps) ;

BOOL CALLBACK CSomeDlg::EnumWindowsProc(HWND hwnd, LPARAM lParam)
  DWORD wndPid;
  CString Title;
  // lParam = procInfo.dwProcessId;

  // This gets the windows handle and pid of enumerated window.
  GetWindowThreadProcessId(hwnd, &wndPid);

  // This gets the windows title text
  // from the window, using the window handle
  CWnd::FromHandle( hwnd )->GetWindowText(Title);

  //  this makes sure that the PID matches that PID we started, and window
  // text exists, before we kill it . I don't think this is really needed, 
  // I included it because some apps have more than one window.
  if ( wndPid == (DWORD)lParam && Title.GetLength() != 0)
    //  Please kindly close this process
    ::PostMessage(hwnd, WM_CLOSE, 0, 0);
    return false;
    // Keep enumerating
    return true;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer Nokia Pty Ltd
Australia Australia
No Biography provided

You may also be interested in...


Comments and Discussions

Generalprocess creation Pin
Member 425070523-Dec-08 0:29
memberMember 425070523-Dec-08 0:29 
Generalprocess Pin
prathuraj9-Apr-07 1:04
memberprathuraj9-Apr-07 1:04 
GeneralProcess in a Thread Pin
Gagan komal16-Nov-06 22:20
memberGagan komal16-Nov-06 22:20 
GeneralGreat help Pin
ellasunxl13-Jun-06 17:20
memberellasunxl13-Jun-06 17:20 
GeneralRe: Great help Pin
tifen27-Jul-09 18:22
membertifen27-Jul-09 18:22 
GeneralProblem in retreving the HANDLE of the window Pin
Raj241129-Mar-06 0:12
memberRaj241129-Mar-06 0:12 
QuestionApplication? Pin
Rodolfo Brazil29-Mar-05 7:09
memberRodolfo Brazil29-Mar-05 7:09 
GeneralLinking error Pin
NTSC12-Nov-03 14:20
memberNTSC12-Nov-03 14:20 
GeneralRe: Linking error Pin
Agus Subhan Akbar26-May-05 21:40
memberAgus Subhan Akbar26-May-05 21:40 
GeneralPassing instruction to the screen Pin
vgandhi13-Aug-03 14:42
membervgandhi13-Aug-03 14:42 
Generala simple question I hope Pin
Gregor Schiller1-Feb-03 10:14
memberGregor Schiller1-Feb-03 10:14 
GeneralRe: a simple question I hope Pin
ljp1-Feb-03 10:35
memberljp1-Feb-03 10:35 
QuestionUsing this in Visual c++? Pin
microcyb26-Dec-02 7:27
membermicrocyb26-Dec-02 7:27 
AnswerRe: Using this in Visual c++? Pin
Anonymous7-Aug-03 4:01
sussAnonymous7-Aug-03 4:01 
GeneralhProcess.. Pin
Preetham26-Apr-02 15:01
memberPreetham26-Apr-02 15:01 
GeneralRe: hProcess.. Pin
Anonymous2-May-02 17:28
memberAnonymous2-May-02 17:28 
QuestionNot working? Pin
Conphused19-Mar-02 13:11
memberConphused19-Mar-02 13:11 
AnswerRe: Not working? Pin
Rob maddison27-Jan-03 3:51
sussRob maddison27-Jan-03 3:51 
GeneralplayerPid = procInfo.dwProcessId; Pin
virle23-Feb-02 20:17
membervirle23-Feb-02 20:17 
GeneralRe: playerPid = procInfo.dwProcessId; Pin
kidconan11-Aug-05 21:45
memberkidconan11-Aug-05 21:45 
Questionhow Can I start "service program"? Pin
Ahnse28-Jun-01 18:04
memberAhnse28-Jun-01 18:04 
AnswerRe: how Can I start "service program"? Pin
cjc2-May-02 17:30
membercjc2-May-02 17:30 
GeneralError Pin
Gunter Blasek15-May-01 4:34
memberGunter Blasek15-May-01 4:34 
GeneralKilling a process Pin
H.K. Subbu7-Aug-00 22:11
sussH.K. Subbu7-Aug-00 22:11 
GeneralA much different direction. Pin
nerddude18-Oct-03 20:38
membernerddude18-Oct-03 20:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170326.1 | Last Updated 23 Nov 1999
Article Copyright 1999 by ljp
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid