OSDN Git Service

Happy new year 2015 !!!
[x264-launcher/x264-launcher.git] / src / job_object.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
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.
10 //
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.
15 //
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.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "job_object.h"
24
25 #include "global.h"
26
27 #include <QProcess>
28
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>
36
37 JobObject::JobObject(void)
38 :
39         m_hJobObject(NULL)
40 {
41         HANDLE jobObject = CreateJobObject(NULL, NULL);
42         if((jobObject != NULL) && (jobObject != INVALID_HANDLE_VALUE))
43         {
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)))
49                 {
50                         m_hJobObject = jobObject;
51                 }
52                 else
53                 {
54                         qWarning("Failed to set job object information!");
55                         CloseHandle(jobObject);
56                 }
57         }
58         else
59         {
60                 qWarning("Failed to create the job object!");
61         }
62 }
63
64 JobObject::~JobObject(void)
65 {
66         if(m_hJobObject)
67         {
68                 CloseHandle(m_hJobObject);
69                 m_hJobObject = NULL;
70         }
71 }
72
73 bool JobObject::addProcessToJob(const QProcess *proc)
74 {
75         if(!m_hJobObject)
76         {
77                 qWarning("Cannot assign process to job: No job bject available!");
78                 return false;
79         }
80
81         if(Q_PID pid = proc->pid())
82         {
83                 DWORD exitCode;
84                 if(!GetExitCodeProcess(pid->hProcess, &exitCode))
85                 {
86                         qWarning("Cannot assign process to job: Failed to query process status!");
87                         return false;
88                 }
89
90                 if(exitCode != STILL_ACTIVE)
91                 {
92                         qWarning("Cannot assign process to job: Process is not running anymore!");
93                         return false;
94                 }
95
96                 if(!AssignProcessToJobObject(m_hJobObject, pid->hProcess))
97                 {
98                         qWarning("Failed to assign process to job object!");
99                         return false;
100                 }
101
102                 return true;
103         }
104         else
105         {
106                 qWarning("Cannot assign process to job: Process handle not available!");
107                 return false;
108         }
109 }
110
111 bool JobObject::terminateJob(unsigned int exitCode)
112 {
113         if(m_hJobObject)
114         {
115                 if(TerminateJobObject(m_hJobObject, exitCode))
116                 {
117                         return true;
118                 }
119                 else
120                 {
121                         qWarning("Failed to terminate job object!");
122                         return false;
123                 }
124         }
125         else
126         {
127                 qWarning("Cannot assign process to job: No job bject available!");
128                 return false;
129         }
130 }