1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
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.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Tool_Abstract.h"
28 #include <QMutexLocker>
30 #include <QProcessEnvironment>
34 * Win32 API definitions
36 typedef HANDLE (WINAPI *CreateJobObjectFun)(__in_opt LPSECURITY_ATTRIBUTES lpJobAttributes, __in_opt LPCSTR lpName);
37 typedef BOOL (WINAPI *SetInformationJobObjectFun)(__in HANDLE hJob, __in JOBOBJECTINFOCLASS JobObjectInformationClass, __in_bcount(cbJobObjectInformationLength) LPVOID lpJobObjectInformation, __in DWORD cbJobObjectInformationLength);
38 typedef BOOL (WINAPI *AssignProcessToJobObjectFun)(__in HANDLE hJob, __in HANDLE hProcess);
43 QMutex *AbstractTool::m_mutex_startProcess = NULL;
44 HANDLE AbstractTool::m_handle_jobObject = NULL;
49 AbstractTool::AbstractTool(void)
51 static CreateJobObjectFun CreateJobObjectPtr = NULL;
52 static SetInformationJobObjectFun SetInformationJobObjectPtr = NULL;
54 if(!m_mutex_startProcess)
56 m_mutex_startProcess = new QMutex();
59 if(!m_handle_jobObject)
61 if(!CreateJobObjectPtr || !SetInformationJobObjectPtr)
63 QLibrary Kernel32Lib("kernel32.dll");
64 CreateJobObjectPtr = (CreateJobObjectFun) Kernel32Lib.resolve("CreateJobObjectA");
65 SetInformationJobObjectPtr = (SetInformationJobObjectFun) Kernel32Lib.resolve("SetInformationJobObject");
67 if(CreateJobObjectPtr && SetInformationJobObjectPtr)
69 m_handle_jobObject = CreateJobObjectPtr(NULL, NULL);
70 if(m_handle_jobObject == INVALID_HANDLE_VALUE)
72 m_handle_jobObject = NULL;
74 if(m_handle_jobObject)
76 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedLimitInfo;
77 memset(&jobExtendedLimitInfo, 0, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
78 jobExtendedLimitInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION;
79 SetInformationJobObjectPtr(m_handle_jobObject, JobObjectExtendedLimitInformation, &jobExtendedLimitInfo, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
90 AbstractTool::~AbstractTool(void)
92 if(m_handle_jobObject)
94 CloseHandle(m_handle_jobObject);
95 m_handle_jobObject = NULL;
100 * Initialize and launch process object
102 bool AbstractTool::startProcess(QProcess &process, const QString &program, const QStringList &args)
104 static AssignProcessToJobObjectFun AssignProcessToJobObjectPtr = NULL;
106 QMutexLocker lock(m_mutex_startProcess);
107 emit messageLogged(commandline2string(program, args) + "\n");
109 QProcessEnvironment env = process.processEnvironment();
110 if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment();
111 env.insert("TEMP", QDir::toNativeSeparators(lamexp_temp_folder2()));
112 env.insert("TMP", QDir::toNativeSeparators(lamexp_temp_folder2()));
113 process.setProcessEnvironment(env);
115 if(!AssignProcessToJobObjectPtr)
117 QLibrary Kernel32Lib("kernel32.dll");
118 AssignProcessToJobObjectPtr = (AssignProcessToJobObjectFun) Kernel32Lib.resolve("AssignProcessToJobObject");
121 process.setProcessChannelMode(QProcess::MergedChannels);
122 process.setReadChannel(QProcess::StandardOutput);
123 process.start(program, args);
125 if(process.waitForStarted())
128 if(AssignProcessToJobObjectPtr)
130 AssignProcessToJobObjectPtr(m_handle_jobObject, process.pid()->hProcess);
132 if(!SetPriorityClass(process.pid()->hProcess, BELOW_NORMAL_PRIORITY_CLASS))
134 SetPriorityClass(process.pid()->hProcess, IDLE_PRIORITY_CLASS);
141 emit statusUpdated(0);
142 m_firstLaunch = false;
148 emit messageLogged("Process creation has failed :-(");
149 QString errorMsg= process.errorString().trimmed();
150 if(!errorMsg.isEmpty()) emit messageLogged(errorMsg);
153 process.waitForFinished(-1);
158 * Convert program arguments to single string
160 QString AbstractTool::commandline2string(const QString &program, const QStringList &arguments)
162 QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
164 for(int i = 0; i < arguments.count(); i++)
166 commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));
173 * Convert long path to short path
175 QString AbstractTool::pathToShort(const QString &longPath)
178 DWORD buffSize = GetShortPathNameW(reinterpret_cast<const wchar_t*>(longPath.utf16()), NULL, NULL);
182 wchar_t *buffer = new wchar_t[buffSize];
183 DWORD result = GetShortPathNameW(reinterpret_cast<const wchar_t*>(longPath.utf16()), buffer, buffSize);
185 if(result > 0 && result < buffSize)
187 shortPath = QString::fromUtf16(reinterpret_cast<const unsigned short*>(buffer));
193 return (shortPath.isEmpty() ? longPath : shortPath);