Files
raven/testdata/native_loader/module_handle_path_client.c
T
Raymond Bennett 549cda8f77 test: find a library by the path it was loaded from
GetModuleHandleW and GetModuleHandleExW (pinning it) give back the handle
LoadLibraryW returned for a path, a path to another folder finds nothing,
and the bare name still finds it.

Assisted-by: Claude Code:claude-opus-5-5
AI-Scope: the test client and its registration

(cherry picked from commit 15b7e4ccc462826ffff57c1e19bb04d35fa57296)
2026-10-28 23:16:28 -07:00

32 lines
1.1 KiB
C

#include <windows.h>
// A library loaded by its path is found again by that path: GetModuleHandleW and GetModuleHandleExW (pinning it) answer the
// handle LoadLibraryW returned, and a path to another folder finds nothing. Returns 0, or the number of the check that failed.
int raven_entry(void)
{
WCHAR path[MAX_PATH];
if (!GetModuleFileNameW(NULL, path, MAX_PATH))
return 1;
// The fixture library is built beside the program.
WCHAR *slash = path;
for (WCHAR *at = path; *at; ++at)
if (*at == L'\\')
slash = at + 1;
const WCHAR name[] = L"module-path-library.dll";
for (unsigned index = 0; index < sizeof name / sizeof *name; ++index)
slash[index] = name[index];
HMODULE loaded = LoadLibraryW(path);
if (!loaded)
return 2;
if (GetModuleHandleW(path) != loaded)
return 3;
HMODULE pinned = NULL;
if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN, path, &pinned) || pinned != loaded)
return 4;
if (GetModuleHandleW(L"C:\\nowhere\\module-path-library.dll"))
return 5;
if (GetModuleHandleW(L"module-path-library.dll") != loaded)
return 6;
return 0;
}