[Runner] Workaround if we can't open the parent process
This commit is contained in:
parent
bef2cbfdcc
commit
5931c42cc6
@ -17,6 +17,7 @@
|
||||
#include <Wininet.h>
|
||||
#include <d3d9.h>
|
||||
#include <Aclapi.h>
|
||||
#include <Psapi.h>
|
||||
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4996)
|
||||
|
@ -18,25 +18,35 @@ namespace Worker
|
||||
{
|
||||
printf("Attaching to parent process %d...\n", this->processId);
|
||||
HANDLE processHandle = OpenProcess(SYNCHRONIZE, FALSE, this->processId);
|
||||
if (!processHandle || processHandle == INVALID_HANDLE_VALUE)
|
||||
|
||||
if (!processHandle || processHandle == INVALID_HANDLE_VALUE && !Runner::isProcessAlive())
|
||||
{
|
||||
printf("Unable to attach to parent process\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Successfully attached to parent process\n");
|
||||
printf("Starting worker...\n");
|
||||
|
||||
std::thread workerThread(&Runner::worker, this);
|
||||
|
||||
if (!processHandle || processHandle == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
while(this->isProcessAlive())
|
||||
{
|
||||
std::this_thread::sleep_for(100ms);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Successfully attached to parent process\n");
|
||||
printf("Starting worker...\n");
|
||||
|
||||
std::thread workerThread(&Runner::worker, this);
|
||||
|
||||
WaitForSingleObject(processHandle, INFINITE);
|
||||
CloseHandle(processHandle);
|
||||
|
||||
printf("Awaiting worker termination...\n");
|
||||
this->terminate = true;
|
||||
if (workerThread.joinable()) workerThread.join();
|
||||
printf("Worker terminated\n");
|
||||
}
|
||||
|
||||
printf("Awaiting worker termination...\n");
|
||||
this->terminate = true;
|
||||
if (workerThread.joinable()) workerThread.join();
|
||||
printf("Worker terminated\n");
|
||||
}
|
||||
|
||||
void Runner::attachHandler(Runner::Handler* handler)
|
||||
@ -44,6 +54,22 @@ namespace Worker
|
||||
this->handlers[handler->getCommand()] = std::shared_ptr<Runner::Handler>(handler);
|
||||
}
|
||||
|
||||
bool Runner::isProcessAlive()
|
||||
{
|
||||
DWORD aProcesses[1024], cbNeeded;
|
||||
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded)) return false;
|
||||
|
||||
for(DWORD i = 0; i < cbNeeded / sizeof(DWORD); ++i)
|
||||
{
|
||||
if(aProcesses[i] == static_cast<DWORD>(this->processId))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void Runner::worker()
|
||||
{
|
||||
printf("Worker started\n");
|
||||
|
@ -47,6 +47,7 @@ namespace Worker
|
||||
|
||||
private:
|
||||
void worker();
|
||||
bool isProcessAlive();
|
||||
|
||||
int processId;
|
||||
bool terminate;
|
||||
|
Loading…
Reference in New Issue
Block a user