#1717·jna

windows/security: explicit LOAD_LIBRARY_SEARCH_SYSTEM32 for system32 dll

Author: jiaz83Created May 7, 2026Updated May 7, 2026

We recently stumpled upon dll loading issues of wtsapi32.dll with prior modification of dll search path via SetDefaultDllDirectories resulting exact same issue as in https://github.com/java-native-access/jna/pull/1614

calling LoadLibraryEx(relative_path, ..., LOAD_WITH_ALTERED_SEARCH_PATH) results in ERROR_INVALID_PARAMETER.

According to LoadLibraryEx documentation, this results in an undefined behavior when a library name is given as a relative path. With that in mind, it would be best practise to secure system32 dll loading by using OPTION_OPEN_FLAGS set to LOAD_LIBRARY_SEARCH_SYSTEM32 for those interfaces against system32 dlls.

Map<String, Object> DEFAULT_W32SYSTEM32APIOptions = new HashMap<String, Object>(W32APIOptions.DEFAULT_OPTIONS) {{ //LOAD_LIBRARY_SEARCH_SYSTEM32 put(com.sun.jna.Library.OPTION_OPEN_FLAGS, 0x00000800); }}; Once we switched to LOAD_LIBRARY_SEARCH_SYSTEM32 all system32 dlls in use were properly found/loaded.