Some clean-up and simplification for JobObject class.

This commit is contained in:
LoRd_MuldeR 2017-01-03 23:47:59 +01:00
parent 5dab5632f8
commit 4075bf405c
2 changed files with 26 additions and 42 deletions

View File

@ -27,18 +27,18 @@ class QProcess;
namespace MUtils namespace MUtils
{ {
class MUTILS_API JobObject_Private;
class MUTILS_API JobObject class MUTILS_API JobObject
{ {
public: public:
JobObject(void); JobObject(void);
~JobObject(void); ~JobObject(void);
bool addProcessToJob(const QProcess *proc); bool isObjectCreated(void);
bool terminateJob(unsigned int exitCode); bool addProcessToJob(const QProcess *const process);
bool terminateJob(const quint32 &exitCode);
private: private:
JobObject_Private *const p; uintptr_t m_jobPtr;
MUTILS_NO_COPY(JobObject)
}; };
} }

View File

@ -30,44 +30,28 @@
#define NOMINMAX #define NOMINMAX
#define WIN32_LEAN_AND_MEAN 1 #define WIN32_LEAN_AND_MEAN 1
#include <Windows.h> #include <Windows.h>
#include <MMSystem.h>
#include <ShellAPI.h>
#include <WinInet.h>
namespace MUtils //Utilities
{ #define PTR2HANDLE(X) reinterpret_cast<HANDLE>((X))
class JobObject_Private #define HANDLE2PTR(X) reinterpret_cast<uintptr_t>((X))
{
friend class JobObject;
protected:
JobObject_Private(void)
{
m_hJobObject = NULL;
}
HANDLE m_hJobObject;
};
}
MUtils::JobObject::JobObject(void) MUtils::JobObject::JobObject(void)
: :
p(new JobObject_Private()) m_jobPtr(NULL)
{ {
const HANDLE jobObject = CreateJobObject(NULL, NULL); const HANDLE jobObject = CreateJobObject(NULL, NULL);
if((jobObject != NULL) && (jobObject != INVALID_HANDLE_VALUE)) if((jobObject != NULL) && (jobObject != INVALID_HANDLE_VALUE))
{ {
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedLimitInfo; JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedLimitInfo;
memset(&jobExtendedLimitInfo, 0, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)); memset(&jobExtendedLimitInfo, 0, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
memset(&jobExtendedLimitInfo.BasicLimitInformation, 0, sizeof(JOBOBJECT_BASIC_LIMIT_INFORMATION));
jobExtendedLimitInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION; jobExtendedLimitInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION;
if(SetInformationJobObject(jobObject, JobObjectExtendedLimitInformation, &jobExtendedLimitInfo, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION))) if(SetInformationJobObject(jobObject, JobObjectExtendedLimitInformation, &jobExtendedLimitInfo, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)))
{ {
p->m_hJobObject = jobObject; m_jobPtr = HANDLE2PTR(jobObject);
} }
else else
{ {
qWarning("Failed to set job object information!"); qWarning("Failed to set up job object limit information!");
CloseHandle(jobObject); CloseHandle(jobObject);
} }
} }
@ -79,24 +63,27 @@ MUtils::JobObject::JobObject(void)
MUtils::JobObject::~JobObject(void) MUtils::JobObject::~JobObject(void)
{ {
if(p->m_hJobObject) if(m_jobPtr)
{ {
CloseHandle(p->m_hJobObject); CloseHandle(PTR2HANDLE(m_jobPtr));
p->m_hJobObject = NULL; m_jobPtr = NULL;
} }
delete p;
} }
bool MUtils::JobObject::addProcessToJob(const QProcess *proc) bool MUtils::JobObject::isObjectCreated(void)
{ {
if(!p->m_hJobObject) return (bool) m_jobPtr;
}
bool MUtils::JobObject::addProcessToJob(const QProcess *const process)
{
if(!m_jobPtr)
{ {
qWarning("Cannot assign process to job: No job bject available!"); qWarning("Cannot assign process to job: No job bject available!");
return false; return false;
} }
if(Q_PID pid = proc->pid()) if(const Q_PID pid = process->pid())
{ {
DWORD exitCode; DWORD exitCode;
if(!GetExitCodeProcess(pid->hProcess, &exitCode)) if(!GetExitCodeProcess(pid->hProcess, &exitCode))
@ -104,19 +91,16 @@ bool MUtils::JobObject::addProcessToJob(const QProcess *proc)
qWarning("Cannot assign process to job: Failed to query process status!"); qWarning("Cannot assign process to job: Failed to query process status!");
return false; return false;
} }
if(exitCode != STILL_ACTIVE) if(exitCode != STILL_ACTIVE)
{ {
qWarning("Cannot assign process to job: Process is not running anymore!"); qWarning("Cannot assign process to job: Process is not running anymore!");
return false; return false;
} }
if(!AssignProcessToJobObject(PTR2HANDLE(m_jobPtr), pid->hProcess))
if(!AssignProcessToJobObject(p->m_hJobObject, pid->hProcess))
{ {
qWarning("Failed to assign process to job object!"); qWarning("Failed to assign process to job object!");
return false; return false;
} }
return true; return true;
} }
else else
@ -126,11 +110,11 @@ bool MUtils::JobObject::addProcessToJob(const QProcess *proc)
} }
} }
bool MUtils::JobObject::terminateJob(unsigned int exitCode) bool MUtils::JobObject::terminateJob(const quint32 &exitCode)
{ {
if(p->m_hJobObject) if(m_jobPtr)
{ {
if(TerminateJobObject(p->m_hJobObject, exitCode)) if(TerminateJobObject(PTR2HANDLE(m_jobPtr), exitCode))
{ {
return true; return true;
} }