Answer the question
In order to leave comments, you need to log in
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
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question