1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
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 ///////////////////////////////////////////////////////////////////////////////
24 #include <MUtils/JobObject.h>
31 #define WIN32_LEAN_AND_MEAN 1
35 #define PTR2HANDLE(X) reinterpret_cast<HANDLE>((X))
36 #define HANDLE2PTR(X) reinterpret_cast<uintptr_t>((X))
38 MUtils::JobObject::JobObject(void)
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
);
54 qWarning("Failed to set up job object limit information!");
55 CloseHandle(jobObject
);
60 qWarning("Failed to create the job object!");
64 MUtils::JobObject::~JobObject(void)
68 CloseHandle(PTR2HANDLE(m_jobPtr
));
73 bool MUtils::JobObject::isObjectCreated(void)
75 return (bool) m_jobPtr
;
78 bool MUtils::JobObject::addProcessToJob(const QProcess
*const process
)
82 qWarning("Cannot assign process to job: No job bject available!");
86 if(const Q_PID pid
= process
->pid())
89 if(!GetExitCodeProcess(pid
->hProcess
, &exitCode
))
91 qWarning("Cannot assign process to job: Failed to query process status!");
94 if(exitCode
!= STILL_ACTIVE
)
96 qWarning("Cannot assign process to job: Process is not running anymore!");
99 if(!AssignProcessToJobObject(PTR2HANDLE(m_jobPtr
), pid
->hProcess
))
101 qWarning("Failed to assign process to job object!");
108 qWarning("Cannot assign process to job: Process handle not available!");
113 bool MUtils::JobObject::terminateJob(const quint32
&exitCode
)
117 if(TerminateJobObject(PTR2HANDLE(m_jobPtr
), exitCode
))
123 qWarning("Failed to terminate job object!");
129 qWarning("Cannot assign process to job: No job bject available!");