8

In the unmanaged world, I was able to write a __declspec(dllexport) or, alternatively, use a .DEF file to expose a function to be able to call a DLL. (Because of name mangling in C++ for the __stdcall, I put aliases into the .DEF file so certain applications could re-use certain exported DLL functions.) Now, I am interested in being able to expose a single entry-point function from a .NET assembly, in unmanaged-fashion, but have it enter into .NET-style functions within the DLL. Is this possible, in a simple and straight-forward fashion?

What I have is a third-party program that I have extended through DLLs (plugins) that implement some complex mathematics. However, the third-party program has no means for me to visualize the calculations. I want to somehow take these pre-written math functions, compile them into a separate DLL (but using C++/CLI in .NET), but then add hooks to the functions so I can render what's going on under the hood in a .NET user control. I'm not sure how to blend the .NET stuff with the unmanaged stuff, or what to Google to accomplish this task.

Specific suggestions with regard to the managed/unmanaged bridge, or alternative methods to accomplish the rendering in the manner I have described would be helpful. Thank you.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
user343400
  • 171
  • 1
  • 3

3 Answers3

6

Well, the C++/CLI compiler makes it pretty easy. Just write a static managed function and attribute it with __declspec(dllexport). The compiler injects a stub that automatically loads the CLR to execute the managed code.

That's a serviceable approach, it isn't very extensible and it won't be very fast. The next step up is that you write a ref class with the [ComVisible(true)] attribute. After registering it with Regasm.exe, any unmanaged COM aware client can use that server. Hosting the CLR yourself (CorBindToRuntimeEx) is usually the last choice, but the most universal one.


Example code:

ref class ManagedClass {
public:
  static void StaticFunc() {}
};

extern "C" __declspec(dllexport)
void __stdcall UnmanagedFunc() {
  ManagedClass::StaticFunc();
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • If I need __stdcall on the name and want to get rid of the @number on the name mangling and need to create an alias in a .DEF file, how do I do that in this case? What I need is an extern "C" style signature so it can be called by a specific type of third party application. – user343400 May 20 '10 at 21:50
  • Simply use extern "C" and the __stdcall declarators on the export. You don't need a .def file. Sample posted. – Hans Passant May 20 '10 at 22:26
  • @HansPassant What do you mean by "compiler injects a stub" ? Does the compiler generate a .lib ( and the caller of UnmanagedFunc() would need to link with the lib)? – qqqqq Jan 13 '16 at 23:24
  • 1
    The C++ compiler auto-generates lots of magic code. That's the job of a compiler, it makes seemingly very difficult code look easy. Multiple inheritance with virtual base classes, that's rocket science. That stub, much easier. – Hans Passant Jan 13 '16 at 23:37
5

Do you use C++/CLI because you want to, or because you think you have to to export functions?

In case of the latter, check out my unmanaged exports, which allows you to declare unmanaged exports in C# equivalent to how DllImport works.

internal class Sample
{
  [DllExport("_export_test", CallingConvention.Cdecl)]
  static int Test(int a)
  {
     return a + 1;
  }
}
Robert Giesecke
  • 4,314
  • 21
  • 22
  • I wanted to use C++/CLI because the third-party library would compile automatically by just using the /clr option. Then adding tie-ins to the .NET framework would make life much better (I think.) Your example seems like a reasonable approach. Thank you. – user343400 May 17 '10 at 20:44
  • I am not sure I am following you here. When you take my project template, it will compile automatically as well (It will even create a .lib file). Except if I misunderstood you, and you actually want to use C++/CLI. – Robert Giesecke May 17 '10 at 21:01
3

This CodeProject article explains the process pretty well.

Using managed code in an unmanaged application
http://www.codeproject.com/KB/mcpp/ijw_unmanaged.aspx

See also here and here.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501