-
Notifications
You must be signed in to change notification settings - Fork 430
Expand file tree
/
Copy pathExportLoader.h
More file actions
72 lines (65 loc) · 1.95 KB
/
ExportLoader.h
File metadata and controls
72 lines (65 loc) · 1.95 KB
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
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.
#if !defined(__EXPORTLOADER_H)
#define __EXPORTLOADER_H
namespace ExportLoader
{
inline HRESULT Load(PCWSTR moduleName, _Out_ HMODULE* module)
{
auto hmodule{ ::LoadLibraryExW(moduleName, nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32) };
if (!hmodule)
{
const auto rc{ GetLastError() };
RETURN_HR_IF_MSG(HRESULT_FROM_WIN32(rc), rc != ERROR_MOD_NOT_FOUND, "%ls", moduleName);
RETURN_HR(E_NOTIMPL);
}
*module = hmodule;
return S_OK;
}
inline HMODULE Load(PCWSTR moduleName)
{
auto hmodule{ ::LoadLibraryExW(moduleName, nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32) };
if (!hmodule)
{
const auto rc{ GetLastError() };
THROW_HR_IF_MSG(HRESULT_FROM_WIN32(rc), rc != ERROR_MOD_NOT_FOUND, "%ls", moduleName);
THROW_HR(E_NOTIMPL);
}
return hmodule;
}
template <typename T>
inline HRESULT GetFunctionIfExists(HMODULE module, PCSTR functionName, _Out_ T* function)
{
auto fn{ reinterpret_cast<T>(::GetProcAddress(module, functionName)) };
if (!fn)
{
const auto rc{ GetLastError() };
RETURN_HR_IF_MSG(HRESULT_FROM_WIN32(rc), rc != ERROR_PROC_NOT_FOUND, "%hs", functionName);
}
*function = fn;
return S_OK;
}
template <typename T>
inline T GetFunctionIfExists(HMODULE module, PCSTR functionName)
{
auto function{ reinterpret_cast<T>(::GetProcAddress(module, functionName)) };
if (!function)
{
const auto rc{ GetLastError() };
if (rc == ERROR_PROC_NOT_FOUND)
{
return nullptr;
}
THROW_WIN32_MSG(rc, "%hs", functionName);
}
return function;
}
template <typename T>
inline T GetFunction(HMODULE module, PCSTR functionName)
{
auto function{ GetFunctionIfExists<T>(module, functionName) };
THROW_HR_IF_NULL_MSG(E_NOTIMPL, function, "%hs", functionName);
return function;
}
}
#endif // defined(__EXPORTLOADER_H)