Skip to content

Commit 9c894ec

Browse files
committed
Trying to solve issue of windows dlls.
1 parent 1a60665 commit 9c894ec

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

source/loader/include/loader/loader_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ LOADER_API int loader_impl_dependencies(loader_impl impl, detour d);
4949

5050
LOADER_API int loader_impl_link(plugin p, loader_impl impl);
5151

52+
LOADER_API dynlink loader_impl_dependency(loader_impl impl, const char *library);
53+
5254
LOADER_API detour_handle loader_impl_detour(loader_impl impl, const char *library, int (*load_cb)(detour, detour_handle));
5355

5456
LOADER_API void loader_impl_attach(loader_impl impl, plugin p);

source/loader/source/loader_impl.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,13 @@ int loader_impl_link(plugin p, loader_impl impl)
559559
return 0;
560560
}
561561

562+
dynlink loader_impl_dependency(loader_impl impl, const char *library)
563+
{
564+
dynlink library_handle = set_get(impl->library_map, (const set_key)library);
565+
566+
return library_handle;
567+
}
568+
562569
detour_handle loader_impl_detour(loader_impl impl, const char *library, int (*load_cb)(detour, detour_handle))
563570
{
564571
detour_handle handle = set_get(impl->detour_map, (const set_key)library);

source/loaders/node_loader/source/node_loader_impl.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3699,11 +3699,15 @@ void *node_loader_impl_register(void *node_impl_ptr, void *env_ptr, void *functi
36993699
#if defined(_WIN32) && defined(_MSC_VER) && (_MSC_VER >= 1200)
37003700
{
37013701
/* As the library handle is correctly resolved here, either to executable, library of the executable,
3702-
or the loader dependency we can directly obtain the handle of this dependency from a function pointer,
3703-
use any function that is contained in node runtime, in this case we are using napi_create_array */
3704-
if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)&napi_create_array, &node_loader_node_dll_handle))
3702+
or the loader dependency we can directly get the handle of this dependency */
3703+
dynlink node_library = loader_impl_dependency(node_impl->impl, "node");
3704+
3705+
node_loader_node_dll_handle = dynlink_get_impl(node_library);
3706+
3707+
if (node_loader_node_dll_handle == NULL)
37053708
{
3706-
napi_throw_type_error(env, nullptr, "Failed to initialize the hooking against node extensions load mechanism");
3709+
napi_throw_error(env, nullptr, "Failed to initialize the hooking against node extensions load mechanism");
3710+
return NULL;
37073711
}
37083712

37093713
detour d = detour_create(metacall_detour());
@@ -4032,7 +4036,7 @@ loader_impl_data node_loader_impl_initialize(loader_impl impl, configuration con
40324036
/* Result will never be defined properly */
40334037
node_impl->result = 0;
40344038

4035-
if (metacall_link_register_impl(impl, "node", "napi_register_module_v1", (void (*)(void))(&node_loader_port_initialize)) != 0)
4039+
if (metacall_link_register_loader(impl, "node", "napi_register_module_v1", (void (*)(void))(&node_loader_port_initialize)) != 0)
40364040
{
40374041
log_write("metacall", LOG_LEVEL_ERROR, "Node Loader failed to hook napi_register_module_v1");
40384042
}

source/metacall/include/metacall/metacall_link.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ METACALL_API int metacall_link_register(const char *tag, const char *library, co
9292
* @return
9393
* Zero if success, different from zero otherwise
9494
*/
95-
METACALL_API int metacall_link_register_impl(void *loader, const char *library, const char *symbol, void (*fn)(void));
95+
METACALL_API int metacall_link_register_loader(void *loader, const char *library, const char *symbol, void (*fn)(void));
9696

9797
/**
9898
* @brief

source/metacall/source/metacall_link.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ int metacall_link_register(const char *tag, const char *library, const char *sym
187187
return set_insert(metacall_link_table, (set_key)symbol, ptr);
188188
}
189189

190-
int metacall_link_register_impl(void *loader, const char *library, const char *symbol, void (*fn)(void))
190+
int metacall_link_register_loader(void *loader, const char *library, const char *symbol, void (*fn)(void))
191191
{
192192
void *ptr;
193193

source/tests/metacall_node_extension_test/node_extension_test/source/node_extension_test_win32_delay_load.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include <delayimp.h>
3030
#include <string.h>
3131

32+
static const char node_library_name[] = NODEJS_LIBRARY_NAME;
33+
3234
static FARPROC WINAPI load_exe_hook(unsigned int event, DelayLoadInfo *info)
3335
{
3436
HMODULE m;
@@ -38,7 +40,7 @@ static FARPROC WINAPI load_exe_hook(unsigned int event, DelayLoadInfo *info)
3840
return NULL;
3941
}
4042

41-
if (_stricmp(info->szDll, NODEJS_LIBRARY_NAME) != 0)
43+
if (_stricmp(info->szDll, node_library_name) != 0)
4244
{
4345
return NULL;
4446
}

0 commit comments

Comments
 (0)