Skip to content

Load optional Windows DLLs at runtime and drop delay loading - #146

Merged
ethindp merged 3 commits into
ethindp:masterfrom
trypsynth:fix/load-optional-dlls-at-runtime
Sep 30, 2026
Merged

ethindp merged 3 commits into
ethindp:masterfrom
trypsynth:fix/load-optional-dlls-at-runtime

Conversation

@trypsynth

Copy link
Copy Markdown
Contributor

Currently when calling static Prism from Rust, /DELAYLOAD only works on the final link, and Cargo never passes a dependency's link args to the binary, so unless your app remembers a build script for it, the screen reader DLLs and the Wine bridges become hard imports. In Fedra, for example, without putting a 15-line or so function in my build.rs, it dies on launch asking for byctrl-x64.dll, ZDSRAPI_x64.dll, and PCTKUSR.dll using prismer and prism-sys, hence this PR.

In other words, this does on Windows what #136 did on Linux. SharedLibrary gets a Windows side, and ZDSR, BoyPCReader, PC-Talker, Orca, and the Wine sides of Speech Dispatcher and Spiel each bind the functions they actually call into a decltype table. If the DLL or a function is missing, the backend just reports itself unavailable. open_optional_library keeps the old search order: the DLL search path, then prism's own folder, then the ZDSR or BoyPC install folder from the registry. With nothing delay-loaded anymore, delayimp.cpp, defs/, the generated import libraries, the /DELAY flags, and prism-static-windows.txt all go, and CONTRIBUTING now describes the new way.

I tested x64 static, x64 shared, and Win32 with MSVC, and they all build clean. A static test program initializes every backend: NVDA, OneCore, and SAPI succeed, and the six converted ones all return BackendNotAvailable instead of crashing. Also, neither prism.dll nor Fedra imports any of the six DLLs anymore.

With MinGW (WinLibs GCC 16), everything this touches compiles, but prism still doesn't build there overall because of ATL and C++/WinRT. Decided just to not touch that, now or probably ever.

Sidenote: Once this lands, I'll update prism-sys to stop reading prism-static-windows.txt.

Cheers, and sorry for the huge diff!

@ethindp

ethindp commented Sep 28, 2026

Copy link
Copy Markdown
Owner

Can you provide a reproduction of this particular bug? I am hesitant to switch to run-time library loading just because it means we must manually maintain structures for each backend but scattered all over the codebase.

For what it's worth, a consuming application shouldn't require /delayload regardless of it's link mode; that would defeat the point of /delayload. If this is indeed a problem then I think it may be easier to figure out why that is instead of completely replacing a substantial portion of the library.

@trypsynth

Copy link
Copy Markdown
Contributor Author

@ethindp thanks! You're right that nothing linking prism.dll as a shared library ever needs /delayload, since that's baked into the DLL. Static is different though. The image being linked is the app's own exe, so delay loading is decided by the app's link, and /DELAYLOAD isn't one of the options a library can pass through #pragma comment(linker). CMake consumers never notice because prism's config adds the flags as INTERFACE link options, but anything else gets hard imports.

I built master with -DBUILD_SHARED_LIBS=OFF, ran cmake --install, and linked this against only the installed files, from a VS 2026 x64 developer prompt:

#include <prism.h>
#include <stdio.h>

int main(void) {
	PrismConfig cfg = prism_config_init();
	PrismContext* ctx = prism_init(&cfg);
	PrismBackend* backend = prism_registry_create_best(ctx);
	printf("started, best backend: %s\n", backend ? prism_backend_name(backend) : "none");
	prism_shutdown(ctx);
	return 0;
}
cl /nologo /MD /DPRISM_STATIC /c main.c /I install\include
link /nologo main.obj install\lib\prism.lib install\lib\ZDSR.lib install\lib\byctrl.lib install\lib\PCTalker.lib install\lib\PrismOrcaBridge.lib install\lib\PrismSpeechDispatcherBridge.lib install\lib\PrismSpielBridge.lib ole32.lib onecore.lib runtimeobject.lib uiautomationcore.lib rpcrt4.lib powrprof.lib delayimp.lib /OUT:repro.exe

dumpbin /imports repro.exe lists ZDSRAPI_x64.dll, byctrl-x64.dll, PCTKUSR.dll, prism_orca_bridge.dll, prism_speech_dispatcher_bridge.dll, and prism_spiel_bridge.dll as normal imports, with no delay import section at all. Running it exits with STATUS_DLL_NOT_FOUND before main runs.

Linking the same object again with /DELAYLOAD for those six DLLs prints started, best backend: NVDA and exits with 0.

On the maintenance side, it is your project so it is of course your choice, but I'd argue it's less than before. delayimp.cpp had a stub for every export of every DLL, while the new tables only list what each backend actually calls and sit in that backend's own file. That said, if you'd rather keep delay loading, the other option I can think of would be documenting that static non-CMake consumers have to pass /DELAYLOAD for those six DLLs themselves, which is what prism-sys and Fedra's build script already do with my 15-line hack. That's less preferable to me, but it is workable.

@ethindp

ethindp commented Sep 28, 2026

Copy link
Copy Markdown
Owner

@trypsynth So, I agree with you (even if I'm not super happy switching away from delay loading, but the alternatives, from what my research shows, are even worse). Some comments since they're systematic across the entire diff:

  1. Please don't use &:: in decltype references or in CONTRIBUTING.md. There is little need to do this unless normal name resolution can't figure out which function your referring to. I consider this kind of hack more of a workaround.
  2. Please don't force wide strings; use tchar and _T where possible.
  3. I'm really not a fan of things like orca_bridge being called just to get the API object all over the place (since all it does is load the library and isn't cached anywhere). It also makes the code harder to read. We should probably establish a single convention across the entire codebase now instead of doing differing ones like this. I think the only place where a function-local copy is fine is in get_features and friends where we don't know if the library even exists.
  4. Please don't directly call functions like GetModuleHandleExW; instead, call the unsuffixed versions and allow the macros to resolve the right function.

@trypsynth

Copy link
Copy Markdown
Contributor Author

@ethindp thanks, I'll address all four points in an upcoming commit. I'd push back slightly on point 3, though. Each API is actually cached in a function-local static, so the library only loads once, but I do agree it reads badly. I'll have each backend hold a pointer to its API, set once when it initializes, and call through that. I'll also fix point 4, but make the PCTK*W calls keep their suffix because I believe it would break otherwise.

- Drop the `::` from the `decltype` pointers, which compile fine without it.
- Use `TCHAR`, `_T` and the unsuffixed Win32 functions instead of wide
  strings and the `W` versions.
- Have the Orca, Speech Dispatcher and Spiel backends hold a pointer to
  their bridge, set once in `initialize`, like the other backends already
  do, instead of calling the loader every time.

Signed-off-by: Quin Gillespie <[email protected]>
@ethindp

ethindp commented Sep 28, 2026

Copy link
Copy Markdown
Owner

@trypsynth The PCTalker functions are an exception since I had to manually reconstruct the header from the DLLs. I don't have any kind of SDK so had to do a bunch of work to try to figure out exactly how the DLL worked, so I've always treated that as an exception to the normal rule of avoiding suffixed functions. That rule (which should probably be in the contributing guidelines) primarily applies to win32 APIs, not to external ones.

@ethindp

ethindp commented Sep 29, 2026

Copy link
Copy Markdown
Owner

@trypsynth Please review CI/CD; this has failing checks.

GCC 15 rejects a member declared as decltype(&name) name; with
-Wchanges-meaning, since the member's name changes what the earlier
unqualified lookup found. Looking the function up as ::name avoids
the rule, which applies only to unqualified names.
@ethindp
ethindp merged commit d0153e4 into ethindp:master Sep 30, 2026
65 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants