0

Let's say I'm writing a C# class with a simple method which returns a int type for example. I build the project which is a class library and then I obtain a dll. Can I use that dll to call that simple method but in C language?

I've been doing some research and I found out that is possible if a have a dll written in c, and called in C# but nothing about the vice versa scenario.

Thank you in advance!

Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
Liviu Sosu
  • 1,381
  • 3
  • 15
  • 26
  • 1
    Not in any simple way. See this relevant question, it may apply: http://stackoverflow.com/questions/14257428/reverse-pinvoke-from-native-c . It's asked for C++, but I don't enough about the differences to say it's also relevant for C. – Rotem Oct 23 '14 at 10:12
  • 1
    Have a look to https://www.nuget.org/packages/UnmanagedExports that may help you. I tried it to wrap a C# .NET dll and used it from Delphi as a native dll. – Omar.Alani Oct 23 '14 at 10:30

1 Answers1

0

It is not so straightforward. Since C# is a managed language, it run in a sort of infrastructure, ant to call it from another language outside the dotnet environment you have to host such environment. You can have a look here to start working on: CorBindToRuntimeEx. If you can work with C++ the less painful integration is C++ / CLI http://msdn.microsoft.com/en-us/magazine/cc163681.aspx#S1 that allow you to mix call in C/c++ with call to c# libraries in a quite low friction way.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115