Eugene Beresovsky Eugene Beresovsky - 4 months ago 18
Node.js Question

How to use mixed C++ & .Net dll in node.js? (Error: abort() has been called)

I want to create a native node extension using a dll containing C++ and C# code in Visual Studio 2015. I cannot make it work following my own instructions of yesteryear, which is based on the latest

node-gyp
.

When not using the
/clr
option, I can run a program like the following just fine.

console.log("1");
const addon = require('./build/Release/addon');
console.log("2");


When enabling
/clr
, only the first call to log gets executed. When compiling the dll in debug mode, I get the following message:

enter image description here

How to fix / debug this?

(I know there's edge, but I am trying to go the node-gyp way)

Answer

After unsuccessfully twiddling all (?) the compiler and linker options in VS2015, I found out how to set up my binding.gyp instead, in order to get .Net to work:

{
  "targets": [
    {
      "target_name": "addon",
      "sources": [ "hello.cc" ],
      "msbuild_settings": {
        "ClCompile": {
          "CompileAsManaged": "true",
          "ExceptionHandling": "Async",
        },
      },
    }
  ]
}

I verified the build by successfully executing the following mix of managed and unmanaged code:

#include <node.h>
#include <v8.h>

namespace demo {

  #pragma managed

  void callManaged()
  {
    System::String^ result = gcnew System::String("hola");
    System::Console::WriteLine("It works: " + result);
  }

  #pragma unmanaged

  using v8::FunctionCallbackInfo;
  using v8::Isolate;
  using v8::Local;
  using v8::Object;
  using v8::String;
  using v8::Value;

  void Method(const FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = args.GetIsolate();
    callManaged();
    args.GetReturnValue().Set(String::NewFromUtf8(isolate, "woooooorld"));
  }

  void init(Local<Object> exports) {
    NODE_SET_METHOD(exports, "hello", Method);
  }

  NODE_MODULE(addon, init)

}