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.
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:
The flags that I had to change to make it work:
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 howto: http://joseoncode.com/2012/04/10/writing-your-first-native-module-for-node-dot-js-on-windows/
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With