Restarting a process from another process

I have two processes, ProcessA and ProcessB.

When I start my application, I call ProcessA, which CreateProcess()

ProcessB uses to start. ProcessA is killed by ProcessB when my application receives command A. Likewise, ProcessB must restart ProcessA when it receives command B.

Where I am stuck is the process of restarting ProcessA. Since ProcessA has the code to restart ProcessB, I cannot stop it from restarting another instance of ProcessB. Ideally, I only want 1 instance of ProcessB.

To create ProcessB from ProcessA I have the following code:

for(int i32Index = 0; i32Index < NUM_PROCESS; i32Index++)
        {
            wcscpy(wcPath,Directorypath.c_str());
            wcscat(wcPath,wcProcess[i32Index]);
            RETAILMSG(1,(_T("Path:%s\r\n"),wcPath));
            bCreateProcessSuccess = CreateProcess(wcPath,   // No module name (use command line)
                                    NULL,                   // Command line
                                    NULL,                   // Process handle not inheritable
                                    NULL,                   // Thread handle not inheritable
                                    FALSE,                  // Set handle inheritance to FALSE
                                    0,                      // No creation flags
                                    NULL,                   // Use parent environment block
                                    NULL,                   // Use parent starting directory 
                                    &si,                    // Pointer to STARTUPINFO structure
                                    &pi ) ;                 // Pointer to PROCESS_INFORMATION structure
             if(bCreateProcessSuccess == FALSE)
             {
                 RETAILMSG(1,(_T("Create process failed:%d\r\n"),GetLastError()));
             }
             else
             {
                RETAILMSG(1,(_T("Loading Exes\r\n")));
             }

      

Pretty simple, basic code. I basically repeat this in ProcessB, but in such a way that it creates ProcessA.

Now, I am stuck on how I could implement the condition for ProcessA to start without having ProcessB re-running. I originally thought of using a flag, but that flag would be reset as starting ProcessA would reset the flag as it is local to this function.

Also, to clarify: this is in Windows embedded compact environment, so both processes exist as different subprojects, so an IPC would be required to access ProcessA from ProcessB.

My next idea was to use CreateEvent()

with WaitForSingleObject()

to check if an event is being signaled, but I figured out that the wait duration should be infinite, which would cause a problem the first time I started my application.

So, are there any windows (wince) api that will resolve this? (Or some weird coding that I can't think of?)

+3


source to share


2 answers


There are several ways to do this, but two options:



  • When you run ProcessA again, pass it a command line argument using a parameter lpCommandLine

    (for example /nolaunch

    ). ProcessA can then get its command line using GetCommandLine

    at startup and see if it contains the string /nolaunch

    .

  • In ProcessA and ProcessB use CreateSemaphore

    or CreateMutex

    to create a named semaphore / mutex. In ProcessB, all you have to do is just make sure you don't close the handle until the process finishes. In ProcessA, after creating a semaphore / mutex check, if it GetLastError()

    returns ERROR_ALREADY_EXISTS

    , then it indicates that ProcessB still has the semaphore / mutex open (and therefore already running).

+6


source


There are several ways you can do this:

1. Holding a mutex

In process B, you can hold on to a named mutex and lock it. When processB is created, it will try to lock the mutex and fail, and it will kill itself. You can also check if the mutex is locked in processA and prevent processB from being created from the beginning.

2. Sending an argument to create a process



CreateProcess supports sending command line arguments to the created process through the second argument. You can use it as an indicator for whether processB needs to be created or not.

3. Walking through the process list

You can view the current list of processes and check if ProcessB is running. You might want to explore Taking Snapshots and Viewing Processes .

+2


source







All Articles