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. TerminateProcess()

TerminateProcess()

Scheduled Pinned Locked Moved C / C++ / MFC
10 Posts 4 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.
  • G Offline
    G Offline
    gamefreak2291
    wrote on last edited by
    #1

    Alright, I'm trying to incorporate TerminateProcess() into my progrms. I have two programs running at the same time, the first program opens the second via system("") and then continues to run a series of GetAsyncKeyState()checks that blocks input when certain keys are pressed. The second program is an annoying little game that runs through a random array of MessageBoxes. The issue I've hit is that when the second program finishes, the first program is still going--which means the BlockInput code is still ready to set off. What I'm trying to do is incorporate TerminateProcess() at the very end of the second program so that right before the second program finishes it closes the first, does anyone know how to do this?

    J G 2 Replies Last reply
    0
    • G gamefreak2291

      Alright, I'm trying to incorporate TerminateProcess() into my progrms. I have two programs running at the same time, the first program opens the second via system("") and then continues to run a series of GetAsyncKeyState()checks that blocks input when certain keys are pressed. The second program is an annoying little game that runs through a random array of MessageBoxes. The issue I've hit is that when the second program finishes, the first program is still going--which means the BlockInput code is still ready to set off. What I'm trying to do is incorporate TerminateProcess() at the very end of the second program so that right before the second program finishes it closes the first, does anyone know how to do this?

      J Offline
      J Offline
      Joe Woodbury
      wrote on last edited by
      #2

      Why not have the first program start the second using CreateProcess() and then wait on the process handle? An alternative is to use a globally named event and have the first application wait on it (which is pretty much what my first suggestion is doing.)

      Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

      G 1 Reply Last reply
      0
      • J Joe Woodbury

        Why not have the first program start the second using CreateProcess() and then wait on the process handle? An alternative is to use a globally named event and have the first application wait on it (which is pretty much what my first suggestion is doing.)

        Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

        G Offline
        G Offline
        gamefreak2291
        wrote on last edited by
        #3

        Because I'm attending a High School that offers no programming class at all and thus I have no prior instructed knowledge and have no idea how to do that. I've attempted via examples from searching around but I couldn't get them to work because I just don't have enough knowledge at this point.

        J 1 Reply Last reply
        0
        • G gamefreak2291

          Because I'm attending a High School that offers no programming class at all and thus I have no prior instructed knowledge and have no idea how to do that. I've attempted via examples from searching around but I couldn't get them to work because I just don't have enough knowledge at this point.

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          DWORD ExecuteProcess(LPCTSTR pCommandLine, LPCTSTR pDirectory = NULL, DWORD millisecondsTimeout = INFINITE)
          {
          // _alloca allocates the memory on the stack, so I don't need to worry about cleaning it up
          // You could use new and then delete the buffer later
          TCHAR* pCmdLine = (TCHAR*) _alloca((lstrlen(pCommandLine) + 1) * sizeof(TCHAR));
          lstrcpy(pCmdLine, pCommandLine);

          STARTUPINFO startupInfo;
          memset(&startupInfo, 0, sizeof(startupInfo));
          startupInfo.cb = sizeof(startupInfo);
          
          PROCESS\_INFORMATION processInfo;
          memset(&processInfo, 0, sizeof(processInfo));
          
          DWORD rval;
          
          if (CreateProcess(NULL, pCmdLine, NULL, NULL, FALSE, CREATE\_DEFAULT\_ERROR\_MODE, NULL, pDirectory, &startupInfo, &processInfo))
          {
          	rval = WaitForSingleObject(processInfo.hProcess, millisecondsTimeout);
          }
          else
          {
          	rval = GetLastError();
          }
          
          CloseHandle(processInfo.hProcess);
          CloseHandle(processInfo.hThread);
          
          return rval;
          

          }

          Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

          G 1 Reply Last reply
          0
          • J Joe Woodbury

            DWORD ExecuteProcess(LPCTSTR pCommandLine, LPCTSTR pDirectory = NULL, DWORD millisecondsTimeout = INFINITE)
            {
            // _alloca allocates the memory on the stack, so I don't need to worry about cleaning it up
            // You could use new and then delete the buffer later
            TCHAR* pCmdLine = (TCHAR*) _alloca((lstrlen(pCommandLine) + 1) * sizeof(TCHAR));
            lstrcpy(pCmdLine, pCommandLine);

            STARTUPINFO startupInfo;
            memset(&startupInfo, 0, sizeof(startupInfo));
            startupInfo.cb = sizeof(startupInfo);
            
            PROCESS\_INFORMATION processInfo;
            memset(&processInfo, 0, sizeof(processInfo));
            
            DWORD rval;
            
            if (CreateProcess(NULL, pCmdLine, NULL, NULL, FALSE, CREATE\_DEFAULT\_ERROR\_MODE, NULL, pDirectory, &startupInfo, &processInfo))
            {
            	rval = WaitForSingleObject(processInfo.hProcess, millisecondsTimeout);
            }
            else
            {
            	rval = GetLastError();
            }
            
            CloseHandle(processInfo.hProcess);
            CloseHandle(processInfo.hThread);
            
            return rval;
            

            }

            Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            G Offline
            G Offline
            gamefreak2291
            wrote on last edited by
            #5

            okay, thanks for the tidbit of code but I still dont know where to insert it or how to tell it what to open

            I J 2 Replies Last reply
            0
            • G gamefreak2291

              Alright, I'm trying to incorporate TerminateProcess() into my progrms. I have two programs running at the same time, the first program opens the second via system("") and then continues to run a series of GetAsyncKeyState()checks that blocks input when certain keys are pressed. The second program is an annoying little game that runs through a random array of MessageBoxes. The issue I've hit is that when the second program finishes, the first program is still going--which means the BlockInput code is still ready to set off. What I'm trying to do is incorporate TerminateProcess() at the very end of the second program so that right before the second program finishes it closes the first, does anyone know how to do this?

              G Offline
              G Offline
              gamefreak2291
              wrote on last edited by
              #6

              I never did get an answer to my initial question

              S I 2 Replies Last reply
              0
              • G gamefreak2291

                I never did get an answer to my initial question

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                You can't do what you want. system doesn't give you back the identity of your process, so you have no way to (later) control it. You have to use some other mechanism to create the process, like (as Joe suggested) CreateProcess, which does give you the process identity (through the PID and a process handle in the case of CreateProcess).

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                1 Reply Last reply
                0
                • G gamefreak2291

                  okay, thanks for the tidbit of code but I still dont know where to insert it or how to tell it what to open

                  I Offline
                  I Offline
                  Iain Clarke Warrior Programmer
                  wrote on last edited by
                  #8

                  You call this function in the first program instead of the system function. It runs the second program (given in one of the parameters), waits for the second program to finish, then the function returns, so you can carry on doing whatever the first program should do. Iain.

                  In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!

                  1 Reply Last reply
                  0
                  • G gamefreak2291

                    I never did get an answer to my initial question

                    I Offline
                    I Offline
                    Iain Clarke Warrior Programmer
                    wrote on last edited by
                    #9

                    gamefreak2291 wrote:

                    I never did get an answer to my initial question

                    Your original question was posted when Europe had just gone to bed, India was debating getting to eat its breakfast Bhaji, and the Americas were having their after work beer. Or in your case, doing their homework. 1/ You don't pay us - we'll answer if / when we feel like. 2/ I'll give you credit for not putting "URGENTZ" in your subject line! Anyway, Joe has given you a very good answer - if you need details, functions like CreateProcess have very long documentation to read. I wish you success with your course, Iain.

                    In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!

                    1 Reply Last reply
                    0
                    • G gamefreak2291

                      okay, thanks for the tidbit of code but I still dont know where to insert it or how to tell it what to open

                      J Offline
                      J Offline
                      Joe Woodbury
                      wrote on last edited by
                      #10

                      You call ExecuteProcess with the same command line you would use at a command prompt. The directory is what the current directory should be set to. For example, ExecuteProcess(_T("C:\\Windows\\System32\\Notepad.exe win.ini"), _T("C:\\Windows")); This is very simple code, but it illustrates the basic principles. Go to MSDN and read up on CreateProcess() and WaitForSingleObject(). Read up on multi-threading and synchronization. Experiment. Step through the code. Until you fully understand this code, you will not be qualified to write any code.

                      Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                      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