-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdllmain.cpp
More file actions
132 lines (106 loc) · 3.79 KB
/
dllmain.cpp
File metadata and controls
132 lines (106 loc) · 3.79 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
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
#include <string>
#include <Windows.h>
#include <Shlwapi.h>
#define NETHOST_USE_AS_STATIC
#include "inc\hostfxr.h"
#include <vector>
#pragma comment(lib, "shlwapi.lib")
#pragma warning(disable : 4996)
hostfxr_initialize_for_dotnet_command_line_fn init_cmdline;
hostfxr_close_fn close_fptr;
hostfxr_run_app_fn run_fptr;
//OUTPUT DLL IS "DXGI.dll"
//DO *NOT* CHANGE.
std::wstring GetExecutableDir()
{
WCHAR buf[MAX_PATH];
GetModuleFileName(nullptr, buf, MAX_PATH);
PathRemoveFileSpec(buf);
return buf;
}
bool load_hostfxr()
{
auto fxr_path = GetExecutableDir() + L"\\hostfxr.dll";
HMODULE lib = LoadLibraryW(fxr_path.c_str());
init_cmdline = (hostfxr_initialize_for_dotnet_command_line_fn)GetProcAddress(lib, "hostfxr_initialize_for_dotnet_command_line");
run_fptr = (hostfxr_run_app_fn)GetProcAddress(lib, "hostfxr_run_app");
close_fptr = (hostfxr_close_fn)GetProcAddress(lib, "hostfxr_close");
return (init_cmdline && run_fptr && close_fptr);
}
HRESULT LoadCLR()
{
auto host_path = GetExecutableDir() + L"\\";
auto exec_path = host_path + L"VoicemeeterFancyOsd.dll";
if (!load_hostfxr())
{
// Nope, not necessary if you use Debug (x64) configuration.
MessageBox(0, L"Framework-dependent net core? Then copy hostfxr.dll to the APPX output directory", L"Hey", 0);
auto pl = GetCurrentProcess();
TerminateProcess(pl, EXIT_SUCCESS);
return EXIT_FAILURE;
}
hostfxr_initialize_parameters params{};
params.dotnet_root = host_path.c_str();
params.host_path = exec_path.c_str();
params.size = sizeof(hostfxr_initialize_parameters);
hostfxr_handle handle{};
// The CoreCLR executes with empty arguments if there are no arguments.
int argc = 0;
LPWSTR* argv = CommandLineToArgvW(GetCommandLine(), &argc);
int32_t init_res;
if (argc > 1)
{
const char_t** dotnet_args = new const char_t * [argc + 1];
dotnet_args[0] = exec_path.c_str(); // The 1st argument has to be the path of the main ModernFlyouts.dll (.NET) library
for (size_t i = 0; i < argc; i++)
dotnet_args[i + 1] = *(argv + i); // Subsequent arguments are passed after that
// TODO: We should clear up the array BTW.
// But the process will exit after .NET CoreCLR shuts down anyway so... *shrug*
init_res = init_cmdline(1 + argc, dotnet_args, ¶ms, &handle);
}
else
{
const char_t* dotnet_args[1] = { exec_path.c_str() };
init_res = init_cmdline(1, dotnet_args, ¶ms, &handle);
}
bool isSuccess = (init_res >= 0) && (init_res <= 2);
if (!isSuccess)
{
std::wstring message = L"Result code: " + std::to_wstring(init_res) +
L". Check if the correct version of .NET is installed\nProgram may use different .NET version after update";
MessageBox(0, message.c_str(), L"Error loading CLR", 0);
}
run_fptr(handle);
close_fptr(handle);
auto p = GetCurrentProcess();
TerminateProcess(p, EXIT_SUCCESS);
//It will never happen :)
return S_OK;
}
extern "C"
{
__declspec(dllexport) HRESULT CreateDXGIFactory(REFIID riid, void** ppFactory)
{
return LoadCLR();
}
__declspec(dllexport) HRESULT CreateDXGIFactory1(REFIID riid, void** ppFactory)
{
return LoadCLR();
}
//This is in case you get an error about entry point being meme for D3D11 (lol? d3d11 does not export CreateDXGIFactory*)
__declspec(dllexport) HRESULT CreateDXGIFactory2(UINT Flags, REFIID riid, void** ppFactory)
{
return LoadCLR();
}
//Our main target.
__declspec(dllexport) HRESULT DXGIDeclareAdapterRemovalSupport()
{
return LoadCLR();
}
}
BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
return TRUE;
}