Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash calling c# dll's method from native addon #1068

Closed
MatteoMeil opened this issue Jan 16, 2018 · 11 comments
Closed

Crash calling c# dll's method from native addon #1068

MatteoMeil opened this issue Jan 16, 2018 · 11 comments

Comments

@MatteoMeil
Copy link

From @MatteoMeil on January 16, 2018 18:26

  • Version: 7.10.0
  • Platform: Windows 10

I tried to build a C# dll(.NET Framework 4) with simple Hello World method in it. This is the code:

using System;
namespace ClassLibrary1
{
    public class Class1
    {
        public static void Hello()
        {
            Console.WriteLine("Hello World");
        }
    }
}

Then i tried to call this dll from C++ but terminal outputs nothing. I did this way:
C++:

#include <node.h>
#using <../ClassLibrary1.dll>

#pragma unmanaged

using namespace v8;
void Greet();
void Method(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();
  Greet();
  args.GetReturnValue().Set(Boolean::New(isolate, true));
}
void init(Local<Object> exports) {
  NODE_SET_METHOD(exports, "hello", Method);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, init)

#pragma managed

void Greet(){
	ClassLibrary1::Class1::Hello();
}

binding.gyp

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

Files in folder:

Origine.cc
binding.gyp
ClassLibrary1.dll  

Copied from original issue: nodejs/node#18183

@MatteoMeil
Copy link
Author

From @jasnell on January 16, 2018 18:58

@MatteoMeil ... your best bet for this type of question would be to ask over in the https://github.com/nodejs/help repository.

@addaleax
Copy link
Member

I’m not a Windows or C# expert, so I’m not sure what kind of help I can provide. But on a very general level: Can you give more information than saying it ”crashes”? Did you try running your code (i.e. node itself, with arguments pointing to your script) in a debugger?

@MatteoMeil
Copy link
Author

@addaleax I tried to debug with VS2017 following these steps:

  • download Node.js 7.10.0 from source
  • build it with debug simbols (command vcbuild.bat debug nosign x64 from elevated terminal)
  • run node-gyp configure --debug --nodedir="path/to/builded/node-v7.10.0"
  • run node-gyp build --debug
    but in the last step I came in an error so I couldn't debug anything. This is the error (it's written in italian)
Origine.obj : error LNK2028: al token non risolto (0A000241) "extern "C" void __cdecl _invalid_p
arameter(wchar_t const *,wchar_t const *,wchar_t const *,unsigned int,unsigned __int64)" (?_inva
lid_parameter@@$$J0YAXPEB_W00I_K@Z) si fa riferimento nella funzione "public: void __cdecl std::
_Iterator_base12::_Orphan_me(void)" (?_Orphan_me@_Iterator_base12@std@@$$FQEAAXXZ) [C:\Users\Mat
teo\Desktop\Cartella\build\addon.vcxproj]
Origine.obj : error LNK2019: riferimento al simbolo esterno "extern "C" void __cdecl _invalid_pa
rameter(wchar_t const *,wchar_t const *,wchar_t const *,unsigned int,unsigned __int64)" (?_inval
id_parameter@@$$J0YAXPEB_W00I_K@Z) non risolto nella funzione "public: void __cdecl std::_Iterat
or_base12::_Orphan_me(void)" (?_Orphan_me@_Iterator_base12@std@@$$FQEAAXXZ) [C:\Users\Matteo\Des
ktop\Cartella\build\addon.vcxproj]
C:\Users\Matteo\Desktop\Cartella\build\Debug\addon.node : fatal error LNK1120: 2 esterni non ris
olti [C:\Users\Matteo\Desktop\Cartella\build\addon.vcxproj]

@gireeshpunathil
Copy link
Member

I was able to reproduce the problem (terminal outputs nothing) with the latest node (I did not try 7.10.x as it is not LTS anyways)

#echo %errorlevel% gave me

-532462766 which is 0xE0434352 - whenever we have the CLR linkage in the addon.

@MatteoMeil - can you confirm if this is happening at your end too?

I am wondering whether the top level executable (node.exe) too should be built with /clr option to avail the common language runtime support, other than the native addon.

/cc @nodejs/platform-windows

@MatteoMeil
Copy link
Author

@gireeshpunathil Yes, I can confirm this.
To avoid CLR linkage in the addon i tried to follow this guide to wrap C# code into a C++/CLI dll and then call dll's method from unmanaged C++ but I gave no results

@benjamingr
Copy link
Member

Have you tried https://github.com/tjanczuk/edge?

@MatteoMeil
Copy link
Author

@benjamingr
I ended up using edge but I'm still trying to resolve this issue

@benjamingr
Copy link
Member

Maybe @tjanczuk can weigh in?

@gireeshpunathil
Copy link
Member

/cc @nodejs/platform-windows again. Questions:

  • does node support common language runtime support for addons linked into it?
  • if so, what is the guidance for addon build configuration?

@benjamingr
Copy link
Member

does node support common language runtime support for addons linked into it?

Not officially as far as I know.

@gireeshpunathil
Copy link
Member

Closing as answered: No support exists for CLR. If there is a strong use case exists for this, please you may raise a feature request in nodejs/node repo. Also, if you have ideas on how to bring in support, PRs welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants