Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

c# - .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 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 howto: http://joseoncode.com/2012/04/10/writing-your-first-native-module-for-node-dot-js-on-windows/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...