Click here to Skip to main content
15,917,328 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to close other processes? Pin
Moochie515-Oct-04 6:27
Moochie515-Oct-04 6:27 
GeneralRe: How to close other processes? Pin
Andrzej Markowski15-Oct-04 7:04
Andrzej Markowski15-Oct-04 7:04 
GeneralRe: How to close other processes? Pin
Moochie515-Oct-04 8:14
Moochie515-Oct-04 8:14 
GeneralRe: How to close other processes? Pin
Andrzej Markowski15-Oct-04 9:32
Andrzej Markowski15-Oct-04 9:32 
GeneralRe: How to close other processes? Pin
Moochie515-Oct-04 10:53
Moochie515-Oct-04 10:53 
GeneralRe: How to close other processes? Pin
Moochie516-Oct-04 9:46
Moochie516-Oct-04 9:46 
GeneralRe: How to close other processes? Pin
Moochie516-Oct-04 10:01
Moochie516-Oct-04 10:01 
GeneralRe: How to close other processes? Pin
Andrzej Markowski17-Oct-04 11:54
Andrzej Markowski17-Oct-04 11:54 
Your program has to save the ThreadId for each new created process. Later the ThreadId is passed as the parameter to the PostThreadMessage function which is called to close the program the user opened. The modified example saves this information in the rp array of RUNNING_PROCESS_INFO structures. The nRPCounter specifies a number of currently running processes and also is the next avaliable index in the rp table. When the process is created a space for the RUNNING_PROCESS_INFO structure is allocated at the and of the array and the nRPCounter is incremented. When the user closes the program the counter is decremented and the structures in the array located below deleted item are moved one item up.
#include stdio.h
#include process.h
#include iostream.h
#include time.h
#include windows.h

typedef struct 
{
	DWORD dwThreadId;
	BYTE bProcessType; // 1 - FreeCell 2 - MineSweeper 3 - Paint
} RUNNING_PROCESS_INFO;

#define MAX_PROCESSES 10
RUNNING_PROCESS_INFO rp[MAX_PROCESSES];
int nRPCounter = 0; // running processes counter

int DisplayMainMenu();
void DisplayStopProcessMenu();
void process(int option);

int main(int argc, char* argv[])
{
	while(DisplayMainMenu()!=5);
	return 0;
}

int DisplayMainMenu()
{
	int option=0;
	system("cls");
	cout << "\n\n\n";
	cout << "\n\t*********************************";
	cout << "\n\t* *";
	cout << "\n\t* MAIN MENU *";
	cout << "\n\t* *";
	cout << "\n\t* 1. FreeCell *";
	cout << "\n\t* 2. MineSweeper *";
	cout << "\n\t* 3. Paint *";
	cout << "\n\t* 4. StopProcess *";
	cout << "\n\t* 5. Quit *";
	cout << "\n\t* *";
	cout << "\n\t*********************************";
	cout << "\n\nPlease type your choice "<< "and press the return key : ";
	cin >> option;

	process(option);
	
	return option;
}

void DisplayStopProcessMenu()
{
	int option=0;
	system("cls");
	cout << "\n\n\n";
	cout << "\n\t*********************************";
	cout << "\n\t* *";
	cout << "\n\t* STOP PROCESS MENU *";
	cout << "\n\t* *";
	for(int i=0;i<nRPCounter;i++)
	{
		cout << "\n\t* ";
		cout << i+1;
		cout << ". ";
		if(rp[i].bProcessType==1)
			cout << "FreeCell *";
		else if(rp[i].bProcessType==2)
			cout << "MineSweeper *";
		else
			cout << "Paint *";
	}
	cout << "\n\t* *";
	cout << "\n\t*********************************";
	cout << "\n\nPlease type your choice "<< "and press the return key : ";
	cin >> option;
	if(option>=1 && option<=nRPCounter)
	{
		PostThreadMessage(rp[option-1].dwThreadId,WM_QUIT,0,0);
		nRPCounter--;
		for(int i=option-1; i<nRPCounter; i++)
			memcpy(&rp[i],&rp[i+1],sizeof(RUNNING_PROCESS_INFO));
	}
}

void process(int option)
{
	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	if(option>=1 && option<=3)
	{
		if(nRPCounter==MAX_PROCESSES)
		{
			printf("\a\aRunning processes table full\n");
			return;
		}
		ZeroMemory( &pi, sizeof(PROCESS_INFORMATION) );
		ZeroMemory( &si, sizeof(STARTUPINFO) );
		si.cb = sizeof(STARTUPINFO); 
	}
	switch (option)
	{
	case 1 : 
		if(CreateProcess( NULL, "FreeCell.exe", NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi))
		{
			rp[nRPCounter].dwThreadId = pi.dwThreadId;
			rp[nRPCounter++].bProcessType = 1;
			CloseHandle(pi.hThread);
			CloseHandle(pi.hProcess);
		}
		else
			printf("\a\aCreateProcess(...) failed\n");
		break; 
	case 2 : 
		if(CreateProcess( NULL, "winmine.exe", NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi))
		{
			rp[nRPCounter].dwThreadId = pi.dwThreadId;
			rp[nRPCounter++].bProcessType = 2;
			CloseHandle(pi.hThread);
			CloseHandle(pi.hProcess);
		}
		else
			printf("\a\aCreateProcess(...) failed\n");
		break;
	case 3 : 
		if(CreateProcess( NULL, "MsPaint.exe", NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi))
		{
			rp[nRPCounter].dwThreadId = pi.dwThreadId;
			rp[nRPCounter++].bProcessType = 3;
			CloseHandle(pi.hThread);
			CloseHandle(pi.hProcess);
		}
		else
			printf("\a\aCreateProcess(...) failed\n");
		break;
	case 4:
		DisplayStopProcessMenu();
		break;
	case 5 : 
		{
			for(int i=0; i<nRPCounter;i++)
				PostThreadMessage(rp[i].dwThreadId,WM_QUIT,0,0);
		}
		break;
	default : 
		printf("\a\aOption Not Available\n");
		break;
	}
}



Regards,
Andrzej Markowski

My Latest ArticlesCCustomBitmapButton: An owner-draw button and a frame for the caption bar, in one class.
CCustomTabCtrl: A clone of the Excel tab sheet control.

AnswerRe: How to close other processes? Pin
Andrzej Markowski21-Oct-04 8:38
Andrzej Markowski21-Oct-04 8:38 
AnswerRe: How to close other processes? Pin
Andrzej Markowski23-Oct-04 19:16
Andrzej Markowski23-Oct-04 19:16 
QuestionProblem trying to create an alpha surface? Pin
Dani10000114-Oct-04 12:12
Dani10000114-Oct-04 12:12 
Generaldouble-clicking application causes error Pin
dotbomb14-Oct-04 12:02
dotbomb14-Oct-04 12:02 
GeneralRe: double-clicking application causes error Pin
Joaquín M López Muñoz14-Oct-04 12:15
Joaquín M López Muñoz14-Oct-04 12:15 
GeneralRe: double-clicking application causes error Pin
dotbomb14-Oct-04 12:23
dotbomb14-Oct-04 12:23 
GeneralRe: double-clicking application causes error Pin
Joaquín M López Muñoz14-Oct-04 12:44
Joaquín M López Muñoz14-Oct-04 12:44 
QuestionHow To Hold a Key Pin
Dody_DK14-Oct-04 11:48
Dody_DK14-Oct-04 11:48 
AnswerRe: How To Hold a Key Pin
ThatsAlok14-Oct-04 18:42
ThatsAlok14-Oct-04 18:42 
AnswerRe: How To Hold a Key Pin
toxcct14-Oct-04 20:01
toxcct14-Oct-04 20:01 
GeneralRe: How To Hold a Key Pin
Dody_DK17-Oct-04 9:56
Dody_DK17-Oct-04 9:56 
Generalmessage reflection Pin
prateekkathuria14-Oct-04 11:47
prateekkathuria14-Oct-04 11:47 
GeneralRe: message reflection Pin
prateekkathuria14-Oct-04 12:33
prateekkathuria14-Oct-04 12:33 
GeneralConstruction fails Pin
Fernando A. Gomez F.14-Oct-04 10:50
Fernando A. Gomez F.14-Oct-04 10:50 
GeneralRe: Construction fails Pin
David Crow14-Oct-04 11:01
David Crow14-Oct-04 11:01 
GeneralRe: Construction fails Pin
Fernando A. Gomez F.14-Oct-04 11:09
Fernando A. Gomez F.14-Oct-04 11:09 
GeneralRe: Construction fails Pin
Fernando A. Gomez F.14-Oct-04 11:16
Fernando A. Gomez F.14-Oct-04 11:16 

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.