102 Patches: Detours to the Rescue
C reference for DttR maintainers and modders.
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1#include <stdbool.h>
2#include <stdio.h>
3#include <string.h>
4#include <windows.h>
5
6typedef int (*launcher_main_fn)(int argc, char **argv);
7
8static const char *const LAUNCHER_DLL_RELATIVE_PATH = "modules\\libdttr_launcher.dll";
9
10static bool get_exe_dir(char *buf, size_t buf_size) {
11 if (buf_size == 0) {
12 return false;
13 }
14
15 const DWORD len = GetModuleFileNameA(NULL, buf, (DWORD)buf_size);
16 if (len == 0 || len >= buf_size) {
17 buf[0] = '\0';
18 return false;
19 }
20
21 char *last_sep = strrchr(buf, '\\');
22 if (!last_sep) {
23 buf[0] = '\0';
24 return false;
25 }
26
27 last_sep[1] = '\0';
28 return true;
29}
30
31static bool launcher_dll_path(char *out, size_t out_size) {
32 if (!get_exe_dir(out, out_size)) {
33 return false;
34 }
35
36 const size_t len = strlen(out);
37 if (len >= out_size) {
38 return false;
39 }
40
41 const int
42 written = snprintf(out + len, out_size - len, "%s", LAUNCHER_DLL_RELATIVE_PATH);
43 return written > 0 && (size_t)written < out_size - len;
44}
45
46static void show_startup_error(const char *message, DWORD error) {
47 char detail[512];
48 char text[768];
49
50 if (error
51 && FormatMessageA(
52 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
53 NULL,
54 error,
55 0,
56 detail,
57 sizeof(detail),
58 NULL
59 )
60 && detail[0]) {
61 snprintf(text, sizeof(text), "%s\n\nError %lu: %s", message, error, detail);
62 } else {
63 snprintf(text, sizeof(text), "%s", message);
64 }
65
66 MessageBoxA(NULL, text, "DttR: Error", MB_OK | MB_ICONERROR);
67}
68
70 return (launcher_main_fn)GetProcAddress(module, "dttr_launcher_main");
71}
72
73int main(int argc, char *argv[]) {
74 char path[MAX_PATH];
75 if (!launcher_dll_path(path, sizeof(path))) {
76 show_startup_error("Could not resolve the DttR launcher module path.", 0);
77 return 1;
78 }
79
80 HMODULE module = LoadLibraryExA(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
81 if (!module) {
83 "Could not load modules\\libdttr_launcher.dll.",
84 GetLastError()
85 );
86 return 1;
87 }
88
89 launcher_main_fn launcher_main = resolve_launcher_main(module);
90 if (!launcher_main) {
92 "Could not find the DttR launcher entry point.",
93 GetLastError()
94 );
95 FreeLibrary(module);
96 return 1;
97 }
98
99 return launcher_main(argc, argv);
100}
DTTR_Graphics_COM_DirectDrawSurface7 DWORD flags void NULL
int main(int argc, char *argv[])
Definition main.c:69
static void show_startup_error(const char *message, DWORD error)
Definition main.c:42
static bool get_exe_dir(char *buf, size_t buf_size)
Definition main.c:10
static launcher_main_fn resolve_launcher_main(HMODULE module)
Definition main.c:69
static const char *const LAUNCHER_DLL_RELATIVE_PATH
Definition main.c:8
static bool launcher_dll_path(char *out, size_t out_size)
Definition main.c:31
int(* launcher_main_fn)(int argc, char **argv)
Definition main.c:6