forked from noahc3/AffinityPluginLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.c
More file actions
166 lines (138 loc) · 5.86 KB
/
bootstrap.c
File metadata and controls
166 lines (138 loc) · 5.86 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* AffinityBootstrap - Native Wine-compatible bootstrap DLL
*
* This native DLL is loaded by Wine and initializes the managed AffinityPluginLoader.dll
*
* Build with MinGW:
* x86_64-w64-mingw32-gcc -shared -o AffinityBootstrap.dll bootstrap.c -lole32 -loleaut32 -luuid
*
* Build with Visual Studio:
* cl.exe /LD bootstrap.c ole32.lib oleaut32.lib mscoree.lib /Fe:AffinityBootstrap.dll
*/
#define _WIN32_WINNT 0x0600
#include <windows.h>
#include <stdio.h>
// COM interfaces for .NET hosting
#include <metahost.h> // This has ICLRMetaHost, ICLRRuntimeInfo, etc.
// Need to link these libraries (for Visual Studio)
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
#pragma comment(lib, "mscoree.lib")
// Function to initialize the managed DLL
static DWORD WINAPI InitializeManagedLoader(LPVOID lpParam)
{
HRESULT hr;
ICLRMetaHost *pMetaHost = NULL;
ICLRRuntimeInfo *pRuntimeInfo = NULL;
ICLRRuntimeHost *pClrHost = NULL;
DWORD dwRet = 0;
// Small delay to let DllMain complete
Sleep(100);
OutputDebugStringW(L"AffinityBootstrap: Starting initialization...\n");
// Get the directory where this DLL is located
WCHAR dllPath[MAX_PATH];
HMODULE hModule = NULL;
GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCWSTR)&InitializeManagedLoader,
&hModule);
GetModuleFileNameW(hModule, dllPath, MAX_PATH);
// Get directory
WCHAR *lastSlash = wcsrchr(dllPath, L'\\');
if (lastSlash) *lastSlash = L'\0';
// Build path to AffinityPluginLoader.dll
WCHAR loaderPath[MAX_PATH];
_snwprintf_s(loaderPath, MAX_PATH, _TRUNCATE, L"%s\\AffinityPluginLoader.dll", dllPath);
// Debug output
WCHAR debugMsg[MAX_PATH + 50];
_snwprintf_s(debugMsg, sizeof(debugMsg)/sizeof(WCHAR), _TRUNCATE,
L"AffinityBootstrap: Looking for: %s\n", loaderPath);
OutputDebugStringW(debugMsg);
// Initialize CLR
hr = CLRCreateInstance(&CLSID_CLRMetaHost, &IID_ICLRMetaHost, (LPVOID*)&pMetaHost);
if (FAILED(hr)) {
_snwprintf_s(debugMsg, sizeof(debugMsg)/sizeof(WCHAR), _TRUNCATE,
L"AffinityBootstrap: Failed to create CLR instance (0x%08X)\n", hr);
OutputDebugStringW(debugMsg);
return 1;
}
OutputDebugStringW(L"AffinityBootstrap: CLR instance created\n");
// Get .NET Framework 4.0 runtime
hr = pMetaHost->lpVtbl->GetRuntime(pMetaHost, L"v4.0.30319", &IID_ICLRRuntimeInfo, (LPVOID*)&pRuntimeInfo);
if (FAILED(hr)) {
_snwprintf_s(debugMsg, sizeof(debugMsg)/sizeof(WCHAR), _TRUNCATE,
L"AffinityBootstrap: Failed to get runtime info (0x%08X)\n", hr);
OutputDebugStringW(debugMsg);
pMetaHost->lpVtbl->Release(pMetaHost);
return 1;
}
OutputDebugStringW(L"AffinityBootstrap: Runtime info obtained\n");
// Get runtime host
hr = pRuntimeInfo->lpVtbl->GetInterface(pRuntimeInfo, &CLSID_CLRRuntimeHost, &IID_ICLRRuntimeHost, (LPVOID*)&pClrHost);
if (FAILED(hr)) {
_snwprintf_s(debugMsg, sizeof(debugMsg)/sizeof(WCHAR), _TRUNCATE,
L"AffinityBootstrap: Failed to get runtime host (0x%08X)\n", hr);
OutputDebugStringW(debugMsg);
pRuntimeInfo->lpVtbl->Release(pRuntimeInfo);
pMetaHost->lpVtbl->Release(pMetaHost);
return 1;
}
OutputDebugStringW(L"AffinityBootstrap: Runtime host obtained\n");
// Start CLR
hr = pClrHost->lpVtbl->Start(pClrHost);
if (FAILED(hr) && hr != S_FALSE) { // S_FALSE means already started
_snwprintf_s(debugMsg, sizeof(debugMsg)/sizeof(WCHAR), _TRUNCATE,
L"AffinityBootstrap: Failed to start CLR (0x%08X)\n", hr);
OutputDebugStringW(debugMsg);
pClrHost->lpVtbl->Release(pClrHost);
pRuntimeInfo->lpVtbl->Release(pRuntimeInfo);
pMetaHost->lpVtbl->Release(pMetaHost);
return 1;
}
OutputDebugStringW(L"AffinityBootstrap: CLR started\n");
// Execute managed code
// Call AffinityPluginLoader.EntryPoint.Initialize()
hr = pClrHost->lpVtbl->ExecuteInDefaultAppDomain(
pClrHost,
loaderPath,
L"AffinityPluginLoader.EntryPoint",
L"Initialize",
L"",
&dwRet
);
if (FAILED(hr)) {
_snwprintf_s(debugMsg, sizeof(debugMsg)/sizeof(WCHAR), _TRUNCATE,
L"AffinityBootstrap: Failed to execute managed code (0x%08X)\n", hr);
OutputDebugStringW(debugMsg);
// Common error codes
if (hr == 0x80070002) {
OutputDebugStringW(L"AffinityBootstrap: ERROR - AffinityPluginLoader.dll not found!\n");
} else if (hr == 0x80131513) {
OutputDebugStringW(L"AffinityBootstrap: ERROR - Method not found in assembly!\n");
}
} else {
OutputDebugStringW(L"AffinityBootstrap: Successfully initialized managed loader!\n");
}
// Don't release - keep CLR running
// pClrHost->lpVtbl->Release(pClrHost);
// pRuntimeInfo->lpVtbl->Release(pRuntimeInfo);
// pMetaHost->lpVtbl->Release(pMetaHost);
return 0;
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH)
{
OutputDebugStringW(L"AffinityBootstrap: DLL loaded\n");
// Disable thread notifications for better performance
DisableThreadLibraryCalls(hinstDLL);
// Initialize in a new thread to avoid blocking DllMain
HANDLE hThread = CreateThread(NULL, 0, InitializeManagedLoader, NULL, 0, NULL);
if (hThread) {
CloseHandle(hThread);
} else {
OutputDebugStringW(L"AffinityBootstrap: Failed to create initialization thread!\n");
}
}
return TRUE;
}