Click here to Skip to main content
15,893,487 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Any cryptography library in Visual C++? Pin
Keith Worden11-Mar-10 6:49
Keith Worden11-Mar-10 6:49 
QuestionMFC and DLL initialization - is this approach correct? Pin
Vaclav_11-Mar-10 4:31
Vaclav_11-Mar-10 4:31 
AnswerRe: MFC and DLL initialization - is this approach correct? [modified] Pin
Eugen Podsypalnikov11-Mar-10 4:56
Eugen Podsypalnikov11-Mar-10 4:56 
QuestionKill child process Pin
Pryabu10-Mar-10 23:13
Pryabu10-Mar-10 23:13 
AnswerRe: Kill child process Pin
Naveen10-Mar-10 23:15
Naveen10-Mar-10 23:15 
AnswerRe: Kill child process Pin
Eugen Podsypalnikov10-Mar-10 23:16
Eugen Podsypalnikov10-Mar-10 23:16 
AnswerRe: Kill child process Pin
Stephen Hewitt11-Mar-10 3:31
Stephen Hewitt11-Mar-10 3:31 
AnswerRe: Kill child process Pin
Stephen Hewitt11-Mar-10 4:59
Stephen Hewitt11-Mar-10 4:59 
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;
}


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

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <sstream>

using namespace std;

int main(int argc, char* argv[])
{
	// Get the parent's HANDLE from the command line.
	void* val;
	istringstream iss(argv[1]);
	iss >> val;
	HANDLE hParent = reinterpret_cast<HANDLE>(val);

	cout << "Child got HANDLE " << val << ". Waiting..." << endl;

	// Wait for the parent.
	WaitForSingleObject(hParent, INFINITE);

	// The parent is history. Clean up and exit.
	CloseHandle(hParent);

	return 0;
}
Steve

QuestionUrgent help required to Do something in standby mode for windows Mobile. Pin
Rahul Vaishnav10-Mar-10 22:28
Rahul Vaishnav10-Mar-10 22:28 
AnswerRe: Urgent help required to Do something in standby mode for windows Mobile. Pin
Rajesh R Subramanian10-Mar-10 22:56
professionalRajesh R Subramanian10-Mar-10 22:56 
Questionending a running program Pin
tamar8210-Mar-10 22:13
tamar8210-Mar-10 22:13 
AnswerRe: ending a running program Pin
«_Superman_»10-Mar-10 22:19
professional«_Superman_»10-Mar-10 22:19 
AnswerRe: ending a running program Pin
Eugen Podsypalnikov10-Mar-10 22:20
Eugen Podsypalnikov10-Mar-10 22:20 
QuestionRe: ending a running program Pin
CPallini10-Mar-10 22:35
mveCPallini10-Mar-10 22:35 
AnswerRe: ending a running program Pin
Rajesh R Subramanian10-Mar-10 22:55
professionalRajesh R Subramanian10-Mar-10 22:55 
QuestionCSpinButtonCtrl message handling to know up or down button is clicked Pin
A&Ms10-Mar-10 21:48
A&Ms10-Mar-10 21:48 
AnswerRe: CSpinButtonCtrl message handling to know up or down button is clicked Pin
Eugen Podsypalnikov10-Mar-10 22:06
Eugen Podsypalnikov10-Mar-10 22:06 
QuestionRe: CSpinButtonCtrl message handling to know up or down button is clicked Pin
A&Ms10-Mar-10 22:47
A&Ms10-Mar-10 22:47 
AnswerRe: CSpinButtonCtrl message handling to know up or down button is clicked [modified] Pin
Eugen Podsypalnikov10-Mar-10 23:02
Eugen Podsypalnikov10-Mar-10 23:02 
QuestionFourCC Pin
gmallax10-Mar-10 21:30
gmallax10-Mar-10 21:30 
AnswerRe: FourCC Pin
R@jeev K R10-Mar-10 21:42
R@jeev K R10-Mar-10 21:42 
GeneralRe: FourCC Pin
Cool_Dev10-Mar-10 21:52
Cool_Dev10-Mar-10 21:52 
GeneralRe: FourCC Pin
gmallax10-Mar-10 23:02
gmallax10-Mar-10 23:02 
GeneralRe: FourCC Pin
gmallax10-Mar-10 21:52
gmallax10-Mar-10 21:52 
GeneralRe: FourCC Pin
R@jeev K R10-Mar-10 22:05
R@jeev K R10-Mar-10 22:05 

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.