How to close other processes?
-
Send WM_QUIT message to each running application.
PROCESS_INFORMATION pi[3]; // for each process
STARTUPINFO si[3];
// init structures
ZeroMemory( &si[0], sizeof(si[0]) );
si.cb = sizeof(si[0]);
ZeroMemory( &pi[0], sizeof(pi[0]) );ZeroMemory( &si[1], sizeof(si[1]) );
si.cb = sizeof(si[1]);
ZeroMemory( &pi[1], sizeof(pi[1]) );ZeroMemory( &si[2], sizeof(si[2]) );
si.cb = sizeof(si[2]);
ZeroMemory( &pi[2], sizeof(pi[2]) );// start appications
if(pi[0].hProcess==NULL)
CreateProcess( NULL, "C://FreeCell.exe",NULL,NULL,FALSE,NULL,NULL,NULL,&si[0],&pi[0]));
if(pi[1].hProcess==NULL)
CreateProcess( NULL, "winmine.exe",NULL,NULL,FALSE,NULL,NULL,NULL,&si[1],&pi[1]);
if(pi[2].hProcess==NULL)
CreateProcess( NULL, "MsPaint.exe",NULL,NULL,FALSE, NULL,NULL,NULL,&si[2],&pi[2]);// shutdown applications
if(pi[0].hProcess)
::PostThreadMessage(pi[0].dwThreadId,WM_QUIT,0,0);
if(pi[1].hProcess)
::PostThreadMessage(pi[1].dwThreadId,WM_QUIT,0,0);
if(pi[2].hProcess)
::PostThreadMessage(pi[2].dwThreadId,WM_QUIT,0,0);CloseHandle( pi[0].hProcess );
CloseHandle( pi[0].hThread );
CloseHandle( pi[1].hProcess );
CloseHandle( pi[1].hThread );
CloseHandle( pi[2].hProcess );
CloseHandle( pi[2].hThread );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.
Enumerate All Windows on specified thread. Post WM_CLOSE to them , in order to close the application in safe manner.Later u may call terminateprocess API
-
Send WM_QUIT message to each running application.
PROCESS_INFORMATION pi[3]; // for each process
STARTUPINFO si[3];
// init structures
ZeroMemory( &si[0], sizeof(si[0]) );
si.cb = sizeof(si[0]);
ZeroMemory( &pi[0], sizeof(pi[0]) );ZeroMemory( &si[1], sizeof(si[1]) );
si.cb = sizeof(si[1]);
ZeroMemory( &pi[1], sizeof(pi[1]) );ZeroMemory( &si[2], sizeof(si[2]) );
si.cb = sizeof(si[2]);
ZeroMemory( &pi[2], sizeof(pi[2]) );// start appications
if(pi[0].hProcess==NULL)
CreateProcess( NULL, "C://FreeCell.exe",NULL,NULL,FALSE,NULL,NULL,NULL,&si[0],&pi[0]));
if(pi[1].hProcess==NULL)
CreateProcess( NULL, "winmine.exe",NULL,NULL,FALSE,NULL,NULL,NULL,&si[1],&pi[1]);
if(pi[2].hProcess==NULL)
CreateProcess( NULL, "MsPaint.exe",NULL,NULL,FALSE, NULL,NULL,NULL,&si[2],&pi[2]);// shutdown applications
if(pi[0].hProcess)
::PostThreadMessage(pi[0].dwThreadId,WM_QUIT,0,0);
if(pi[1].hProcess)
::PostThreadMessage(pi[1].dwThreadId,WM_QUIT,0,0);
if(pi[2].hProcess)
::PostThreadMessage(pi[2].dwThreadId,WM_QUIT,0,0);CloseHandle( pi[0].hProcess );
CloseHandle( pi[0].hThread );
CloseHandle( pi[1].hProcess );
CloseHandle( pi[1].hThread );
CloseHandle( pi[2].hProcess );
CloseHandle( pi[2].hThread );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.
Thanks for the reply. I am getting an 3 errors which say "left of .cb must have class/struct/union type". This is the part of the code it is giving those errors:
PROCESS_INFORMATION pi[3]; // for each process STARTUPINFO si[3];// init structures ZeroMemory( &si[0], sizeof(si[0]) ); //HERE si.cb = sizeof(si[0]); ZeroMemory( &pi[0], sizeof(pi[0]) ); ZeroMemory( &si[1], sizeof(si[1]) ); //HERE si.cb = sizeof(si[1]); ZeroMemory( &pi[1], sizeof(pi[1]) ); ZeroMemory( &si[2], sizeof(si[2]) ); //HERE si.cb = sizeof(si[2]);
-
Thanks for the reply. I am getting an 3 errors which say "left of .cb must have class/struct/union type". This is the part of the code it is giving those errors:
PROCESS_INFORMATION pi[3]; // for each process STARTUPINFO si[3];// init structures ZeroMemory( &si[0], sizeof(si[0]) ); //HERE si.cb = sizeof(si[0]); ZeroMemory( &pi[0], sizeof(pi[0]) ); ZeroMemory( &si[1], sizeof(si[1]) ); //HERE si.cb = sizeof(si[1]); ZeroMemory( &pi[1], sizeof(pi[1]) ); ZeroMemory( &si[2], sizeof(si[2]) ); //HERE si.cb = sizeof(si[2]);
Change the code like this:
PROCESS_INFORMATION pi[3]; // for each process STARTUPINFO si[3];// init structures ZeroMemory( &si[0], sizeof(STARTUPINFO)); si[0].cb = sizeof(STARTUPINFO); ZeroMemory( &pi[0], sizeof(PROCESS_INFORMATION)); ZeroMemory( &si[1], sizeof(STARTUPINFO)); si[1].cb = sizeof(STARTUPINFO); ZeroMemory( &pi[1], sizeof(PROCESS_INFORMATION)); ZeroMemory( &si[2], sizeof(STARTUPINFO)); si[2].cb = sizeof(STARTUPINFO); ZeroMemory( &pi[2], sizeof(PROCESS_INFORMATION));
Regards, Andrzej MarkowskiMy 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.
-
Change the code like this:
PROCESS_INFORMATION pi[3]; // for each process STARTUPINFO si[3];// init structures ZeroMemory( &si[0], sizeof(STARTUPINFO)); si[0].cb = sizeof(STARTUPINFO); ZeroMemory( &pi[0], sizeof(PROCESS_INFORMATION)); ZeroMemory( &si[1], sizeof(STARTUPINFO)); si[1].cb = sizeof(STARTUPINFO); ZeroMemory( &pi[1], sizeof(PROCESS_INFORMATION)); ZeroMemory( &si[2], sizeof(STARTUPINFO)); si[2].cb = sizeof(STARTUPINFO); ZeroMemory( &pi[2], sizeof(PROCESS_INFORMATION));
Regards, Andrzej MarkowskiMy 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.
Thanks for the help Andrzej I have configured the code as shown below: Including a couple of functions. The program builds with 0 errors and 0 warnings, but for some reason when I enter a choice like (1) for "Freecell", my program quits, the program that I have opened remains open, so I do not have the chance to quit the Freecell program. Not sure why it is not giving me another choice:
#include #include #include #include void DisplayMenu(); void process(int option); void main() { DisplayMenu(); return; } void DisplayMenu() { int option; cout << "\n\n\n"; cout << "\n\t*********************************"; cout << "\n\t* *"; cout << "\n\t* MENU *"; cout << "\n\t* *"; cout << "\n\t* 1. FreeCell *"; cout << "\n\t* 2. MineSweeper *"; cout << "\n\t* 3. Paint *"; cout << "\n\t* 4. Quit *"; cout << "\n\t* *"; cout << "\n\t*********************************"; cout << "\n\nPlease type your choice " << "and press the return key : "; cin >> option; process(option); return; } void process(int option) { STARTUPINFO si[3]; PROCESS_INFORMATION pi[3]; int i=0; ZeroMemory( &si[0], sizeof(si[0]) ); si[0].cb = sizeof(si[0]); ZeroMemory( &pi[0], sizeof(pi[0]) ); ZeroMemory( &si[1], sizeof(si[1]) ); si[1].cb = sizeof(si[1]); ZeroMemory( &pi[1], sizeof(pi[1]) ); ZeroMemory( &si[2], sizeof(si[2]) ); si[2].cb = sizeof(si[2]); ZeroMemory( &pi[2], sizeof(pi[2]) ); switch (option) { case 1 : if(pi[0].hProcess==NULL)CreateProcess( NULL, "FreeCell.exe", NULL, NULL, FALSE, NULL, NULL, NULL, &si[0], &pi[0]); { ExitProcess(1); } system("cls"); DisplayMenu(); break; case 2 : if(pi[1].hProcess==NULL)CreateProcess( NULL, "winmine.exe", NULL, NULL, FALSE, NULL, NULL, NULL, &si[1], &pi[1]); { ExitProcess(1); } system("cls"); DisplayMenu(); break; case 3 : if(pi[2].hProcess==NULL)CreateProcess( NULL, "MsPaint.exe", // Command line. NULL, NULL, FALSE,
-
Thanks for the help Andrzej I have configured the code as shown below: Including a couple of functions. The program builds with 0 errors and 0 warnings, but for some reason when I enter a choice like (1) for "Freecell", my program quits, the program that I have opened remains open, so I do not have the chance to quit the Freecell program. Not sure why it is not giving me another choice:
#include #include #include #include void DisplayMenu(); void process(int option); void main() { DisplayMenu(); return; } void DisplayMenu() { int option; cout << "\n\n\n"; cout << "\n\t*********************************"; cout << "\n\t* *"; cout << "\n\t* MENU *"; cout << "\n\t* *"; cout << "\n\t* 1. FreeCell *"; cout << "\n\t* 2. MineSweeper *"; cout << "\n\t* 3. Paint *"; cout << "\n\t* 4. Quit *"; cout << "\n\t* *"; cout << "\n\t*********************************"; cout << "\n\nPlease type your choice " << "and press the return key : "; cin >> option; process(option); return; } void process(int option) { STARTUPINFO si[3]; PROCESS_INFORMATION pi[3]; int i=0; ZeroMemory( &si[0], sizeof(si[0]) ); si[0].cb = sizeof(si[0]); ZeroMemory( &pi[0], sizeof(pi[0]) ); ZeroMemory( &si[1], sizeof(si[1]) ); si[1].cb = sizeof(si[1]); ZeroMemory( &pi[1], sizeof(pi[1]) ); ZeroMemory( &si[2], sizeof(si[2]) ); si[2].cb = sizeof(si[2]); ZeroMemory( &pi[2], sizeof(pi[2]) ); switch (option) { case 1 : if(pi[0].hProcess==NULL)CreateProcess( NULL, "FreeCell.exe", NULL, NULL, FALSE, NULL, NULL, NULL, &si[0], &pi[0]); { ExitProcess(1); } system("cls"); DisplayMenu(); break; case 2 : if(pi[1].hProcess==NULL)CreateProcess( NULL, "winmine.exe", NULL, NULL, FALSE, NULL, NULL, NULL, &si[1], &pi[1]); { ExitProcess(1); } system("cls"); DisplayMenu(); break; case 3 : if(pi[2].hProcess==NULL)CreateProcess( NULL, "MsPaint.exe", // Command line. NULL, NULL, FALSE,
You should declare the arrays (
si
andpi
) as global variables. Also you can't call theDisplayMenu
from within theprocess
function (recursion). Here's an example how to fix these problems:STARTUPINFO si[3];
PROCESS_INFORMATION pi[3];int DisplayMenu();
void process(int option);void main()
{
ZeroMemory( &si[0], sizeof(si[0]) );
si[0].cb = sizeof(si[0]);
ZeroMemory( &pi[0], sizeof(pi[0]) );ZeroMemory( &si[1], sizeof(si[1]) );
si[1].cb = sizeof(si[1]);
ZeroMemory( &pi[1], sizeof(pi[1]) );ZeroMemory( &si[2], sizeof(si[2]) );
si[2].cb = sizeof(si[2]);
ZeroMemory( &pi[2], sizeof(pi[2]) );while(DisplayMenu()!=4)
return;
}int DisplayMenu()
{
int option;
system("cls");
cout << "\n\n\n";
cout << "\n\t*********************************";
cout << "\n\t* *";
cout << "\n\t* MENU *";
cout << "\n\t* *";
cout << "\n\t* 1. FreeCell *";
cout << "\n\t* 2. MineSweeper *";
cout << "\n\t* 3. Paint *";
cout << "\n\t* 4. 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 process(int option)
{
switch (option)
{
case 1 : if(pi[0].hProcess==NULL)CreateProcess( NULL,
"FreeCell.exe",
NULL,
NULL,
FALSE,
NULL,
NULL,
NULL,
&si[0],
&pi[0]);break;
case 2 : if(pi[1].hProcess==NULL)CreateProcess( NULL,
"winmine.exe",
NULL,
NULL,
FALSE,
NULL,
NULL,
NULL,
&si[1],
&pi[1]);break;
case 3 : if(pi[2].hProcess==NULL)CreateProcess( NULL,
"MsPaint.exe", // Command line.
NULL,
NULL,
FALSE,
NULL,
NULL,
NULL,
&si[2],
&pi[2]);break;
case 4 : if(pi[0].hProcess)::PostThreadMessage(pi[0].dwThreadId,WM_QUIT,0,0);
if(pi[1].hProcess)::PostThreadMessage(pi[1].dwThreadId,WM_QUIT,0,0);
if(pi[2].hProcess)::PostThreadMessage(pi[2].dwThreadId,WM_QUIT,0,0);
CloseHandle( pi[0].hProcess );
CloseHandle( pi[0].hThread );
CloseHandle( pi[1].hProcess );
CloseHandle( pi[1].hThread );
CloseHandle( pi[2].hProcess );
CloseHandle( pi[2].hThread );
// CloseHandle( pi[3].hProcess );
// CloseHandle( pi[3].hThread );break;
default : printf("\a\aOption Not Available\n");
break;
}return;
}Regards, Andrzej Markowski
My Latest Articles
-
You should declare the arrays (
si
andpi
) as global variables. Also you can't call theDisplayMenu
from within theprocess
function (recursion). Here's an example how to fix these problems:STARTUPINFO si[3];
PROCESS_INFORMATION pi[3];int DisplayMenu();
void process(int option);void main()
{
ZeroMemory( &si[0], sizeof(si[0]) );
si[0].cb = sizeof(si[0]);
ZeroMemory( &pi[0], sizeof(pi[0]) );ZeroMemory( &si[1], sizeof(si[1]) );
si[1].cb = sizeof(si[1]);
ZeroMemory( &pi[1], sizeof(pi[1]) );ZeroMemory( &si[2], sizeof(si[2]) );
si[2].cb = sizeof(si[2]);
ZeroMemory( &pi[2], sizeof(pi[2]) );while(DisplayMenu()!=4)
return;
}int DisplayMenu()
{
int option;
system("cls");
cout << "\n\n\n";
cout << "\n\t*********************************";
cout << "\n\t* *";
cout << "\n\t* MENU *";
cout << "\n\t* *";
cout << "\n\t* 1. FreeCell *";
cout << "\n\t* 2. MineSweeper *";
cout << "\n\t* 3. Paint *";
cout << "\n\t* 4. 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 process(int option)
{
switch (option)
{
case 1 : if(pi[0].hProcess==NULL)CreateProcess( NULL,
"FreeCell.exe",
NULL,
NULL,
FALSE,
NULL,
NULL,
NULL,
&si[0],
&pi[0]);break;
case 2 : if(pi[1].hProcess==NULL)CreateProcess( NULL,
"winmine.exe",
NULL,
NULL,
FALSE,
NULL,
NULL,
NULL,
&si[1],
&pi[1]);break;
case 3 : if(pi[2].hProcess==NULL)CreateProcess( NULL,
"MsPaint.exe", // Command line.
NULL,
NULL,
FALSE,
NULL,
NULL,
NULL,
&si[2],
&pi[2]);break;
case 4 : if(pi[0].hProcess)::PostThreadMessage(pi[0].dwThreadId,WM_QUIT,0,0);
if(pi[1].hProcess)::PostThreadMessage(pi[1].dwThreadId,WM_QUIT,0,0);
if(pi[2].hProcess)::PostThreadMessage(pi[2].dwThreadId,WM_QUIT,0,0);
CloseHandle( pi[0].hProcess );
CloseHandle( pi[0].hThread );
CloseHandle( pi[1].hProcess );
CloseHandle( pi[1].hThread );
CloseHandle( pi[2].hProcess );
CloseHandle( pi[2].hThread );
// CloseHandle( pi[3].hProcess );
// CloseHandle( pi[3].hThread );break;
default : printf("\a\aOption Not Available\n");
break;
}return;
}Regards, Andrzej Markowski
My Latest Articles
-
You should declare the arrays (
si
andpi
) as global variables. Also you can't call theDisplayMenu
from within theprocess
function (recursion). Here's an example how to fix these problems:STARTUPINFO si[3];
PROCESS_INFORMATION pi[3];int DisplayMenu();
void process(int option);void main()
{
ZeroMemory( &si[0], sizeof(si[0]) );
si[0].cb = sizeof(si[0]);
ZeroMemory( &pi[0], sizeof(pi[0]) );ZeroMemory( &si[1], sizeof(si[1]) );
si[1].cb = sizeof(si[1]);
ZeroMemory( &pi[1], sizeof(pi[1]) );ZeroMemory( &si[2], sizeof(si[2]) );
si[2].cb = sizeof(si[2]);
ZeroMemory( &pi[2], sizeof(pi[2]) );while(DisplayMenu()!=4)
return;
}int DisplayMenu()
{
int option;
system("cls");
cout << "\n\n\n";
cout << "\n\t*********************************";
cout << "\n\t* *";
cout << "\n\t* MENU *";
cout << "\n\t* *";
cout << "\n\t* 1. FreeCell *";
cout << "\n\t* 2. MineSweeper *";
cout << "\n\t* 3. Paint *";
cout << "\n\t* 4. 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 process(int option)
{
switch (option)
{
case 1 : if(pi[0].hProcess==NULL)CreateProcess( NULL,
"FreeCell.exe",
NULL,
NULL,
FALSE,
NULL,
NULL,
NULL,
&si[0],
&pi[0]);break;
case 2 : if(pi[1].hProcess==NULL)CreateProcess( NULL,
"winmine.exe",
NULL,
NULL,
FALSE,
NULL,
NULL,
NULL,
&si[1],
&pi[1]);break;
case 3 : if(pi[2].hProcess==NULL)CreateProcess( NULL,
"MsPaint.exe", // Command line.
NULL,
NULL,
FALSE,
NULL,
NULL,
NULL,
&si[2],
&pi[2]);break;
case 4 : if(pi[0].hProcess)::PostThreadMessage(pi[0].dwThreadId,WM_QUIT,0,0);
if(pi[1].hProcess)::PostThreadMessage(pi[1].dwThreadId,WM_QUIT,0,0);
if(pi[2].hProcess)::PostThreadMessage(pi[2].dwThreadId,WM_QUIT,0,0);
CloseHandle( pi[0].hProcess );
CloseHandle( pi[0].hThread );
CloseHandle( pi[1].hProcess );
CloseHandle( pi[1].hThread );
CloseHandle( pi[2].hProcess );
CloseHandle( pi[2].hThread );
// CloseHandle( pi[3].hProcess );
// CloseHandle( pi[3].hThread );break;
default : printf("\a\aOption Not Available\n");
break;
}return;
}Regards, Andrzej Markowski
My Latest Articles
Again thanks for the help Andrzej, I have pretty much configured the program to work how I want except for one thing. I want to be able to open more than one of the same program. So if I open one Paint program I want to be able to enter 3 again for Paint and have it open another. It only lets me open one of each right now. Is there anyway to work around it? Meaning can I keep the global variables or is that one of the things that is preventing me from opening more than one program? Thanks again.
-
Again thanks for the help Andrzej, I have pretty much configured the program to work how I want except for one thing. I want to be able to open more than one of the same program. So if I open one Paint program I want to be able to enter 3 again for Paint and have it open another. It only lets me open one of each right now. Is there anyway to work around it? Meaning can I keep the global variables or is that one of the things that is preventing me from opening more than one program? Thanks again.
Just want to clarify something... I know how to be able to open more than one program but I'm not sure how to let the computer keep track of them so it closes them all. When I do change it to open more than one program and I enter 4 to close them it does not close any of them.
-
Just want to clarify something... I know how to be able to open more than one program but I'm not sure how to let the computer keep track of them so it closes them all. When I do change it to open more than one program and I enter 4 to close them it does not close any of them.
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.htypedef 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 counterint 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> option;
if(option>=1 && option<=nRPCounter)
{
PostThreadMessage(rp[option-1].dwThreadId,WM_QUIT,0,0);
nRPCounter--;
for(int -
Hello, I'm creating a program that displays a menu to the user and gives the user the option to open the following programs: 1. Freecell 2. Minesweeper 3. Paint 4. Quit It does open the programs correctly but when the user enters 4 for quit, I want my program to close all of the programs the user opened. I am not sure how to do that though. I have created a function called ExitProcess and have a call to it when the user enters 4 as their choice. There is no code in that function. Here is most of the code:
#include #include #include #include void DisplayMenu(); void CreateProcess(int option); void ExitProcess(void); // Main void main() { DisplayMenu(); return; } void DisplayMenu() { int option; //MENU IS DISPLAYED HERE cout << "\n\nPlease type your choice " << "and press the return key : "; cin >> option; CreateProcess(option); return; } void CreateProcess(int option) { STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); switch (option) { case 1 : if( !CreateProcess( NULL, "C://FreeCell.exe",NULL,NULL,FALSE,NULL,NULL,NULL,&si,&pi)) { ExitProcess(1); } system("cls"); DisplayMenu(); break; case 2 : if( !CreateProcess( NULL, "winmine.exe",NULL,NULL,FALSE,NULL,NULL,NULL,&si,&pi)) { ExitProcess(1); } system("cls"); DisplayMenu(); break; case 3 : if( !CreateProcess( NULL, "MsPaint.exe",NULL,NULL,FALSE, NULL,NULL,NULL,&si,&pi )) { ExitProcess(1); } system("cls"); DisplayMenu(); break; case 4 : WaitForSingleObject( pi.hProcess, INFINITE ); ExitProcess(); CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); exit(100); break; default : cout << "\a\aOption Not Available\n"; system("cls"); DisplayMenu(); } return; } void ExitProcess(void) { }
Very Important!!!! Don't use WM_QUIT message to kill other processes. Instead of WM_QUIT use WM_CLOSE. If you send WM_QUIT the application will not clean up resources and you will have memory leaks. 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.
-
Hello, I'm creating a program that displays a menu to the user and gives the user the option to open the following programs: 1. Freecell 2. Minesweeper 3. Paint 4. Quit It does open the programs correctly but when the user enters 4 for quit, I want my program to close all of the programs the user opened. I am not sure how to do that though. I have created a function called ExitProcess and have a call to it when the user enters 4 as their choice. There is no code in that function. Here is most of the code:
#include #include #include #include void DisplayMenu(); void CreateProcess(int option); void ExitProcess(void); // Main void main() { DisplayMenu(); return; } void DisplayMenu() { int option; //MENU IS DISPLAYED HERE cout << "\n\nPlease type your choice " << "and press the return key : "; cin >> option; CreateProcess(option); return; } void CreateProcess(int option) { STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); switch (option) { case 1 : if( !CreateProcess( NULL, "C://FreeCell.exe",NULL,NULL,FALSE,NULL,NULL,NULL,&si,&pi)) { ExitProcess(1); } system("cls"); DisplayMenu(); break; case 2 : if( !CreateProcess( NULL, "winmine.exe",NULL,NULL,FALSE,NULL,NULL,NULL,&si,&pi)) { ExitProcess(1); } system("cls"); DisplayMenu(); break; case 3 : if( !CreateProcess( NULL, "MsPaint.exe",NULL,NULL,FALSE, NULL,NULL,NULL,&si,&pi )) { ExitProcess(1); } system("cls"); DisplayMenu(); break; case 4 : WaitForSingleObject( pi.hProcess, INFINITE ); ExitProcess(); CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); exit(100); break; default : cout << "\a\aOption Not Available\n"; system("cls"); DisplayMenu(); } return; } void ExitProcess(void) { }
Posting WM_CLOSE message to the main thread using PostThreadMessage doesn't close your application also. You have to post WM_CLOSE message to all windows on the main thread, as vikrams said. Here's the code how to do this :
// define callback function like below
BOOL CALLBACK EnumThreadWndProc(HWND hwnd, LPARAM lParam)
{
::PostMessage(hwnd,WM_CLOSE,0,0);
return TRUE;
}
// call EnumThreadWindows instead of PostThreadMessage
EnumThreadWindows(rp[option-1].dwThreadId,EnumThreadWndProc,0);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.