diff --git a/src/Components/Modules/IPCPipe.cpp b/src/Components/Modules/IPCPipe.cpp index b50eab0a..3cdd88b9 100644 --- a/src/Components/Modules/IPCPipe.cpp +++ b/src/Components/Modules/IPCPipe.cpp @@ -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(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() diff --git a/src/Worker/Main.cpp b/src/Worker/Main.cpp index 9d7d5101..d32947d6 100644 --- a/src/Worker/Main.cpp +++ b/src/Worker/Main.cpp @@ -1,15 +1,57 @@ #include "STDInclude.hpp" #include -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; }