summaryrefslogtreecommitdiff
path: root/src/spawn.cpp
blob: 13e74f939dc6614fe7244cb7ed9e70dbb5237aed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Copyright (C) 2012 Sebastian Herbord. All rights reserved.

This file is part of Mod Organizer.

Mod Organizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Mod Organizer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Mod Organizer.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "spawn.h"
#include "report.h"
#include "utility.h"
#include <gameinfo.h>
#include <inject.h>
#include <appconfig.h>
#include <windows_error.h>
#include <QApplication>


bool spawn(LPCWSTR binary, LPCWSTR arguments, LPCWSTR currentDirectory, bool suspended, HANDLE& processHandle, HANDLE& threadHandle)
{
  STARTUPINFO si;
  ::ZeroMemory(&si, sizeof(si));
  si.cb = sizeof(si);
  int length = wcslen(binary) + wcslen(arguments) + 4;
  wchar_t *commandLine = NULL;
  if (arguments[0] != L'\0') {
    commandLine = new wchar_t[length];
    _snwprintf(commandLine, length, L"\"%ls\" %ls", binary, arguments);
  } else {
    commandLine = new wchar_t[length];
    _snwprintf(commandLine, length, L"\"%ls\"", binary);

  }

  PROCESS_INFORMATION pi;
  BOOL success = ::CreateProcess(NULL,
                                 commandLine,
                                 NULL, NULL,       // no special process or thread attributes
                                 FALSE,            // don't inherit handle
                                 suspended ? CREATE_SUSPENDED : 0, // create suspended so I have time to inject the DLL
                                 NULL,             // same environment as parent
                                 currentDirectory, // current directory
                                 &si, &pi          // startup and process information
                                 );

  delete [] commandLine;

  if (!success) {
    throw windows_error("failed to start process");
  }

  processHandle = pi.hProcess;
  threadHandle = pi.hThread;
  return true;
}


HANDLE startBinary(const QFileInfo &binary, const QString &arguments, const QString& profileName, int logLevel, const QDir &currentDirectory, bool hooked)
{
  HANDLE processHandle, threadHandle;
  std::wstring binaryName = ToWString(QDir::toNativeSeparators(binary.absoluteFilePath()));
  std::wstring currentDirectoryName = ToWString(QDir::toNativeSeparators(currentDirectory.absolutePath()));

  try {
    if (!spawn(binaryName.c_str(), ToWString(arguments).c_str(), currentDirectoryName.c_str(), hooked, processHandle, threadHandle)) {
      reportError(QObject::tr("failed to spawn \"%1\"").arg(binary.fileName()));
      return INVALID_HANDLE_VALUE;
    }
  } catch (const windows_error &e) {
    reportError(QObject::tr("failed to spawn \"%1\": %2").arg(binary.fileName()).arg(e.what()));
    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 (::ResumeThread(threadHandle) == (DWORD)-1) {
      reportError(QObject::tr("failed to run \"%1\"").arg(binary.fileName()));
      return INVALID_HANDLE_VALUE;
    }
  }
  return processHandle;
}