casualexpiesource

cu-trainer/UltraPara/src/version.c

87 lines · 2.4 KB · plain text · download

1#define WIN32_LEAN_AND_MEAN
2#include <windows.h>
3
4typedef INT_PTR (WINAPI *Fwd)(INT_PTR, INT_PTR, INT_PTR, INT_PTR, INT_PTR, INT_PTR, INT_PTR, INT_PTR, INT_PTR);
5
6static HMODULE self;
7static HMODULE volatile real;
8
9static FARPROC Real(const char *name)
10{
11 if (!real)
12 {
13 wchar_t p[MAX_PATH + 16];
14 UINT n = GetSystemDirectoryW(p, MAX_PATH);
15 if (n == 0 || n > MAX_PATH) return NULL;
16 lstrcatW(p, L"\\version.dll");
17 HMODULE h = LoadLibraryW(p);
18 if (!h) return NULL;
19 if (InterlockedCompareExchangePointer((PVOID volatile *)&real, h, NULL) != NULL) FreeLibrary(h);
20 }
21 return GetProcAddress(real, name);
22}
23
24#define FWD(name) \
25 INT_PTR WINAPI up_##name(INT_PTR a, INT_PTR b, INT_PTR c, INT_PTR d, INT_PTR e, INT_PTR f, INT_PTR g, INT_PTR h, INT_PTR i) \
26 { \
27 static Fwd fn; \
28 if (!fn) fn = (Fwd)Real(#name); \
29 if (!fn) { SetLastError(ERROR_PROC_NOT_FOUND); return 0; } \
30 return fn(a, b, c, d, e, f, g, h, i); \
31 }
32
33FWD(GetFileVersionInfoA)
34FWD(GetFileVersionInfoByHandle)
35FWD(GetFileVersionInfoExA)
36FWD(GetFileVersionInfoExW)
37FWD(GetFileVersionInfoSizeA)
38FWD(GetFileVersionInfoSizeExA)
39FWD(GetFileVersionInfoSizeExW)
40FWD(GetFileVersionInfoSizeW)
41FWD(GetFileVersionInfoW)
42FWD(VerFindFileA)
43FWD(VerFindFileW)
44FWD(VerInstallFileA)
45FWD(VerInstallFileW)
46FWD(VerLanguageNameA)
47FWD(VerLanguageNameW)
48FWD(VerQueryValueA)
49FWD(VerQueryValueW)
50
51static int IsGame(void)
52{
53 wchar_t p[MAX_PATH];
54 DWORD n = GetModuleFileNameW(NULL, p, MAX_PATH);
55 if (n == 0 || n >= MAX_PATH) return 0;
56 wchar_t *leaf = p;
57 for (wchar_t *c = p; *c; c++) if (*c == L'\\' || *c == L'/') leaf = c + 1;
58 return lstrcmpiW(leaf, L"CasualtiesUnknown.exe") == 0;
59}
60
61static DWORD WINAPI Start(LPVOID unused)
62{
63 wchar_t p[MAX_PATH + 32];
64 DWORD n = GetModuleFileNameW(self, p, MAX_PATH);
65 if (n == 0 || n >= MAX_PATH) return 0;
66 wchar_t *cut = p;
67 for (wchar_t *c = p; *c; c++) if (*c == L'\\' || *c == L'/') cut = c + 1;
68 *cut = 0;
69 lstrcatW(p, L"UltraPara.dll");
70 if (GetFileAttributesW(p) != INVALID_FILE_ATTRIBUTES) LoadLibraryW(p);
71 return 0;
72}
73
74BOOL WINAPI DllMain(HINSTANCE h, DWORD why, LPVOID r)
75{
76 if (why == DLL_PROCESS_ATTACH)
77 {
78 self = h;
79 DisableThreadLibraryCalls(h);
80 if (IsGame())
81 {
82 HANDLE t = CreateThread(NULL, 0, Start, NULL, 0, NULL);
83 if (t) CloseHandle(t);
84 }
85 }
86 return TRUE;
87}