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. How to close other processes?

How to close other processes?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
13 Posts 3 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.
  • M Offline
    M Offline
    Moochie5
    wrote on last edited by
    #1

    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) { }

    A 3 Replies Last reply
    0
    • M Moochie5

      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) { }

      A Offline
      A Offline
      Andrzej Markowski
      wrote on last edited by
      #2

      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.

      V M 2 Replies Last reply
      0
      • A Andrzej Markowski

        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.

        V Offline
        V Offline
        vikramlinux
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • A Andrzej Markowski

          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.

          M Offline
          M Offline
          Moochie5
          wrote on last edited by
          #4

          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]);

          A 1 Reply Last reply
          0
          • M Moochie5

            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]);

            A Offline
            A Offline
            Andrzej Markowski
            wrote on last edited by
            #5

            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 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.

            M 1 Reply Last reply
            0
            • A Andrzej Markowski

              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 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.

              M Offline
              M Offline
              Moochie5
              wrote on last edited by
              #6

              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,

              A 1 Reply Last reply
              0
              • M Moochie5

                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,

                A Offline
                A Offline
                Andrzej Markowski
                wrote on last edited by
                #7

                You should declare the arrays (si and pi) as global variables. Also you can't call the DisplayMenu from within the process 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

                M 2 Replies Last reply
                0
                • A Andrzej Markowski

                  You should declare the arrays (si and pi) as global variables. Also you can't call the DisplayMenu from within the process 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

                  M Offline
                  M Offline
                  Moochie5
                  wrote on last edited by
                  #8

                  Finally got it! Thanks for all the help

                  1 Reply Last reply
                  0
                  • A Andrzej Markowski

                    You should declare the arrays (si and pi) as global variables. Also you can't call the DisplayMenu from within the process 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

                    M Offline
                    M Offline
                    Moochie5
                    wrote on last edited by
                    #9

                    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.

                    M 1 Reply Last reply
                    0
                    • M Moochie5

                      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.

                      M Offline
                      M Offline
                      Moochie5
                      wrote on last edited by
                      #10

                      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.

                      A 1 Reply Last reply
                      0
                      • M Moochie5

                        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.

                        A Offline
                        A Offline
                        Andrzej Markowski
                        wrote on last edited by
                        #11

                        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> option;
                        if(option>=1 && option<=nRPCounter)
                        {
                        PostThreadMessage(rp[option-1].dwThreadId,WM_QUIT,0,0);
                        nRPCounter--;
                        for(int

                        1 Reply Last reply
                        0
                        • M Moochie5

                          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) { }

                          A Offline
                          A Offline
                          Andrzej Markowski
                          wrote on last edited by
                          #12

                          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.

                          1 Reply Last reply
                          0
                          • M Moochie5

                            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) { }

                            A Offline
                            A Offline
                            Andrzej Markowski
                            wrote on last edited by
                            #13

                            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.

                            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