Happy new year 2015 !!!
[MUtilities.git] / src / JobObject_Win32.cpp
bloba5a89af2acff0a8294c4a4eb7ae7e64745bbc9e5
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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>
33 #include <MMSystem.h>
34 #include <ShellAPI.h>
35 #include <WinInet.h>
37 namespace MUtils
39 class JobObject_Private
41 friend class JobObject;
43 protected:
44 JobObject_Private(void)
46 m_hJobObject = NULL;
49 HANDLE m_hJobObject;
53 MUtils::JobObject::JobObject(void)
55 p(new JobObject_Private())
57 const HANDLE jobObject = CreateJobObject(NULL, NULL);
58 if((jobObject != NULL) && (jobObject != INVALID_HANDLE_VALUE))
60 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedLimitInfo;
61 memset(&jobExtendedLimitInfo, 0, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
62 memset(&jobExtendedLimitInfo.BasicLimitInformation, 0, sizeof(JOBOBJECT_BASIC_LIMIT_INFORMATION));
63 jobExtendedLimitInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION;
64 if(SetInformationJobObject(jobObject, JobObjectExtendedLimitInformation, &jobExtendedLimitInfo, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)))
66 p->m_hJobObject = jobObject;
68 else
70 qWarning("Failed to set job object information!");
71 CloseHandle(jobObject);
74 else
76 qWarning("Failed to create the job object!");
80 MUtils::JobObject::~JobObject(void)
82 if(p->m_hJobObject)
84 CloseHandle(p->m_hJobObject);
85 p->m_hJobObject = NULL;
88 delete p;
91 bool MUtils::JobObject::addProcessToJob(const QProcess *proc)
93 if(!p->m_hJobObject)
95 qWarning("Cannot assign process to job: No job bject available!");
96 return false;
99 if(Q_PID pid = proc->pid())
101 DWORD exitCode;
102 if(!GetExitCodeProcess(pid->hProcess, &exitCode))
104 qWarning("Cannot assign process to job: Failed to query process status!");
105 return false;
108 if(exitCode != STILL_ACTIVE)
110 qWarning("Cannot assign process to job: Process is not running anymore!");
111 return false;
114 if(!AssignProcessToJobObject(p->m_hJobObject, pid->hProcess))
116 qWarning("Failed to assign process to job object!");
117 return false;
120 return true;
122 else
124 qWarning("Cannot assign process to job: Process handle not available!");
125 return false;
129 bool MUtils::JobObject::terminateJob(unsigned int exitCode)
131 if(p->m_hJobObject)
133 if(TerminateJobObject(p->m_hJobObject, exitCode))
135 return true;
137 else
139 qWarning("Failed to terminate job object!");
140 return false;
143 else
145 qWarning("Cannot assign process to job: No job bject available!");
146 return false;