[IPCPipe] Launch the worker process

This commit is contained in:
momo5502 2017-01-27 15:06:31 +01:00
parent 8764d8327f
commit ef01b4e617
2 changed files with 60 additions and 6 deletions

View File

@ -284,6 +284,18 @@ namespace Components
Logger::Print("Sending ping to pipe!\n");
IPCPipe::Write("ping", "");
});
STARTUPINFOA sInfo;
PROCESS_INFORMATION pInfo;
ZeroMemory(&sInfo, sizeof(sInfo));
ZeroMemory(&pInfo, sizeof(pInfo));
sInfo.cb = sizeof(sInfo);
CreateProcessA("iw4x/iw4xworker.exe", const_cast<char*>(Utils::String::VA("-parentProc %d", GetCurrentProcessId())), nullptr, nullptr, false, NULL, nullptr, nullptr, &sInfo, &pInfo);
if (pInfo.hThread && pInfo.hThread != INVALID_HANDLE_VALUE) CloseHandle(pInfo.hThread);
if (pInfo.hProcess && pInfo.hProcess != INVALID_HANDLE_VALUE) CloseHandle(pInfo.hProcess);
}
void IPCPipe::preDestroy()

View File

@ -1,15 +1,57 @@
#include "STDInclude.hpp"
#include <conio.h>
int main(int /*argc*/, char /*argv*/[])
void worker(bool* terminator)
{
AllocConsole();
AttachConsole(GetCurrentProcessId());
freopen("CONOUT$", "w", stdout);
printf("Worker started\n");
while(!*terminator)
{
std::this_thread::sleep_for(1ms);
}
printf("Terminating worker\n");
}
int main()
{
bool terminator = false;
char* command = "-parentProc ";
char* parentProc = strstr(GetCommandLineA(), command);
if (!parentProc)
{
printf("No parent process argument found\n");
return 0;
}
parentProc += strlen(command);
int pid = atoi(parentProc);
printf("Attaching to process %d...\n", pid);
HANDLE processHandle = OpenProcess(SYNCHRONIZE, FALSE, pid);
if (!processHandle || processHandle == INVALID_HANDLE_VALUE)
{
printf("Unable to attach to parent process\n");
return 0;
}
printf("Successfully attached to parent process\n");
printf("Starting worker...\n");
std::thread runner(worker, &terminator);
WaitForSingleObject(processHandle, INFINITE);
CloseHandle(processHandle);
terminator = true;
printf("Awaiting worker termination...\n");
if (runner.joinable()) runner.join();
printf("Worker terminated\n");
printf("Hi");
_getch();
return 0;
}