| 1 |
/* |
| 2 |
* Copyright 2009 Kovid Goyal |
| 3 |
*/ |
| 4 |
|
| 5 |
#define UNICODE |
| 6 |
#define PY_SSIZE_T_CLEAN |
| 7 |
|
| 8 |
#define _WIN32_WINNT 0x0502 |
| 9 |
#define WINDOWS_LEAN_AND_MEAN |
| 10 |
|
| 11 |
#include <windows.h> |
| 12 |
#include <Python.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <Shellapi.h> |
| 16 |
#include <delayimp.h> |
| 17 |
#include <io.h> |
| 18 |
#include <fcntl.h> |
| 19 |
#include "../run-python.h" |
| 20 |
|
| 21 |
static char python_dll[] = PYDLL; |
| 22 |
|
| 23 |
|
| 24 |
static wchar_t qt_prefix_dir[MAX_PATH] = {0}; |
| 25 |
|
| 26 |
static void |
| 27 |
get_app_dirw(void) { |
| 28 |
wchar_t buf[MAX_PATH] = {0}; |
| 29 |
wchar_t drive[4] = L"\0\0\0"; |
| 30 |
DWORD sz; |
| 31 |
errno_t err; |
| 32 |
|
| 33 |
sz = GetModuleFileNameW(NULL, interpreter_data.exe_path, MAX_PATH); |
| 34 |
if (sz >= MAX_PATH - 1) ExitProcess(_show_error(L"Installation directory path too long", L"", 1)); |
| 35 |
err = _wsplitpath_s(interpreter_data.exe_path, drive, 4, buf, MAX_PATH, NULL, 0, NULL, 0); |
| 36 |
if (err != 0) ExitProcess(show_last_error_crt(L"Failed to find application directory")); |
| 37 |
_snwprintf_s(interpreter_data.app_dir, MAX_PATH, _TRUNCATE, L"%ls%ls", drive, buf); |
| 38 |
_snwprintf_s(interpreter_data.resources_path, MAX_PATH, _TRUNCATE, L"%ls%lsapp\\resources", drive, buf); |
| 39 |
_snwprintf_s(interpreter_data.extensions_path, MAX_PATH, _TRUNCATE, L"%ls%lsapp\\bin", drive, buf); |
| 40 |
_snwprintf_s(interpreter_data.executables_path, MAX_PATH, _TRUNCATE, L"%ls%lsapp\\bin", drive, buf); |
| 41 |
} |
| 42 |
|
| 43 |
static void |
| 44 |
get_install_locations(void) { |
| 45 |
get_app_dirw(); |
| 46 |
_snwprintf_s(qt_prefix_dir, MAX_PATH - 1, _TRUNCATE, L"%ls\\ossl-modules", interpreter_data.executables_path); |
| 47 |
_wputenv_s(L"OPENSSL_MODULES", qt_prefix_dir); |
| 48 |
// Have to disable sandbox or Qt WebEngine Render Process crashes as of Qt 6.6 |
| 49 |
_wputenv_s(L"QTWEBENGINE_DISABLE_SANDBOX", "1"); |
| 50 |
_snwprintf_s(qt_prefix_dir, MAX_PATH - 1, _TRUNCATE, L"%ls\\app", interpreter_data.app_dir); |
| 51 |
_wputenv_s(L"CALIBRE_QT_PREFIX", qt_prefix_dir); |
| 52 |
// Lots of people have trouble with various websites failing to download |
| 53 |
// because of missing intermediate certificates in the windows store |
| 54 |
// so use the Mozilla certificate bundle |
| 55 |
_snwprintf_s(qt_prefix_dir, MAX_PATH - 1, _TRUNCATE, L"%ls\\mozilla-ca-certs", interpreter_data.resources_path); |
| 56 |
const char *s = getenv("CALIBRE_USE_SYSTEM_CERTIFICATES"); |
| 57 |
if (!s || strcmp(s, "1") != 0) _wputenv_s(L"SSL_CERT_DIR", qt_prefix_dir); |
| 58 |
} |
| 59 |
|
| 60 |
static void |
| 61 |
load_python_dll() { |
| 62 |
get_install_locations(); |
| 63 |
bypy_setup_python(python_dll); |
| 64 |
} |
| 65 |
|
| 66 |
const static wchar_t out_of_memory[] = L"Out of memory"; |
| 67 |
|
| 68 |
|
| 69 |
static void |
| 70 |
redirect_out_stream(FILE *stream) { |
| 71 |
FILE *f = NULL; |
| 72 |
errno_t err; |
| 73 |
|
| 74 |
err = _wfreopen_s(&f, L"NUL", L"wt", stream); |
| 75 |
if (err != 0) { |
| 76 |
ExitProcess(show_last_error_crt( |
| 77 |
L"Failed to redirect stdout/stderr to NUL. This indicates a corrupted Windows install.\r\n You should contact Microsoft for assistance and/or " |
| 78 |
L"follow the steps described here:\r\n http://bytes.com/topic/net/answers/264804-compile-error-null-device-missing")); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
static void |
| 83 |
null_invalid_parameter_handler(const wchar_t *expression, const wchar_t *function, const wchar_t *file, unsigned int line, uintptr_t pReserved) { |
| 84 |
// The python runtime expects various system calls with invalid parameters |
| 85 |
// to return errors instead of aborting the program. So get the windows CRT |
| 86 |
// to do that. |
| 87 |
} |
| 88 |
__declspec(dllexport) int __cdecl |
| 89 |
simple_print(const wchar_t *msg) { |
| 90 |
int n = wprintf(L"%ls", msg); |
| 91 |
fflush(stdout); |
| 92 |
return n; |
| 93 |
} |
| 94 |
|
| 95 |
__declspec(dllexport) int __cdecl |
| 96 |
execute_python_entrypoint(const wchar_t *basename, const wchar_t *module, const wchar_t *function, int is_gui_app) { |
| 97 |
int ret = 0; |
| 98 |
// Prevent Windows' idiotic error dialog popups when various win32 api functions fail |
| 99 |
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX); |
| 100 |
// Disable the invalid parameter handler |
| 101 |
_set_invalid_parameter_handler(null_invalid_parameter_handler); |
| 102 |
detect_tty(); |
| 103 |
|
| 104 |
if (is_gui_app) { |
| 105 |
// Redirect stdout and stderr to NUL so that python does not fail writing to them |
| 106 |
if (!stdout_is_a_tty) redirect_out_stream(stdout); |
| 107 |
if (!stderr_is_a_tty) redirect_out_stream(stderr); |
| 108 |
} |
| 109 |
GUI_APP = is_gui_app; |
| 110 |
interpreter_data.argv = CommandLineToArgvW(GetCommandLineW(), &interpreter_data.argc); |
| 111 |
if (interpreter_data.argv == NULL) ExitProcess(show_last_error(L"Failed to get command line")); |
| 112 |
interpreter_data.basename = basename; |
| 113 |
interpreter_data.module = module; |
| 114 |
interpreter_data.function = function; |
| 115 |
load_python_dll(); |
| 116 |
pre_initialize_interpreter(is_gui_app); |
| 117 |
run_interpreter(); |
| 118 |
|
| 119 |
|
| 120 |
/* printf("111111111111 returning: %d\r\n", ret); */ |
| 121 |
|
| 122 |
return ret; |
| 123 |
} |