T
T
Tesla4o2020-03-03 16:54:14
C++ / C#
Tesla4o, 2020-03-03 16:54:14

How to create an independent process?

Unable to create independent twin process using WinApi methods.
I use the CreateProcess(...) method, and a new process is launched, but if it is closed, the parent is also closed.
And it is necessary that the state does not affect each other
. Here is the code:

int main() {
    int a;
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    si.wShowWindow = SW_HIDE;
    si.dwFlags = STARTF_USEPOSITION | STARTF_USESIZE;

    char szFileName[FILENAME_MAX];
    szFileName[0] = '\0';

    GetModuleFileName(NULL, szFileName, FILENAME_MAX);

    std::cout << "enter - ";
    std::cin >> a;
    if (a == 1) {
        CreateProcess(szFileName, NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);

        WaitForSingleObject(pi.hProcess, INFINITE);

        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
    } else
        std::cout << "Shh";

    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2020-03-03
@jcmvbkbc

if (a == 1) {
        CreateProcess(szFileName, NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
        WaitForSingleObject(pi.hProcess, INFINITE);
        ...
    }
    ...
    return 0;

a new process is launched, but if it is closed, the parent is also closed.

Well, that's exactly what you wrote: the parent starts the process and waits for its completion, and after that it ends itself. If you want other behavior, write another behavior.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question