From 7f4f4cafea5a196ddf824adf7c4e65cec5d44d88 Mon Sep 17 00:00:00 2001 From: Tannin Date: Tue, 17 Nov 2015 18:58:00 +0100 Subject: first work on interfacing with usvfs --- src/spawn.cpp | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) (limited to 'src/spawn.cpp') diff --git a/src/spawn.cpp b/src/spawn.cpp index c79714bb..ad6707e3 100644 --- a/src/spawn.cpp +++ b/src/spawn.cpp @@ -24,6 +24,7 @@ along with Mod Organizer. If not, see . #include #include #include +#include #include #include #include @@ -37,7 +38,8 @@ using namespace MOShared; static const int BUFSIZE = 4096; -static bool spawn(LPCWSTR binary, LPCWSTR arguments, LPCWSTR currentDirectory, bool suspended, +static bool spawn(LPCWSTR binary, LPCWSTR arguments, LPCWSTR currentDirectory, + bool suspended, bool hooked, HANDLE stdOut, HANDLE stdErr, HANDLE& processHandle, HANDLE& threadHandle) { @@ -85,15 +87,28 @@ static bool spawn(LPCWSTR binary, LPCWSTR arguments, LPCWSTR currentDirectory, b } PROCESS_INFORMATION pi; - BOOL success = ::CreateProcess(nullptr, - commandLine, - nullptr, nullptr, // no special process or thread attributes - inheritHandles, // inherit handles if we plan to use stdout or stderr reroute - CREATE_BREAKAWAY_FROM_JOB | (suspended ? CREATE_SUSPENDED : 0), // create suspended so I have time to inject the DLL - nullptr, // same environment as parent - currentDirectory, // current directory - &si, &pi // startup and process information - ); + BOOL success = FALSE; + if (hooked) { + success = ::CreateProcessHooked(nullptr, + commandLine, + nullptr, nullptr, // no special process or thread attributes + inheritHandles, // inherit handles if we plan to use stdout or stderr reroute + CREATE_BREAKAWAY_FROM_JOB, + nullptr, // same environment as parent + currentDirectory, // current directory + &si, &pi // startup and process information + ); + } else { + success = ::CreateProcess(nullptr, + commandLine, + nullptr, nullptr, // no special process or thread attributes + inheritHandles, // inherit handles if we plan to use stdout or stderr reroute + CREATE_BREAKAWAY_FROM_JOB, + nullptr, // same environment as parent + currentDirectory, // current directory + &si, &pi // startup and process information + ); + } ::SetEnvironmentVariable(TEXT("PATH"), oldPath.get()); @@ -136,8 +151,8 @@ HANDLE startBinary(const QFileInfo &binary, std::wstring currentDirectoryName = ToWString(QDir::toNativeSeparators(currentDirectory.absolutePath())); try { - if (!spawn(binaryName.c_str(), ToWString(arguments).c_str(), currentDirectoryName.c_str(), true, - stdOut, stdErr, processHandle, threadHandle)) { + if (!spawn(binaryName.c_str(), ToWString(arguments).c_str(), currentDirectoryName.c_str(), + true, hooked, stdOut, stdErr, processHandle, threadHandle)) { reportError(QObject::tr("failed to spawn \"%1\"").arg(binary.fileName())); return INVALID_HANDLE_VALUE; } @@ -165,7 +180,7 @@ HANDLE startBinary(const QFileInfo &binary, return INVALID_HANDLE_VALUE; } } - +/* if (hooked) { try { QFileInfo dllInfo(QApplication::applicationDirPath() + "/" + ToQString(AppConfig::hookDLLName())); @@ -185,6 +200,7 @@ HANDLE startBinary(const QFileInfo &binary, reportError("ready?"); #endif // DEBUG } + */ if (::AssignProcessToJobObject(jobObject, processHandle) == 0) { qWarning("failed to assign to job object: %lu", ::GetLastError()); @@ -194,10 +210,6 @@ HANDLE startBinary(const QFileInfo &binary, ::CloseHandle(processHandle); } - if (::ResumeThread(threadHandle) == (DWORD)-1) { - reportError(QObject::tr("failed to run \"%1\"").arg(binary.fileName())); - return INVALID_HANDLE_VALUE; - } ::CloseHandle(threadHandle); return jobObject; } -- cgit v1.3.1