OSDN Git Service

Moved JobObject as well as the remaining GUI functions into the MUtilities library.
[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
89 bool MUtils::JobObject::addProcessToJob(const QProcess *proc)
90 {
91         if(!p->m_hJobObject)
92         {
93                 qWarning("Cannot assign process to job: No job bject available!");
94                 return false;
95         }
96
97         if(Q_PID pid = proc->pid())
98         {
99                 DWORD exitCode;
100                 if(!GetExitCodeProcess(pid->hProcess, &exitCode))
101                 {
102                         qWarning("Cannot assign process to job: Failed to query process status!");
103                         return false;
104                 }
105
106                 if(exitCode != STILL_ACTIVE)
107                 {
108                         qWarning("Cannot assign process to job: Process is not running anymore!");
109                         return false;
110                 }
111
112                 if(!AssignProcessToJobObject(p->m_hJobObject, pid->hProcess))
113                 {
114                         qWarning("Failed to assign process to job object!");
115                         return false;
116                 }
117
118                 return true;
119         }
120         else
121         {
122                 qWarning("Cannot assign process to job: Process handle not available!");
123                 return false;
124         }
125 }
126
127 bool MUtils::JobObject::terminateJob(unsigned int exitCode)
128 {
129         if(p->m_hJobObject)
130         {
131                 if(TerminateJobObject(p->m_hJobObject, exitCode))
132                 {
133                         return true;
134                 }
135                 else
136                 {
137                         qWarning("Failed to terminate job object!");
138                         return false;
139                 }
140         }
141         else
142         {
143                 qWarning("Cannot assign process to job: No job bject available!");
144                 return false;
145         }
146 }