Skip to content

Commit cda58d2

Browse files
dunglasclaude
andauthored
fix(windows): ensure DLLs can always be located by PHP (#2227)
Prevent crashes when `php.ini` references PHP extensions using relative paths, and FrankenPHP is started from a different working directory than the one containing extensions, or with `caddy start` (instead of `caddy run`). --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c2b8c8b commit cda58d2

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

frankenphp.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,32 @@ static void *php_main(void *arg) {
10801080

10811081
sapi_startup(&frankenphp_sapi_module);
10821082

1083+
/* TODO: adapted from https://github.com/php/php-src/pull/16958, remove when
1084+
* merged. */
1085+
#ifdef PHP_WIN32
1086+
{
1087+
const DWORD flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
1088+
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
1089+
HMODULE module;
1090+
/* Use a larger buffer to support long module paths on Windows. */
1091+
wchar_t filename[32768];
1092+
if (GetModuleHandleExW(flags, (LPCWSTR)&frankenphp_sapi_module, &module)) {
1093+
const DWORD filename_capacity = (DWORD)_countof(filename);
1094+
DWORD len = GetModuleFileNameW(module, filename, filename_capacity);
1095+
if (len > 0 && len < filename_capacity) {
1096+
wchar_t *slash = wcsrchr(filename, L'\\');
1097+
if (slash) {
1098+
*slash = L'\0';
1099+
if (!SetDllDirectoryW(filename)) {
1100+
fprintf(stderr, "Warning: SetDllDirectoryW failed (error %lu)\n",
1101+
GetLastError());
1102+
}
1103+
}
1104+
}
1105+
}
1106+
}
1107+
#endif
1108+
10831109
#ifdef ZEND_MAX_EXECUTION_TIMERS
10841110
/* overwrite php.ini with custom user settings */
10851111
char *php_ini_overrides = go_get_custom_php_ini(false);

0 commit comments

Comments
 (0)