cu-trainer/UltraPara/src/version.c
| 1 | #define WIN32_LEAN_AND_MEAN |
| 2 | #include <windows.h> |
| 3 | |
| 4 | typedef INT_PTR (WINAPI *Fwd)(INT_PTR, INT_PTR, INT_PTR, INT_PTR, INT_PTR, INT_PTR, INT_PTR, INT_PTR, INT_PTR); |
| 5 | |
| 6 | static HMODULE self; |
| 7 | static HMODULE volatile real; |
| 8 | |
| 9 | static 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 | |
| 33 | FWD(GetFileVersionInfoA) |
| 34 | FWD(GetFileVersionInfoByHandle) |
| 35 | FWD(GetFileVersionInfoExA) |
| 36 | FWD(GetFileVersionInfoExW) |
| 37 | FWD(GetFileVersionInfoSizeA) |
| 38 | FWD(GetFileVersionInfoSizeExA) |
| 39 | FWD(GetFileVersionInfoSizeExW) |
| 40 | FWD(GetFileVersionInfoSizeW) |
| 41 | FWD(GetFileVersionInfoW) |
| 42 | FWD(VerFindFileA) |
| 43 | FWD(VerFindFileW) |
| 44 | FWD(VerInstallFileA) |
| 45 | FWD(VerInstallFileW) |
| 46 | FWD(VerLanguageNameA) |
| 47 | FWD(VerLanguageNameW) |
| 48 | FWD(VerQueryValueA) |
| 49 | FWD(VerQueryValueW) |
| 50 | |
| 51 | static 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 | |
| 61 | static 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 | |
| 74 | BOOL 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 | } |