charto / nbind

:sparkles: Magical headers that make your C++ library accessible from JavaScript :rocket:
MIT License
1.98k stars 119 forks source link

Incompatible with electron 4 and later? #129

Open TanninOne opened 5 years ago

TanninOne commented 5 years ago

The operating system is Windows 10 I have a project that heavily relies on native libraries, some are based on nbind others use nan directly. On electron 2 and 3 they all work, on electron 4 (tested 4.1.3) I can't get the nbind-based modules to load.

When I try to load nbind-based native modules they all produce an error message, either "Module did not self-register." or "The specified procedure could not be found." I've debugged the second one and the error message from the api is "entry point not found".

Now I have a bit of an idea what's the trigger for this problem because starting with electron 4 they don't ship node.dll any more, that functionality is now apparently statically linked into electron.exe. Still, with the same build settings I got all the non-nbind modules to load correctly so I was wondering in how far nbind might be playing into this.

SpaceK33z commented 5 years ago

I can reproduce this too with https://github.com/2bbb/node-abletonlink. It works perfectly fine on macOS, but on Windows crashes with "Module did not self-register."

TanninOne commented 5 years ago

I think I found a fix, although I don't 100% understand it.

Merge this into your bindings.json

{
    "targets": [
        {
            "conditions": [
                [
                    'OS=="win"',
                    {
                        "libraries": [
                            "-DelayLoad:node.exe"
                        ],

So essentially adding the linker flag "/DelayLoad:node.exe".

This apparently allows electron.exe to take the place of node.exe when the native library loads symbols from it. Why I don't have to do that for all the other native libraries I have no clue.

SpaceK33z commented 5 years ago

Thanks so much @TanninOne, this workaround worked!

TanninOne commented 5 years ago

Another related thing I just found out and still don't fully understand: I was getting crashes when using c++ streams in a nbind extension running in electron.

After quite a bit of bumbling about what seems to be causing it is that the c++ runtime functions are now also delay loaded and for reasons I don't know it seems to try and load those function from the electron.exe instead of the proper runtime dlls.

Only solution I have so far is forcing the runtime to be linked statically.