Some clean-up and simplification for JobObject class.
[MUtilities.git] / src / JobObject_Win32.cpp
blob2d1c18ea395c3dc24f88d229079655bd71475dbf
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 //Internal
24 #include <MUtils/JobObject.h>
26 //Qt
27 #include <QProcess>
29 //Windows includes
30 #define NOMINMAX
31 #define WIN32_LEAN_AND_MEAN 1
32 #include <Windows.h>
34 //Utilities
35 #define PTR2HANDLE(X) reinterpret_cast<HANDLE>((X))
36 #define HANDLE2PTR(X) reinterpret_cast<uintptr_t>((X))
38 MUtils::JobObject::JobObject(void)
40 m_jobPtr(NULL)
42 const HANDLE jobObject = CreateJobObject(NULL, NULL);
43 if((jobObject != NULL) && (jobObject != INVALID_HANDLE_VALUE))
45 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedLimitInfo;
46 memset(&jobExtendedLimitInfo, 0, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
47 jobExtendedLimitInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION;
48 if(SetInformationJobObject(jobObject, JobObjectExtendedLimitInformation, &jobExtendedLimitInfo, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)))
50 m_jobPtr = HANDLE2PTR(jobObject);
52 else
54 qWarning("Failed to set up job object limit information!");
55 CloseHandle(jobObject);
58 else
60 qWarning("Failed to create the job object!");
64 MUtils::JobObject::~JobObject(void)
66 if(m_jobPtr)
68 CloseHandle(PTR2HANDLE(m_jobPtr));
69 m_jobPtr = NULL;
73 bool MUtils::JobObject::isObjectCreated(void)
75 return (bool) m_jobPtr;
78 bool MUtils::JobObject::addProcessToJob(const QProcess *const process)
80 if(!m_jobPtr)
82 qWarning("Cannot assign process to job: No job bject available!");
83 return false;
86 if(const Q_PID pid = process->pid())
88 DWORD exitCode;
89 if(!GetExitCodeProcess(pid->hProcess, &exitCode))
91 qWarning("Cannot assign process to job: Failed to query process status!");
92 return false;
94 if(exitCode != STILL_ACTIVE)
96 qWarning("Cannot assign process to job: Process is not running anymore!");
97 return false;
99 if(!AssignProcessToJobObject(PTR2HANDLE(m_jobPtr), pid->hProcess))
101 qWarning("Failed to assign process to job object!");
102 return false;
104 return true;
106 else
108 qWarning("Cannot assign process to job: Process handle not available!");
109 return false;
113 bool MUtils::JobObject::terminateJob(const quint32 &exitCode)
115 if(m_jobPtr)
117 if(TerminateJobObject(PTR2HANDLE(m_jobPtr), exitCode))
119 return true;
121 else
123 qWarning("Failed to terminate job object!");
124 return false;
127 else
129 qWarning("Cannot assign process to job: No job bject available!");
130 return false;