summaryrefslogtreecommitdiff
path: root/graphics/asymptote/win32helpers.cc
blob: c8a5cfe65e64a9ab1e78f2926120981e69f37d13 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
 * @file win32helpers.cc
 * @brief Windows-specific utility functions
 * @author Supakorn "Jamie" Rassameemasmuang (jamievlin [at] outlook.com)
 */

#if defined(_WIN32)
#include "win32helpers.h"
#include "errormsg.h"
#include <shellapi.h>

using camp::reportError;

namespace camp::w32
{

bool checkShellExecuteResult(INT_PTR const shellExecResult, bool const reportWarning)
{
  switch (shellExecResult)
  {
    // see https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea
    // ERROR_FILE_NOT_FOUND and ERROR_PATH_NOT_FOUND shares the same error code as
    // SE_ERR_FNF and SE_ERR_PNF, respectively
    case 0:
    case ERROR_BAD_FORMAT:
    case SE_ERR_ACCESSDENIED:
    case SE_ERR_ASSOCINCOMPLETE:
    case SE_ERR_DDEBUSY:
    case SE_ERR_DDEFAIL:
    case SE_ERR_DDETIMEOUT:
    case SE_ERR_DLLNOTFOUND:
    case SE_ERR_FNF:
    case SE_ERR_NOASSOC:
    case SE_ERR_OOM:
    case SE_ERR_PNF:
    case SE_ERR_SHARE:
    {
      if (reportWarning)
      {
        DWORD const errorCode= GetLastError();
        ostringstream msg;
        msg << "Error code: 0x" << std::hex << errorCode << std::dec << "; message: " << getErrorMessage(errorCode);
        camp::reportWarning(msg.str());
      }
    }
      return false;
    default:
      return true;
  }
}

void reportAndFailWithLastError(string const& message)
{
  DWORD errorCode= GetLastError();
  ostringstream msg;
  msg << message << "; error code = 0x" << std::hex << errorCode << std::dec
      << "; Windows Message: " << getErrorMessage(errorCode);
  reportError(msg);
}

void checkResult(BOOL result, string const& message)
{
  if (!result)
  {
    reportAndFailWithLastError(message);
  }
}

void checkLStatus(LSTATUS result, string const& message)
{
  checkResult(result == ERROR_SUCCESS, message);
}

bool isProcessRunning(DWORD const& pid)
{
  if (pid == 0)
  {
    return true; // system idle is always running
  }

  HandleRaiiWrapper const processHandle(OpenProcess(PROCESS_QUERY_INFORMATION, false, pid));
  if (processHandle.getHandle() == nullptr)
  {
    // handle not in system, returns false
    return false;
  }

  DWORD exitCode=999;
  if (GetExitCodeProcess(processHandle.getHandle(), &exitCode))
  {
    if (exitCode == STILL_ACTIVE)
    {
      return true;
    }
    else
    {
      return false;
    }
  }

  return false;
}

#pragma region RegKeyWrapper

RegKeyWrapper::RegKeyWrapper(HKEY const& regKey)
    : key(regKey)
{
}
RegKeyWrapper::RegKeyWrapper() : key(nullptr)
{
}
RegKeyWrapper::~RegKeyWrapper()
{
  closeExistingKey();
}
RegKeyWrapper::RegKeyWrapper(RegKeyWrapper&& other) noexcept
        : key(std::exchange(other.key, nullptr))
{
}

RegKeyWrapper& RegKeyWrapper::operator=(RegKeyWrapper&& other) noexcept
{
  if (this != &other)
  {
    closeExistingKey();
    this->key = std::exchange(other.key, nullptr);
  }
  return *this;
}

HKEY RegKeyWrapper::getKey() const
{
  return key;
}

void RegKeyWrapper::closeExistingKey()
{
  if (this->key != nullptr)
  {
    RegCloseKey(this->key);
    this->key = nullptr;
  }
}

PHKEY RegKeyWrapper::put()
{
  closeExistingKey();
  return &(this->key);
}
void RegKeyWrapper::release()
{
  this->key = nullptr;
}

#pragma endregion

#pragma region HandleRaiiWrapper

HandleRaiiWrapper::HandleRaiiWrapper(HANDLE const& handle)
    : hd(handle)
{
}

HandleRaiiWrapper::~HandleRaiiWrapper()
{
  if (hd)
  {
    if (!CloseHandle(hd))
    {
      cerr << "Warning: Cannot close handle" << endl;
    }
  }
}

HandleRaiiWrapper::HandleRaiiWrapper(HandleRaiiWrapper&& other) noexcept
: hd(std::exchange(other.hd, nullptr))
{
}

HANDLE HandleRaiiWrapper::getHandle() const
{
  return hd;
}

LPHANDLE HandleRaiiWrapper::put()
{
  if (hd)
  {
    w32::checkResult(CloseHandle(hd));
    hd = nullptr;
  }

  return &hd;
}

#pragma endregion

string buildWindowsCmd(const mem::vector<string>& command)
{
  ostringstream out;
  for (auto it= command.begin(); it != command.end(); ++it)
  {
    out << '"' << *it << '"';
    if (std::next(it) != command.end())
    {
      out << ' ';
    }
  }

  return out.str();
}

string getErrorMessage(DWORD const& errorCode)
{
  LPSTR messageOut= nullptr;
  auto ret = FormatMessageA(
          FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
          nullptr,
          errorCode,
          MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
          reinterpret_cast<LPSTR>(&messageOut),
          0,
          nullptr
  );

  if (ret == 0)
  {
    return "Cannot determine error message";
  }

  string retString(messageOut);
  LocalFree(messageOut);
  
  return retString;
}

}// namespace camp::w32

#endif