Eugene Beresovsky Eugene Beresovsky - 2 months ago 11
Node.js Question

.net native extension for node.js

I want to make use of .net dlls in node.js. Does that mean I need to make those dlls available with c/c++ using 'clr hosting', a la

Unfortunately the example Creating a nodejs native .Net extension over at github was a bit of a disappointment, just scroll down to the last step

Change the "Common Language Runtime Support" option to No Common Language RunTime Support

and you know what I mean.
Correction to do that article justice: It suggests to change that option to "No Common Language RunTime Support" only for the file
, so other .cpp-files you add will have CLR support enabled (the default for a CLR project), which means you can in fact use .net dlls from those other .cpp files.

This question is actually a duplicate of Using a .NET DLL in Node.js / serverside javascript, which was written at a time when there was not even a native Windows port of node, so times might have changed, although google makes me doubt it.


Update: node-gyp can do the manual steps below automatically when the binding.gyp file is setup properly. See this answer for this simplified procedure.

It turned out to be rather easy. After struggling with CLR hosting and getting data in and out of the host for a while, it turns out you can actually enable /clr for your node extension no problem (so far). Here's how:

  • follow the instructions on to generate the project files
  • open the generated .sln in Visual Studio (I'm on VS 2010) and enable /clr in the project settings
  • now it probably won't build and you have to let the - in this case actually quite helpful - error messages guide you to the flags that conflict with /clr

The flags that I had to change to make it work:

  • disable /EHsc (C++ exceptions)
  • disable /RTC1 and /RTCsu
  • Release: change /MT to /MD
  • Debug: change /MTd to /MDd
  • Release: change /GR- to /GR

Then you can mix managed and unmanaged code like this, referencing your .net dlls.

#pragma managed

#using <managed.dll>

void callManaged()
    managed::Class1^ c1 = gcnew managed::Class1();
    System::String^ result = c1->Echo("hola");
    System::Console::WriteLine("It works: " + result);

#pragma unmanaged

Handle<Value> Method(const Arguments& args) {
  HandleScope scope;
  return scope.Close(String::New("world"));

Update Just discovered this link with an easy howto: