Coder Social home page Coder Social logo

Support loading dynamic addon modules (`.node` files) when embedding the Node.js shared library without needing to link with `node.def` about node HOT 1 OPEN

segevfiner avatar segevfiner commented on May 30, 2024
Support loading dynamic addon modules (`.node` files) when embedding the Node.js shared library without needing to link with `node.def`

from node.

Comments (1)

segevfiner avatar segevfiner commented on May 30, 2024

So basically something like the following (Untested though):
In node:

diff --git a/src/node_binding.cc b/src/node_binding.cc
index 00493f03f1..6f56f79dc5 100644
--- a/src/node_binding.cc
+++ b/src/node_binding.cc
@@ -457,6 +457,27 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
     return;  // Exception pending.
   }
 
+#if defined(_WIN32) && defined(NODE_SHARED_MODE)
+  HMODULE mod;
+  if (!GetModuleHandleExA(
+      GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
+      &DLOpen,
+      &mod
+    )) {
+      THROW_ERR_DLOPEN_FAILED(env, "TODO.");
+      return false;
+  }
+
+  // TODO Might want a dynamic allocation, with the possibility to opt into long path support in the future
+  char nodeLibrary[MAX_PATH];
+  if (!GetModuleFileNameA(mod, &nodeLibrary, sizeof(nodeLibrary))) {
+    THROW_ERR_DLOPEN_FAILED(env, "TODO.");
+    return false;
+  }
+
+  std::setenv("NODE_LIBRARY", nodeLibrary);
+#endif  // _WIN32
+
   node::Utf8Value filename(env->isolate(), args[1]);  // Cast
   env->TryLoadAddon(*filename, flags, [&](DLib* dlib) {
     static Mutex dlib_load_mutex;

In node-gyp:

diff --git a/src/win_delay_load_hook.cc b/src/win_delay_load_hook.cc
index 169f802..0c136ed 100644
--- a/src/win_delay_load_hook.cc
+++ b/src/win_delay_load_hook.cc
@@ -28,7 +28,7 @@ static FARPROC WINAPI load_exe_hook(unsigned int event, DelayLoadInfo* info) {
   if (_stricmp(info->szDll, HOST_BINARY) != 0)
     return NULL;
 
-  m = GetModuleHandle(NULL);
+  m = GetModuleHandle(std::getenv("NODE_LIBRARY"));
   return (FARPROC) m;
 }

from node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.