6

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

My C# & Delphi code looks like what I have posted below. In the code GetProcAddress is returning NIL. Please advise if I am missing 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;
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
ary
  • 939
  • 2
  • 13
  • 32
  • I changed code as per "unmanaged programms" suggestion. still no luck. – ary Jan 15 '15 at 16:54
  • I don't understand that comment. I rolled back your edit. Please don't change the question. By all means add more to ask for clarification, but don't make changes that completely invalidate the history. – David Heffernan Jan 15 '15 at 16:58
  • Also, it would be much better if you didn't post fake code. The C# code is fake. – David Heffernan Jan 15 '15 at 16:59
  • `public static string EchoString(string eString:string)` is not valid C#. And it doesn't remotely match the Delphi code. – David Heffernan Jan 15 '15 at 17:08

1 Answers1

4

A C# DLL is a managed assembly and does not export its functionality via classic PE exports. Your options:

  1. Use C++/CLI mixed mode to wrap the C#. You can then export functions in unmanaged mode in the usual way.
  2. Use Robert Giesecke's UnmanagedExports. This is perhaps more convenient than a C++/CLI wrapper.
  3. Expose the managed functionality as a COM object.

Once you get as far as choosing one of these options you will have to deal with your misuse of the string data type. That's a private Delphi data type that is not valid for interop. For the simple example in the question PWideChar would suffice.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I just marked to close as duplicate and linked to your earlier answer! In the comments of your answer it even mentions how frequently this question is asked :-) I have still up-voted your answer though! – Belogix Jan 15 '15 at 16:25
  • So are these the addtional steps I need to take? 1.Install-Package UnmanagedExports in C#. Add [DllExport("EchoString")] above EchoString function in C#. I did the above steps and still its not working. Any thing else I am missing?? – ary Jan 15 '15 at 16:40
  • Well, not all of them. These are your options. UnmanagedExports is the simplest. – David Heffernan Jan 15 '15 at 16:40
  • So after installing Nuget package, I added this code [DllExport("echostring", CallingConvention = CallingConvention.StdCall)] above echostring function. Still delphi is not finding the procedure. What else am I missing. Thanks. – ary Jan 15 '15 at 17:09
  • The function names don't match. You have to get the letter case correct. Also, stop using strings for at least a little while. See if you can get success with simple integers. Then move on to more complex data. See my update. Finally, use `external` rather than runtime linking. It makes the code much simpler. – David Heffernan Jan 15 '15 at 17:10
  • Also there was problem with "Platform Target" in C#. As soon as I changed it to x86, its working fine. Earlier it was set to "Any CPU". Thanks all for help. – ary Jan 15 '15 at 18:16
  • Anyone tried this: http://www.managed-vcl.com/downloads/full/ ? – MBWise Feb 03 '17 at 09:49