Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Kill child process

Kill child process

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pryabu
    wrote on last edited by
    #1

    Hi all, How to close the child process when parent process crashes? thanks,

    N E S 4 Replies Last reply
    0
    • P Pryabu

      Hi all, How to close the child process when parent process crashes? thanks,

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      You can add a crash handler in the parent process and in that function you can notify the child process to exit. to install a crash handler check the API's AddVectoredExceptionHandler or SetUnhandledExceptionFilter

      nave [My Articles] [My Blog]

      1 Reply Last reply
      0
      • P Pryabu

        Hi all, How to close the child process when parent process crashes? thanks,

        E Offline
        E Offline
        Eugen Podsypalnikov
        wrote on last edited by
        #3

        It could be performed in the CATCH_ALL section of the parent process :)

        virtual void BeHappy() = 0;

        1 Reply Last reply
        0
        • P Pryabu

          Hi all, How to close the child process when parent process crashes? thanks,

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          Didn't you already ask[^] this question?

          Steve

          1 Reply Last reply
          0
          • P Pryabu

            Hi all, How to close the child process when parent process crashes? thanks,

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            Here's a demonstration of the technique I described last time you asked this question.

            // Parent.cpp : Defines the entry point for the console application.
            //

            #include "stdafx.h"
            #include <windows.h>
            #include <conio.h>
            #include <memory.h>
            #include <iostream>
            #include <string>
            #include <sstream>

            using namespace std;

            HANDLE GetMyHandle()
            {
            // Get a pseudohandle to the current process.
            HANDLE hPseudo = GetCurrentProcess();

            // Create a "real" HANDLE from the pseudohandle and make it inheritable.
            HANDLE hMe;
            DuplicateHandle(
            		hPseudo,		// handle to the source process
            		hPseudo,		// handle to duplicate
            		hPseudo,		// handle to process to duplicate to
            		&hMe,			// pointer to duplicate handle
            		SYNCHRONIZE,	// access for duplicate handle
            		TRUE,			// handle inheritance flag
            		0				// optional actions
            		);
            
            return hMe;
            

            }

            int main(int argc, char* argv[])
            {
            cout << "Parent process..." << endl;

            HANDLE hMe = GetMyHandle(); // The child will wait on this...
            
            // Build the path to the child.
            char me\[MAX\_PATH\];
            GetModuleFileName(NULL, me, sizeof(me));
            string child = me;
            child = child.substr(0, child.find\_last\_of('\\\\'));
            child += "\\\\Child.exe";
            
            // Finish off the command line with the out process HANDLE.
            ostringstream oss;
            oss << "\\"" << child << "\\" " << reinterpret\_cast<void\*>(hMe);
            string cmd = oss.str();
            
            // Create a mutable copy of the command line.
            char \*pCmd = new char\[cmd.length()+1\];
            memcpy(pCmd, cmd.c\_str(), cmd.length()+1);
            
            // Start the child.
            PROCESS\_INFORMATION pi;
            STARTUPINFO si = {0};
            si.cb = sizeof(si);
            si.dwFlags = STARTF\_USESHOWWINDOW;
            si.wShowWindow = SW\_NORMAL;
            CreateProcess(
            	NULL,	// LPCTSTR lpApplicationName
            	pCmd,	// LPTSTR lpCommandLine
            	NULL,	// LPSECURITY\_ATTRIBUTES lpProcessAttributes
            	NULL,	// LPSECURITY\_ATTRIBUTES lpThreadAttributes
            	TRUE,	// BOOL bInheritHandles
            	CREATE\_DEFAULT\_ERROR\_MODE | CREATE\_NEW\_CONSOLE,	// DWORD dwCreationFlags
            	NULL,	// LPVOID lpEnvironment
            	NULL,	// LPCTSTR lpCurrentDirectory
            	&si,	// LPSTARTUPINFO lpStartupInfo
            	&pi		// LPPROCESS\_INFORMATION lpProcessInformation
            	);
            // Close the HANDLEs created by CreateProcess which we don't use.
            CloseHandle(pi.hProcess);
            CloseHandle(pi.hThread);
            
            // Clean up.
            CloseHandle(hMe);
            delete \[\] pCmd;
            
            cout << "Press any key to crash parent..." << endl;
            \_getch();
            \*(char\*)0 = 0;
            
            return 0;
            

            }<

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups