Debian Codegraph @

bypy/windows/main.c

raw
1 /*
2 * Copyright 2009 Kovid Goyal
3 */
4
5 #ifndef UNICODE
6 #define UNICODE
7 #endif
8 #define WINDOWS_LEAN_AND_MEAN
9 #include <windows.h>
10 #include <strsafe.h>
11
12 static size_t
13 mystrlen(const wchar_t *buf) {
14 size_t ans = 0;
15 if (FAILED(StringCbLengthW(buf, 500, &ans))) return 0;
16 return ans;
17 }
18
19 static int
20 show_error(const wchar_t *preamble, const wchar_t *msg, const int code) {
21 wchar_t *buf;
22 buf = (wchar_t *)LocalAlloc(LMEM_ZEROINIT, sizeof(wchar_t) * (mystrlen(msg) + mystrlen(preamble) + 80));
23 if (!buf) {
24 MessageBox(NULL, preamble, NULL, MB_OK | MB_ICONERROR);
25 return code;
26 }
27
28 MessageBeep(MB_ICONERROR);
29 wsprintf(buf, L"%s\r\n %s (Error Code: %d)\r\n", preamble, msg, code);
30 MessageBox(NULL, buf, NULL, MB_OK | MB_ICONERROR);
31 LocalFree(buf);
32 return code;
33 }
34
35 static int
36 show_last_error(wchar_t *preamble) {
37 wchar_t *msg = NULL;
38 DWORD dw = GetLastError();
39 int ret;
40
41 FormatMessage(
42 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
43 NULL,
44 dw,
45 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
46 (LPWSTR)&msg,
47 0,
48 NULL);
49
50 ret = show_error(preamble, msg, (int)dw);
51 if (msg != NULL) LocalFree(msg);
52 return ret;
53 }
54
55 typedef int(__cdecl *ENTRYPROC)(const wchar_t *, const wchar_t *, const wchar_t *, int);
56 typedef void(__cdecl *SIMPLEPRINT)(const wchar_t *);
57 typedef BOOL (*SETDEFAULTDIRS)(DWORD);
58 static ENTRYPROC entrypoint = NULL;
59 static SIMPLEPRINT simple_print = NULL;
60 static HMODULE dll = 0;
61
62 static void
63 load_launcher_dll() {
64 static wchar_t buf[MAX_PATH]; // Cannot use a zero initializer for the array as it generates an implicit call to memset()
65 wchar_t *dll_point = NULL;
66 int i = 0;
67 DWORD sz = 0;
68
69 if ((sz = GetModuleFileNameW(NULL, buf, MAX_PATH)) >= MAX_PATH - 30) {
70 show_error(L"Installation directory path too long", L"", 1);
71 return;
72 }
73
74 while (sz > 0) {
75 if (buf[sz] == L'\\' || buf[sz] == L'/') {
76 dll_point = buf + sz + 1;
77 break;
78 }
79 sz--;
80 }
81 if (dll_point == NULL) {
82 show_error(L"Executable path has no path separators", L"", 1);
83 return;
84 }
85 wsprintf(dll_point, L"%s\0\0", L"app\\bin");
86 #if _WIN64
87 // Restrict the directories from which DLLs can be loaded
88 // For some reason I cannot determine, using this in 32bit builds causes
89 // a crash even if no dlls are loaded.
90 SETDEFAULTDIRS SetDefaultDllDirectories = (SETDEFAULTDIRS)GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "SetDefaultDllDirectories");
91 if (SetDefaultDllDirectories) SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
92 #endif
93 if (SetDllDirectoryW(buf) == 0) {
94 show_last_error(L"Failed to set DLL directory");
95 return;
96 }
97 // Have to load ucrtbase manually first, otherwise loading fails on systems where the
98 // Universal CRT is not installed.
99 if (!LoadLibraryW(L"ucrtbase.dll")) {
100 show_last_error(L"Unable to find ucrtbase.dll. You should install all Windows updates on your computer to get this file.");
101 return;
102 }
103 if (!(dll = LoadLibraryW(L"calibre-launcher.dll"))) {
104 show_last_error(L"Failed to load: calibre-launcher.dll");
105 return;
106 }
107 if (!(entrypoint = (ENTRYPROC)GetProcAddress(dll, "execute_python_entrypoint"))) {
108 show_last_error(L"Failed to get the calibre-launcher dll entry point");
109 return;
110 }
111 simple_print = (SIMPLEPRINT)GetProcAddress(dll, "simple_print");
112 }
113
114 int __stdcall
115 start_here() {
116 int ret = 0;
117 load_launcher_dll();
118 if (entrypoint) {
119 #ifdef GUI_APP
120 // This should really be returning the value set in the WM_QUIT message, but I cannot be bothered figuring out how to get that.
121 entrypoint(BASENAME, MODULE, FUNCTION, 1);
122 #else
123 ret = entrypoint(BASENAME, MODULE, FUNCTION, 0);
124 #endif
125 } else ret = 1;
126 if (dll != 0) {
127 FreeLibrary(dll);
128 dll = 0;
129 }
130 ExitProcess(ret);
131 return ret;
132 }