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 (*config_main_fn)(int argc, char **argv);
7
8static const char *const CONFIG_DLL_RELATIVE_PATH = "modules\\libdttr_config.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 config_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 const size_t remaining = out_size - len;
38 const int written = snprintf(out + len, remaining, "%s", CONFIG_DLL_RELATIVE_PATH);
39 return written > 0 && (size_t)written < remaining;
40}
41
42static void show_startup_error(const char *message, DWORD error) {
43 char detail[512];
44 char text[768];
45
46 if (error
47 && FormatMessageA(
48 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
49 NULL,
50 error,
51 0,
52 detail,
53 sizeof(detail),
54 NULL
55 )
56 && detail[0]) {
57 snprintf(text, sizeof(text), "%s\n\nError %lu: %s", message, error, detail);
58 } else {
59 snprintf(text, sizeof(text), "%s", message);
60 }
61
62 MessageBoxA(NULL, text, "DttR Config: Error", MB_OK | MB_ICONERROR);
63}
64
65static config_main_fn resolve_config_main(HMODULE module) {
66 return (config_main_fn)GetProcAddress(module, "dttr_config_main");
67}
68
69int main(int argc, char *argv[]) {
70 char path[MAX_PATH];
71 if (!config_dll_path(path, sizeof(path))) {
72 show_startup_error("Could not resolve the DttR config module path.", 0);
73 return 1;
74 }
75
76 HMODULE module = LoadLibraryExA(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
77 if (!module) {
78 show_startup_error("Could not load modules\\libdttr_config.dll.", GetLastError());
79 return 1;
80 }
81
82 config_main_fn config_main = resolve_config_main(module);
83 if (!config_main) {
84 show_startup_error("Could not find the DttR config entry point.", GetLastError());
85 FreeLibrary(module);
86 return 1;
87 }
88
89 const int result = config_main(argc, argv);
90 FreeLibrary(module);
91 return result;
92}
DTTR_Graphics_COM_DirectDrawSurface7 DWORD flags void NULL
int main(int argc, char *argv[])
Definition main.c:69
static bool config_dll_path(char *out, size_t out_size)
Definition main.c:31
static void show_startup_error(const char *message, DWORD error)
Definition main.c:42
int(* config_main_fn)(int argc, char **argv)
Definition main.c:6
static bool get_exe_dir(char *buf, size_t buf_size)
Definition main.c:10
static config_main_fn resolve_config_main(HMODULE module)
Definition main.c:65
static const char *const CONFIG_DLL_RELATIVE_PATH
Definition main.c:8