How to create a DLL in C # and call in Delphi XE6

I created a DLL in VS2013 using File / New Project / Class Library. Then I tried to load it dynamically into Delphi. But Delphiis returns NIL for the GetProcAddress procedure.

My C # and Delphi code looks like below. The GetProcAddress code returns NIL . Please let me know if I missed something.

C # code

 using System; namespace TestDLL { public class Class1 { public static string EchoString(string eString) { return eString; } } } 

Delphi Code

  Type TEchoString = function (eString:string) : integer;stdcall; function TForm1.EchoString(eString:string):integer; begin dllHandle := LoadLibrary('TestDLL.dll') ; if dllHandle <> 0 then begin @EchoString := GetProcAddress(dllHandle, 'EchoString') ; if Assigned (EchoString) then EchoString(eString) //call the function else result := 0; FreeLibrary(dllHandle) ; end else begin ShowMessage('dll not found ') ; end; end; 
+5
source share
1 answer

The C # DLL is a managed assembly and does not export its functionality through classic PE export. Your options:

  • Use C ++ / CLI mixed mode to port C #. Then you can export the functions in unmanaged mode in the usual way.
  • Use Robert Giesecke UnmanagedExports . This is perhaps more convenient than the C ++ / CLI shell.
  • Open managed functionality as a COM object.

Once you select one of these options, you will have to deal with the misuse of the string data type. This is a private Delphi data type that is not valid for interaction. For a simple example in a question, PWideChar would be enough.

+4
source

Source: https://habr.com/ru/post/1211175/


All Articles