-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathHelpers.h
93 lines (77 loc) · 1.83 KB
/
Helpers.h
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
// <copyright file="Helpers.h" company="Microsoft Corporation">
// Copyright (C) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt in the project root for license information.
// </copyright>
#pragma once
#define __nameof(x) L#x
#define FALSESTRING L"False"
#define TRUESTRING L"True"
struct ModuleDeleter
{
void operator()(_In_ HMODULE* phModule)
{
if (phModule && *phModule)
{
::FreeLibrary(*phModule);
}
}
};
typedef std::unique_ptr<HMODULE, ModuleDeleter> module_ptr;
struct SafeArrayDeleter
{
void operator()(_In_ LPSAFEARRAY* ppsa)
{
if (ppsa && *ppsa)
{
if ((*ppsa)->cLocks)
{
::SafeArrayUnlock(*ppsa);
}
::SafeArrayDestroy(*ppsa);
}
}
};
typedef std::unique_ptr<LPSAFEARRAY, SafeArrayDeleter> safearray_ptr;
class win32_exception :
public std::exception
{
public:
win32_exception(_In_ DWORD code, _In_z_ const char* what) noexcept :
std::exception(what),
m_code(code)
{
}
win32_exception(_In_ const win32_exception& obj) noexcept :
std::exception(obj)
{
m_code = obj.m_code;
}
DWORD code() const noexcept
{
return m_code;
}
private:
DWORD m_code;
};
class CoInitializer
{
public:
CoInitializer()
{
hr = ::CoInitialize(nullptr);
if (FAILED(hr))
{
throw win32_exception(hr, "failed to initialize COM");
}
}
~CoInitializer()
{
if (SUCCEEDED(hr))
{
::CoUninitialize();
}
}
private:
HRESULT hr;
};
std::wostream& operator<<(_In_ std::wostream& os, _In_ const variant_t& var);