4

How can I export my c# dll functions to be able to use them from unmanaged programs.

  • For example : Dll in c# and the application in Delphi/c++ .

I don't have the source of the exe because is not Open S. Can't use COM.

Jax
  • 977
  • 8
  • 22
  • 40
  • Had some problems with that Nuget and found quite easy way https://stackoverflow.com/questions/225277/how-to-call-managed-code-from-unmanaged-code/68692560#68692560 – Jan Aug 07 '21 at 13:33

1 Answers1

9

I recommend using Robert Giesecke's Unmanaged Exports.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I'm constantly amused that this question gets asked repeatedly and always gets a different answer :\ – Michael Edenfield Sep 28 '12 at 21:51
  • @Michael It always gets the same answer from me. – David Heffernan Sep 28 '12 at 21:55
  • The problem is this gets asked so often that the same people rarely see it: http://stackoverflow.com/questions/2812338/reverse-pinvoke-and-create-a-full-unmanaged-c-sharp-program, http://stackoverflow.com/questions/225277/calling-managed-code-from-unmanaged-code, http://stackoverflow.com/questions/10821109/calling-managed-code-from-unmanaged-c, http://stackoverflow.com/questions/1635679/how-to-use-managed-code-from-unmanaged-code – Michael Edenfield Sep 28 '12 at 22:01
  • 2
    Your answer, BTW, is easily the "best"; maybe you should go add it to the rest of the list :) – Michael Edenfield Sep 28 '12 at 22:02
  • 1
    Still waiting for an answer that explains how it works. Without that, you'll never know when it *stops* working or how expensive it is. Those stubs certainly are not cheap. Giesecke prefers to be mysterious, more fun that way. – Hans Passant Sep 28 '12 at 22:49
  • 3
    @HansPassant: You have have found out in the over 2 years since that comment, but if not, It works using the the frameworks reverse P/Invoke support. (This makes your assembly platform-specific.) Basically in IL you can add `.export [1]` to a method to export a method in slot one of the unmanaged export table. A marshaling thunk gets generated at runtime. Use the normal marshaling attributes like you would with forward P/Invoke to customize the effective native signature. The `.export` clause also takes an optional 'as exported_name' clause to allow you to set the name in the export table. – Kevin Cathcart Oct 17 '14 at 22:10
  • @HansPassant: (Continued). Chapter 18 of "Expert .NET 2.0 IL Assembler" describes this is slightly more detail, although I suspect better books exist. – Kevin Cathcart Oct 17 '14 at 22:11
  • 2
    Sure, I just use C++/CLI to accomplish the exact same thing. It just takes __declspec(dllexport). I can set a breakpoint on it. – Hans Passant Oct 17 '14 at 22:19