summaryrefslogtreecommitdiff
path: root/graphics/asymptote/win32helpers.h
blob: 266a65f3bd09c4972ae66213a02dca9b6690ae2e (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
/**
 * @file win32helpers.h
 * @brief Windows-specific utility functions header
 * @author Supakorn "Jamie" Rassameemasmuang (jamievlin [at] outlook.com)
 */

#pragma once

#if defined(_WIN32)

#include <Windows.h>
#include "common.h"

namespace camp::w32
{

/**
 * Checks if result from ShellExecute/ShellExecuteEx indicates a successful execution or not.
 * This function does not raise an error in itself, but can generate a warning if specified
 */
bool checkShellExecuteResult(INT_PTR shellExecResult, bool reportWarning=false);

void checkResult(BOOL result, string const& message= "");
void checkLStatus(LSTATUS result, string const& message="");

string buildWindowsCmd(const mem::vector<string>& command);

string getErrorMessage(DWORD const& errorCode);

bool isProcessRunning(DWORD const& pid);

/** Reports error with message from GetLastError and the user specified message */
void reportAndFailWithLastError(string const& message);

/**
 * A simple wraper for <tt>HKEY</tt>
 */
class RegKeyWrapper
{
public:
  RegKeyWrapper(HKEY const& regKey);
  RegKeyWrapper();

  ~RegKeyWrapper();
  RegKeyWrapper(RegKeyWrapper const&) = delete;
  RegKeyWrapper& operator=(RegKeyWrapper const&) = delete;

  RegKeyWrapper(RegKeyWrapper&& other) noexcept;
  RegKeyWrapper& operator=(RegKeyWrapper&& other) noexcept;

  /**
   * @return The registry key held by the wrapper
   */
  [[nodiscard]]
  HKEY getKey() const;
  PHKEY put();

  void release();

private:
  void closeExistingKey();
  HKEY key;
};

class HandleRaiiWrapper
{
public:
    HandleRaiiWrapper() = default;
    HandleRaiiWrapper(HANDLE const& handle);
    ~HandleRaiiWrapper();

    HandleRaiiWrapper(HandleRaiiWrapper const&) = delete;
    HandleRaiiWrapper& operator=(HandleRaiiWrapper const&) = delete;

    HandleRaiiWrapper(HandleRaiiWrapper&& other) noexcept;

    // already hold a handle, should not consume another one
    HandleRaiiWrapper& operator=(HandleRaiiWrapper&& other) = delete;

    [[nodiscard]]
    HANDLE getHandle() const;

    LPHANDLE put();

private:
    HANDLE hd = nullptr;

};

} // namespace camp::w32

#endif