-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRuntime_Win.cpp
More file actions
78 lines (60 loc) · 1.97 KB
/
Runtime_Win.cpp
File metadata and controls
78 lines (60 loc) · 1.97 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
73
74
75
76
77
78
#ifdef _WIN32
#include "MaaUtils/Runtime.h"
#include <memory>
#include "MaaUtils/Platform.h"
#include "MaaUtils/SafeWindows.hpp"
MAA_NS_BEGIN
static std::filesystem::path s_library_dir_cache;
static std::unique_ptr<std::remove_pointer_t<HMODULE>, decltype(&FreeLibrary)> s_dml_holder(nullptr, &FreeLibrary);
const std::filesystem::path& library_dir()
{
return s_library_dir_cache;
}
std::filesystem::path get_library_path(void* addr)
{
WCHAR path_buf[MAX_PATH + 5];
DWORD path_len = GetMappedFileNameW(GetCurrentProcess(), addr, path_buf, MAX_PATH);
if (path_len == 0) {
return {};
}
return { path_buf };
}
void init_library_dir(HINSTANCE hinstDLL)
{
WCHAR buffer[MAX_PATH] = { 0 };
GetModuleFileNameW(hinstDLL, buffer, MAX_PATH);
s_library_dir_cache = std::filesystem::path(buffer).parent_path();
// fix https://github.com/MaaXYZ/MaaFramework/issues/394
const auto dml_path = s_library_dir_cache / "DirectML.dll";
s_dml_holder = decltype(s_dml_holder)(LoadLibraryW(dml_path.c_str()), &FreeLibrary);
}
MAA_NS_END
// https://learn.microsoft.com/zh-cn/windows/win32/dlls/dllmain
BOOL WINAPI DllMain(
HINSTANCE hinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpvReserved) // reserved
{
// Perform actions based on the reason for calling.
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
// Initialize once for each new process.
// Return FALSE to fail DLL load.
MAA_NS::init_library_dir(hinstDLL);
break;
case DLL_THREAD_ATTACH:
// Do thread-specific initialization.
break;
case DLL_THREAD_DETACH:
// Do thread-specific cleanup.
break;
case DLL_PROCESS_DETACH:
if (lpvReserved != nullptr) {
break; // do not do cleanup if process termination scenario
}
// Perform any necessary cleanup.
break;
}
return TRUE; // Successful DLL_PROCESS_ATTACH.
}
#endif