Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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 result := 0; FreeLibrary(dllHandle) ; begin ShowMessage('dll not found ') ; 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, 2015 at 16:58 public static string EchoString(string eString:string) is not valid C#. And it doesn't remotely match the Delphi code. – David Heffernan Jan 15, 2015 at 17:08

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

  • Use C++/CLI mixed mode to wrap the C#. You can then export functions in unmanaged mode in the usual way.
  • Use Robert Giesecke's UnmanagedExports. This is perhaps more convenient than a C++/CLI wrapper.
  • 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.

    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, 2015 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, 2015 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, 2015 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, 2015 at 17:10