Happy new year 2020!
[simple-x264-launcher.git] / src / job_object.cpp
blob71e66767f07d26e8df26faa523d76beb412c5806
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2020 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 #include "job_object.h"
25 #include "global.h"
27 #include <QProcess>
29 //Windows includes
30 #define NOMINMAX
31 #define WIN32_LEAN_AND_MEAN
32 #include <Windows.h>
33 #include <MMSystem.h>
34 #include <ShellAPI.h>
35 #include <WinInet.h>
37 JobObject::JobObject(void)
39 m_hJobObject(NULL)
41 HANDLE jobObject = CreateJobObject(NULL, NULL);
42 if((jobObject != NULL) && (jobObject != INVALID_HANDLE_VALUE))
44 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedLimitInfo;
45 memset(&jobExtendedLimitInfo, 0, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
46 memset(&jobExtendedLimitInfo.BasicLimitInformation, 0, sizeof(JOBOBJECT_BASIC_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_hJobObject = jobObject;
52 else
54 qWarning("Failed to set job object information!");
55 CloseHandle(jobObject);
58 else
60 qWarning("Failed to create the job object!");
64 JobObject::~JobObject(void)
66 if(m_hJobObject)
68 CloseHandle(m_hJobObject);
69 m_hJobObject = NULL;
73 bool JobObject::addProcessToJob(const QProcess *proc)
75 if(!m_hJobObject)
77 qWarning("Cannot assign process to job: No job bject available!");
78 return false;
81 if(Q_PID pid = proc->pid())
83 DWORD exitCode;
84 if(!GetExitCodeProcess(pid->hProcess, &exitCode))
86 qWarning("Cannot assign process to job: Failed to query process status!");
87 return false;
90 if(exitCode != STILL_ACTIVE)
92 qWarning("Cannot assign process to job: Process is not running anymore!");
93 return false;
96 if(!AssignProcessToJobObject(m_hJobObject, pid->hProcess))
98 qWarning("Failed to assign process to job object!");
99 return false;
102 return true;
104 else
106 qWarning("Cannot assign process to job: Process handle not available!");
107 return false;
111 bool JobObject::terminateJob(unsigned int exitCode)
113 if(m_hJobObject)
115 if(TerminateJobObject(m_hJobObject, exitCode))
117 return true;
119 else
121 qWarning("Failed to terminate job object!");
122 return false;
125 else
127 qWarning("Cannot assign process to job: No job bject available!");
128 return false;