Start the process not like a child

I need to start a process and run it as a separate process. I have some kind of starting application, the purpose of which is to launch another exe and exit immediately. What is the best way to achieve this?

I read the CreateProcessdocumentation several times, but I still have questions. The documentation says that I need to call CloseHandleafter completion. But my parent application should not wait for the child to exit. Another part of the documentation says that I can leave only the handles - the system will close them when the parent process is completed. Does this mean that the child application exits immediately after the parent? This does not seem to be the case - I am closing the starter, but my child process is still working.

There is a flag DETACHED_PROCESSthat seems to be what I'm looking for. But the documentation is talking about the console. Which console? I do not need a console.

+4
source share
2 answers

The DETACHED_PROCESS flag documentation contains

For console processes, the new process does not inherit the parent console (default)

This means: if you have a console process and you start a new one, it will not inherit its parent console.

If you don't have a console process, you have nothing to worry about.

CreateProcess creates a child process, but does not wait for the child process to complete , so you are all set.

, CreateProcess, WaitForSingleObject

:

// Version 1) Launch and wait for a child process completion
STARTUPINFO info = { sizeof(info) };
PROCESS_INFORMATION processInfo;
if (CreateProcess(L"C:\\myapp.exe", L"", NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo)) {
    ::WaitForSingleObject(processInfo.hProcess, INFINITE); // DO WAIT for the child to exit
    CloseHandle(processInfo.hProcess);
    CloseHandle(processInfo.hThread);
}

// ----------------------------------------------------------------

// Version 2) Launch and do NOT wait for a child process completion
STARTUPINFO info = { sizeof(info) };
PROCESS_INFORMATION processInfo;
if (CreateProcess(L"C:\\myapp.exe", L"", NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo)) {
    CloseHandle(processInfo.hProcess); // Cleanup since you don't need this
    CloseHandle(processInfo.hThread); // Cleanup since you don't need this
}

, 2 . , .

+3

CreateProcess, . , CloseHandle , . .

, . CloseHandle .

, - , . , ? , - , .

, . , , , .

DETACHED_PROCESS, , . . ? .

, .

+4

Source: https://habr.com/ru/post/1584928/


All Articles