metacall / core

MetaCall: The ultimate polyglot programming experience.
https://metacall.io
Apache License 2.0
1.55k stars 160 forks source link

Cannot load libnode on Windows [126 error] #479

Closed twelvee closed 7 months ago

twelvee commented 7 months ago

🐛 Bug Report

The problem is in the initialization of metacall in the embedded environment. All paths are set correctly, but metacall cannot initialize and crashes with code 126 and the following log

Failed to load: *correct_absolute_path*\node_loader.dll with error code [126]

The problem is due to the fact that the executable is not in the same directory, so we can't get the correct path to the libnode.dll file.

Expected Behavior

All paths are set correctly, metacall should have initialized successfully.

Current Behavior

We get a rather strange error that can be misleading. The file exists, but the error 126 not found.

Possible Solution

We can use SetDllDirectoryA(); to set the correct path to the lib folder where all the dll files needed for metacall are located.

Tested by calling this method before initializing metacall:

SetDllDirectoryA(std::filesystem::absolute("../../metacall/lib").string().c_str());

after which we call metacall_initialize() and metacall is successfully initialized.

Steps to Reproduce

I'm working on a rather unique case study - embedding metacall inside a Source2 game server. So the steps for reproduce are not exactly the same as I used, but I think it will work just as well.

  1. Place the executable file in a different location than metacall.dll and other metacall libraries
  2. Inject metacall.dll in the runtime of this executable
  3. Run metacall_initialize and metacall_load_from_file

Context (Environment)

executable located: E:\cs2\server\game\bin\win64\cs2.exe metacall libraries (including metacall.dll) located: E:\cs2\server\game\csgo\addons\resourcemod\bin\metacall resourcemod (a dll file that I inject into a game and it runs metacall) located: E:\cs2\server\game\csgo\addons\resourcemod\bin\ loader: node

viferga commented 7 months ago

I have added support for this: https://github.com/metacall/core/commit/203a3e79c57d75bb2b7e3599bd65da64ced9d303

I am going to update node in windows, tag a new version and update the distributable, once it's done, could you confirm it works?

viferga commented 7 months ago

@twelvee https://github.com/metacall/node.dll/releases/tag/v0.0.6 https://github.com/metacall/distributable-windows/releases/tag/v0.1.6