OSDN Git Service

b39a54acf9b48847615e9d3564315c7b38dffc83
[mutilities/MUtilities.git] / src / JobObject_Win32.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 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 //Internal
24 #include <MUtils/JobObject.h>
25
26 //Qt
27 #include <QProcess>
28
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>
36
37 namespace MUtils
38 {
39         class JobObject_Private
40         {
41                 friend class JobObject;
42
43         protected:
44                 JobObject_Private(void)
45                 {
46                         m_hJobObject = NULL;
47                 }
48
49                 HANDLE m_hJobObject;
50         };
51 }
52
53 MUtils::JobObject::JobObject(void)
54 :
55         p(new JobObject_Private())
56 {
57         const HANDLE jobObject = CreateJobObject(NULL, NULL);
58         if((jobObject != NULL) && (jobObject != INVALID_HANDLE_VALUE))
59         {
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)))
65                 {
66                         p->m_hJobObject = jobObject;
67                 }
68                 else
69                 {
70                         qWarning("Failed to set job object information!");
71                         CloseHandle(jobObject);
72                 }
73         }
74         else
75         {
76                 qWarning("Failed to create the job object!");
77         }
78 }
79
80 MUtils::JobObject::~JobObject(void)
81 {
82         if(p->m_hJobObject)
83         {
84                 CloseHandle(p->m_hJobObject);
85                 p->m_hJobObject = NULL;
86         }
87
88         delete p;
89 }
90
91 bool MUtils::JobObject::addProcessToJob(const QProcess *proc)
92 {
93         if(!p->m_hJobObject)
94         {
95                 qWarning("Cannot assign process to job: No job bject available!");
96                 return false;
97         }
98
99         if(Q_PID pid = proc->pid())
100         {
101                 DWORD exitCode;
102                 if(!GetExitCodeProcess(pid->hProcess, &exitCode))
103                 {
104                         qWarning("Cannot assign process to job: Failed to query process status!");
105                         return false;
106                 }
107
108                 if(exitCode != STILL_ACTIVE)
109                 {
110                         qWarning("Cannot assign process to job: Process is not running anymore!");
111                         return false;
112                 }
113
114                 if(!AssignProcessToJobObject(p->m_hJobObject, pid->hProcess))
115                 {
116                         qWarning("Failed to assign process to job object!");
117                         return false;
118                 }
119
120                 return true;
121         }
122         else
123         {
124                 qWarning("Cannot assign process to job: Process handle not available!");
125                 return false;
126         }
127 }
128
129 bool MUtils::JobObject::terminateJob(unsigned int exitCode)
130 {
131         if(p->m_hJobObject)
132         {
133                 if(TerminateJobObject(p->m_hJobObject, exitCode))
134                 {
135                         return true;
136                 }
137                 else
138                 {
139                         qWarning("Failed to terminate job object!");
140                         return false;
141                 }
142         }
143         else
144         {
145                 qWarning("Cannot assign process to job: No job bject available!");
146                 return false;
147         }
148 }