.net native extension for node.js
Asked Answered
I

3

18

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 SharpAddon.cpp, 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.

Ineducation answered 29/6, 2012 at 7:21 Comment(2)
It's hard to tell if it's being actively developed/maintained, but if you don't need 100% compatibility with node.js, this looks interesting: newcome.wordpress.com/2010/05/08/… .Florescence
@Florescence Thanks for the link, but for the problem at hand I want to have the most stable and performing implementation possible, so I guess I'm stuck with the original, which is being developed at a really fast pace and is also getting steam on the Windows platform recently.Ineducation
I
13

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 http://nodejs.org/api/addons.html 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;
  callManaged();
  return scope.Close(String::New("world"));
}

Update Just discovered this link with an easy https://web.archive.org/web/20170702003703/http://joseoncode.com/2012/04/10/writing-your-first-native-module-for-node-dot-js-on-windows/

Ineducation answered 9/7, 2012 at 4:21 Comment(4)
+1 nice. Now I have one more way to write native extension for node.js and enjoy power of .NetGumption
@Gumption It should be kept in mind though that crossing the native / CLR boundary incurs a cost every time you do it - and that depends on how often native calls into .net (or the other way around). How high that cost is, I haven't measured yet.Ineducation
Thanks for the note. Once you run the program through the profiler, you may want to share the findings too, like on your blogGumption
The link is no longer active, can use "wayback machine". web.archive.org/web/20170702003703/http://joseoncode.com/2012/…Bale
G
6

Sounds like edge.js is the new answer from the author of iisnode:

Edge.js supports using C# and .NET instead of writing native node.js extensions

Growl answered 1/4, 2013 at 5:9 Comment(0)
I
0

These days, there's cmake-js and node-addon-api that make things both easier, plus the ABI of node-addon-api means the module does not need to be recompiled when used with a newer version of Node.js.

See this answer for a short tutorial: https://mcmap.net/q/741591/-how-to-use-mixed-c-amp-net-dll-in-node-js-error-abort-has-been-called

Ineducation answered 24/1, 2019 at 3:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.