Kill child process
C / C++ / MFC
5
Posts
4
Posters
0
Views
1
Watching
-
It could be performed in the
CATCH_ALL
section of the parent process :)virtual void BeHappy() = 0;
-
Didn't you already ask[^] this question?
Steve
-
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;
}<