Start processes, wait for it to end, and get its exit code (win32 api)

Posted on 2005-05-13
Last Modified: 2013-11-13

As the title indicates, I want to create a new processes using CreateProcesses, wait for it to terminated, and then recover its exit code.

Sample code would be helpful

Question by:nerdmike
    LVL 2

    Expert Comment

    (the following is taken from the above link)
    using System;
    using System.Diagnostics;
    using System.Threading;

    namespace Process_Sample
       class MyProcessClass
          public static void Main()

                Process myProcess;
                myProcess = Process.Start("NotePad.exe");


                   // Get physical memory usage of the associated process.
                   Console.WriteLine("Process's physical memory usage: " + myProcess.WorkingSet);
                   // Get base priority of the associated process.
                   Console.WriteLine("Base priority of the associated process: " + myProcess.BasePriority);
                   // Get priority class of the associated process.
                   Console.WriteLine("Priority class of the associated process: " + myProcess.PriorityClass);
                   // Get user processor time for this process.
                   Console.WriteLine("User Processor Time: " + myProcess.UserProcessorTime);
                   // Get privileged processor time for this process.
                   Console.WriteLine("Privileged Processor Time: " + myProcess.PrivilegedProcessorTime);
                   // Get total processor time for this process.
                   Console.WriteLine("Total Processor Time: " + myProcess.TotalProcessorTime);
                   // Invoke overloaded ToString function.
                   Console.WriteLine("Process's Name: " + myProcess.ToString());

                      Console.WriteLine("Status:  Responding to user interface");
                      Console.WriteLine("Status:  Not Responding");


                Console.WriteLine("Process exit code: {0}", myProcess.ExitCode);
             catch(Exception e)
                Console.WriteLine("The following exception was raised: " + e.Message);


    It looks like that does just what you want.
    Hope it helps!
    LVL 32

    Expert Comment

    Here's an example (launches Notepad, then waits for it to finish):

        // Structures we need to create processes.
          STARTUPINFO si;
          PROCESS_INFORMATION piProcess;

          // Prepare a STARTUPINFO structre for
          // spawning processes.
          ZeroMemory(&si, sizeof(si));
          si.cb = sizeof(si);
          // Create the new process
          BOOL bRetVal = CreateProcess("c:\\windows\\notepad.exe",
                NULL, NULL, FALSE,0, NULL, NULL, &si, &piProcess);

          // Wait for other process to be done, before we proceed
          cout << "The other process has quit" << endl;
    LVL 49

    Accepted Solution

    Also, take a look at this article which demonstrates how to start a process but also how to retrieve the exit code using GetExitCodeProcess

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    A short article about problems I had with the new location API and permissions in Marshmallow
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now