How to Create DLL in C# and call in Delphi XE6

10,120

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.

Share:
10,120
ary
Author by

ary

Learning ASP.net MVC

Updated on June 28, 2022

Comments

  • ary
    ary over 1 year

    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;
    
  • Belogix
    Belogix almost 9 years
    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!
  • ary
    ary almost 9 years
    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??
  • David Heffernan
    David Heffernan almost 9 years
    Well, not all of them. These are your options. UnmanagedExports is the simplest.
  • ary
    ary almost 9 years
    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.
  • David Heffernan
    David Heffernan almost 9 years
    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.
  • ary
    ary almost 9 years
    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.
  • MBWise
    MBWise almost 7 years
    Anyone tried this: managed-vcl.com/downloads/full ?