From 8d1dd6238f8d754aef1385409f4f97273a0d226f Mon Sep 17 00:00:00 2001 From: Tannin Date: Tue, 9 Feb 2016 20:54:54 +0100 Subject: fixed display of process name in unlock-widget --- src/organizercore.cpp | 63 +++++++++++++++++++++++++++++++++------------------ src/spawn.cpp | 44 +---------------------------------- 2 files changed, 42 insertions(+), 65 deletions(-) (limited to 'src') diff --git a/src/organizercore.cpp b/src/organizercore.cpp index d39235f5..ecadfb5d 100644 --- a/src/organizercore.cpp +++ b/src/organizercore.cpp @@ -978,40 +978,59 @@ void OrganizerCore::spawnBinary(const QFileInfo &binary, QCoreApplication::processEvents(); DWORD processExitCode; - DWORD retLen; - JOBOBJECT_BASIC_PROCESS_ID_LIST info; + + dialog->setProcessName( + QString::fromStdWString(getProcessName(::GetProcessId(processHandle)))); { DWORD currentProcess = 0UL; - bool isJobHandle = true; - - DWORD res = ::MsgWaitForMultipleObjects(1, &processHandle, false, 1000, QS_KEY | QS_MOUSE); - while ((res != WAIT_FAILED) && (res != WAIT_OBJECT_0) && !dialog->unlockClicked()) { - if (isJobHandle) { - if (::QueryInformationJobObject(processHandle, JobObjectBasicProcessIdList, &info, sizeof(info), &retLen) > 0) { - if (info.NumberOfProcessIdsInList == 0) { - break; - } else { - if (info.ProcessIdList[0] != currentProcess) { - currentProcess = info.ProcessIdList[0]; - dialog->setProcessName(ToQString(getProcessName(currentProcess))); + + DWORD res = ::MsgWaitForMultipleObjects(1, &processHandle, false, 1000, + QS_KEY | QS_MOUSE); + bool tryAgain = true; + while ((res != WAIT_FAILED) && !dialog->unlockClicked()) { + if (res == WAIT_OBJECT_0) { + // process ended, is there another one in the group? + static const DWORD maxCount = 5; + size_t numProcesses = maxCount; + LPDWORD processes = new DWORD[maxCount]; + if (::GetVFSProcessList(&numProcesses, processes)) { + bool found = false; + for (size_t i = 0; i < std::min(maxCount, numProcesses); + ++i) { + std::wstring processName = getProcessName(processes[i]); + if (!boost::starts_with(processName, L"ModOrganizer.exe")) { + currentProcess = processes[i]; + dialog->setProcessName(QString::fromStdWString(processName)); + processHandle + = ::OpenProcess(SYNCHRONIZE, FALSE, currentProcess); + found = true; } } - } else { - // the info-object I passed only provides space for 1 process id. but since this code only cares about whether there - // is more than one that's good enough. ERROR_MORE_DATA simply signals there are at least two processes running. - // any other error probably means the handle is a regular process handle, probably caused by running MO in a job without - // the right to break out. - if (::GetLastError() != ERROR_MORE_DATA) { - isJobHandle = false; + if (!found) { + // it's possible the previous process has deregistered before + // the new one has registered, so we should try one more time + // with a little delay + if (tryAgain) { + tryAgain = false; + QThread::msleep(500); + continue; + } else { + break; + } + } else { + tryAgain = true; } + } else { + break; } } // keep processing events so the app doesn't appear dead QCoreApplication::processEvents(); - res = ::MsgWaitForMultipleObjects(1, &processHandle, false, 1000, QS_KEY | QS_MOUSE); + res = ::MsgWaitForMultipleObjects(1, &processHandle, false, 1000, + QS_KEY | QS_MOUSE); } ::GetExitCodeProcess(processHandle, &processExitCode); diff --git a/src/spawn.cpp b/src/spawn.cpp index 19cf304c..fc0b448a 100644 --- a/src/spawn.cpp +++ b/src/spawn.cpp @@ -134,19 +134,6 @@ HANDLE startBinary(const QFileInfo &binary, HANDLE stdOut, HANDLE stdErr) { - JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobInfo; - - ::QueryInformationJobObject(nullptr, JobObjectExtendedLimitInformation, &jobInfo, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION), nullptr); - jobInfo.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_BREAKAWAY_OK; - - HANDLE jobObject = ::CreateJobObject(nullptr, nullptr); - - if (jobObject == nullptr) { - qWarning("failed to create job object: %lu", ::GetLastError()); - } else { - ::SetInformationJobObject(jobObject, JobObjectExtendedLimitInformation, &jobInfo, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)); - } - HANDLE processHandle, threadHandle; std::wstring binaryName = ToWString(QDir::toNativeSeparators(binary.absoluteFilePath())); std::wstring currentDirectoryName = ToWString(QDir::toNativeSeparators(currentDirectory.absolutePath())); @@ -181,36 +168,7 @@ HANDLE startBinary(const QFileInfo &binary, return INVALID_HANDLE_VALUE; } } -/* - if (hooked) { - try { - QFileInfo dllInfo(QApplication::applicationDirPath() + "/" + ToQString(AppConfig::hookDLLName())); - if (!dllInfo.exists()) { - reportError(QObject::tr("\"%1\" doesn't exist").arg(dllInfo.fileName())); - return INVALID_HANDLE_VALUE; - } - injectDLL(processHandle, threadHandle, - QDir::toNativeSeparators(dllInfo.canonicalFilePath()).toLocal8Bit().constData(), - ToWString(profileName).c_str(), logLevel); - } catch (const windows_error& e) { - reportError(QObject::tr("failed to inject dll into \"%1\": %2").arg(binary.fileName()).arg(e.what())); - ::TerminateProcess(processHandle, 1); - return INVALID_HANDLE_VALUE; - } -#ifdef _DEBUG - reportError("ready?"); -#endif // DEBUG - } - */ - - if (::AssignProcessToJobObject(jobObject, processHandle) == 0) { - qWarning("failed to assign to job object: %lu", ::GetLastError()); - ::CloseHandle(jobObject); - jobObject = processHandle; - } else { - ::CloseHandle(processHandle); - } ::CloseHandle(threadHandle); - return jobObject; + return processHandle; } -- cgit v1.3.1