34

EDIT: I've posted a better implementation of this, below. I left this here so the responses would make sense.

I've done numerous searches for the correct method for writing a DLL in Delphi, and being able to call it from C#, passing and returning strings. A lot of the information was incomplete or incorrect. After much trial and error, I found the solution.

This was compiled using Delphi 2007 and VS 2010. I suspect it will work fine in other versions as well.

Here's the Delphi code. Remember to include version information in the project.

library DelphiLibrary;

uses SysUtils;

// Compiled using Delphi 2007.

// NOTE: If your project doesn't have version information included, you may
// receive the error "The "ResolveManifestFiles" task failed unexpectedly"
// when compiling the C# application.

{$R *.res}

// Example function takes an input integer and input string, and returns
// inputInt + 1, and inputString + ' ' + IntToStr(outputInt) as output
// parameters. If successful, the return result is nil (null), otherwise it is
// the exception message string.


// NOTE: I've posted a better version of this below. You should use that instead.

function DelphiFunction(inputInt : integer; inputString : PAnsiChar;
                        out outputInt : integer; out outputString : PAnsiChar)
                        : PAnsiChar; stdcall; export;
var s : string;
begin
  outputInt := 0;
  outputString := nil;
  try
    outputInt := inputInt + 1;
    s := inputString + ' ' + IntToStr(outputInt);
    outputString := PAnsiChar(s);
    Result := nil;
  except
    on e : exception do Result := PAnsiChar(e.Message);
  end;
end;

// I would have thought having "export" at the end of the function declartion
// (above) would have been enough to export the function, but I couldn't get it
// to work without this line also.
exports DelphiFunction;

begin
end.

Here's the C# code:

using System;
using System.Runtime.InteropServices;

namespace CsharpApp
{
    class Program
    {
        // I added DelphiLibrary.dll to my project (NOT in References, but 
        // "Add existing file"). In Properties for the dll, I set "BuildAction" 
        // to None, and "Copy to Output Directory" to "Copy always".
        // Make sure your Delphi dll has version information included.

        [DllImport("DelphiLibrary.dll", 
                   CallingConvention = CallingConvention.StdCall, 
                   CharSet = CharSet.Ansi)]
        public static extern 
            string DelphiFunction(int inputInt, string inputString,
                                  out int outputInt, out string outputString);

        static void Main(string[] args)
        {
            int inputInt = 1;
            string inputString = "This is a test";
            int outputInt;
            string outputString;


// NOTE: I've posted a better version of this below. You should use that instead.


            Console.WriteLine("inputInt = {0}, intputString = \"{1}\"",
                              inputInt, inputString);
            var errorString = DelphiFunction(inputInt, inputString,
                                             out outputInt, out outputString);
            if (errorString != null)
                Console.WriteLine("Error = \"{0}\"", errorString);
            else
                Console.WriteLine("outputInt = {0}, outputString = \"{1}\"",
                                  outputInt, outputString);
            Console.Write("Press Enter:");
            Console.ReadLine();
        }
    }
}

I hope this information helps someone else to not have to pull their hair out as much as I did.

5
  • Not really a question, but +1 :). Commented Nov 12, 2010 at 9:57
  • I'm not familiar with delphi but know if is possible to convert it to COM its easy to use it in c#, I made a little search and find some resource which is about delphi and COM relation here: delphi.about.com/library/weekly/aa122804a.htm Commented Nov 12, 2010 at 10:05
  • 6
    You should rephrase your question to say "What is the proper way to use a Delphi DLL from a C# .NET application?" and then answer yourself with the rest of your post. See stackoverflow.com/faq (You may answer your own question) and here: meta.stackexchange.com/questions/12513/… Commented Nov 12, 2010 at 10:36
  • 8
    You need to be very careful here. Delphi strings are reference counted; the refcount of s in DelphiFunction will be zero at the end of the function, so the memory that is used by s will be returned to the memory allocator, and potentially be used (and overwritten) by something else after DelphiFunction returns before the C# caller can fetch the content. When that happens, all sorts of havoc will occur. In a multi-threaded situation (especially on multi-core systems) that can be really soon. Commented Nov 12, 2010 at 12:13
  • Jens - Thanks for the information. I wondered if I was doing this right. -Dan
    – Dan Thomas
    Commented Nov 14, 2010 at 14:28

5 Answers 5

29

Based on responses to my post, I have created a new example that uses string buffers for the returned strings, instead of just returning PAnsiChars.

Delphi DLL source:

library DelphiLibrary;

uses SysUtils;

// Compiled using Delphi 2007.

// NOTE: If your project doesn't have version information included, you may
// receive the error "The "ResolveManifestFiles" task failed unexpectedly"
// when compiling the C# application.

{$R *.res}

// A note on returing strings. I had originally written this so that the
// output string was just a PAnsiChar. But several people pointed out that
// since Delphi strings are reference-counted, this was a bad idea since the
// memory for the string could get overwritten before it was used.
//
// Because of this, I re-wrote the example so that you have to pass a buffer for
// the result strings. I saw some examples of how to do this, where they
// returned the actual string length also. This isn't necessary, because the
// string is null-terminated, and in fact the examples themselves never used the
// returned string length.


// Example function takes an input integer and input string, and returns
// inputInt + 1, and inputString + ' ' + IntToStr(outputInt). If successful,
// the return result is true, otherwise errorMsgBuffer contains the the
// exception message string.
function DelphiFunction(inputInt : integer;
                        inputString : PAnsiChar;
                        out outputInt : integer;
                        outputStringBufferSize : integer;
                        var outputStringBuffer : PAnsiChar;
                        errorMsgBufferSize : integer;
                        var errorMsgBuffer : PAnsiChar)
                        : WordBool; stdcall; export;
var s : string;
begin
  outputInt := 0;
  try
    outputInt := inputInt + 1;
    s := inputString + ' ' + IntToStr(outputInt);
    StrLCopy(outputStringBuffer, PAnsiChar(s), outputStringBufferSize-1);
    errorMsgBuffer[0] := #0;
    Result := true;
  except
    on e : exception do
    begin
      StrLCopy(errorMsgBuffer, PAnsiChar(e.Message), errorMsgBufferSize-1);
      Result := false;
    end;
  end;
end;

// I would have thought having "export" at the end of the function declartion
// (above) would have been enough to export the function, but I couldn't get it
// to work without this line also.
exports DelphiFunction;

begin
end.

C# Code:

using System;
using System.Runtime.InteropServices;

namespace CsharpApp
{
    class Program
    {
        // I added DelphiLibrary.dll to my project (NOT in References, but 
        // "Add existing file"). In Properties for the dll, I set "BuildAction" 
        // to None, and "Copy to Output Directory" to "Copy always".
        // Make sure your Delphi dll has version information included.

        [DllImport("DelphiLibrary.dll", 
                   CallingConvention = CallingConvention.StdCall, 
                   CharSet = CharSet.Ansi)]
        public static extern bool 
            DelphiFunction(int inputInt, string inputString,
                           out int outputInt,
                           int outputStringBufferSize, ref string outputStringBuffer,
                           int errorMsgBufferSize, ref string errorMsgBuffer);

        static void Main(string[] args)
        {
            int inputInt = 1;
            string inputString = "This is a test";
            int outputInt;
            const int stringBufferSize = 1024;
            var outputStringBuffer = new String('\x00', stringBufferSize);
            var errorMsgBuffer = new String('\x00', stringBufferSize);

            if (!DelphiFunction(inputInt, inputString, 
                                out outputInt,
                                stringBufferSize, ref outputStringBuffer,
                                stringBufferSize, ref errorMsgBuffer))
                Console.WriteLine("Error = \"{0}\"", errorMsgBuffer);
            else
                Console.WriteLine("outputInt = {0}, outputString = \"{1}\"",
                                  outputInt, outputStringBuffer);

            Console.Write("Press Enter:");
            Console.ReadLine();
        }
    }
}

And here's an additional class that shows how to load the DLL dynamically (sorry for the long lines):

using System;
using System.Runtime.InteropServices;

namespace CsharpApp
{
    static class DynamicLinking
    {
        [DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
        static extern int LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);

        [DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
        static extern IntPtr GetProcAddress(int hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);

        [DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
        static extern bool FreeLibrary(int hModule);

        [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        delegate bool DelphiFunction(int inputInt, string inputString,
                                     out int outputInt,
                                     int outputStringBufferSize, ref string outputStringBuffer,
                                     int errorMsgBufferSize, ref string errorMsgBuffer);

        public static void CallDelphiFunction(int inputInt, string inputString,
                                              out int outputInt, out string outputString)
        {
            const string dllName = "DelphiLib.dll";
            const string functionName = "DelphiFunction";

            int libHandle = LoadLibrary(dllName);
            if (libHandle == 0)
                throw new Exception(string.Format("Could not load library \"{0}\"", dllName));
            try
            {
                var delphiFunctionAddress = GetProcAddress(libHandle, functionName);
                if (delphiFunctionAddress == IntPtr.Zero)
                    throw new Exception(string.Format("Can't find function \"{0}\" in library \"{1}\"", functionName, dllName));

                var delphiFunction = (DelphiFunction)Marshal.GetDelegateForFunctionPointer(delphiFunctionAddress, typeof(DelphiFunction));

                const int stringBufferSize = 1024;
                var outputStringBuffer = new String('\x00', stringBufferSize);
                var errorMsgBuffer = new String('\x00', stringBufferSize);

                if (!delphiFunction(inputInt, inputString, out outputInt,
                                    stringBufferSize, ref outputStringBuffer,
                                    stringBufferSize, ref errorMsgBuffer))
                    throw new Exception(errorMsgBuffer);

                outputString = outputStringBuffer;
            }
            finally
            {
                FreeLibrary(libHandle);
            }
        }
    }
}

-Dan

1
  • I'm trying to use the dynamic version above with a Delphi procedure that I do not have the source code for, but I know that the function has a void return and accepts a single bool as an argument. I get a PInvokeStackImbalance exception when I run it. Can I pass a C# bool to Delphi procedure, or do I have to cast it to another type, such as byte? Commented Dec 19, 2018 at 13:47
5

As Jeroen Pluimers said in his comment, you should take note that Delphi strings are reference-counted.

IMO, in such circumstances which you are supposed to return a string in heterogeneous environments, you should ask the caller to provide a buffer for the result, and the function should fill that buffer. This way, the caller is responsible for creating the buffer and disposing it when it is done with it. If you take a look at Win32 API functions, you'll see they do the same when they need to return a string to a caller.

To do so, you can use PChar (either PAnsiChar or PWideChar) as the type of function parameter, but you should also ask caller to provide size of the buffer too. Take a look at my answer in the link below, for a sample source code:

Exchanging strings (PChar) between a Freepascal compiled DLL and a Delphi compiled EXE

The question is specifically about exchanging string between FreePascal and Delphi, but the idea and the answer is applicable to your case too.

2
  • Thanks to both of you for pointing this out - it's the kind of potential bug that could take forever to weed out. In the back of my mind I was wondering about this, but I didn't pay enough attention to that little voice of reason. Shame on me. ;p I've posted a better example below, that fixes this issue.
    – Dan Thomas
    Commented Nov 14, 2010 at 16:42
  • In my testing I've found that passing back the result of StrNew(PChar(s)) doesn't error like GetMem -> StrPLCopy, is this any safer? Commented Sep 24, 2013 at 10:19
3

In Delphi 2009 the code works better if you explicitly type variable s as an AnsiString viz:

var s : Ansistring;

giving the expected result from C# following the call:

outputInt = 2, outputString = "This is a test 2"

instead of

outputInt = 2, outputString = "T"
0

It is easier to retireve a string using PString:

function DelphiFunction(inputString : PAnsiChar;
                    var outputStringBuffer : PString;
                    var errorMsgBuffer : PString)
                    : WordBool; stdcall; export;
var 
  s : string;
begin
  try
    s := inputString;
    outputStringBuffer:=PString(AnsiString(s));
    Result := true;
  except
    on e : exception do
    begin
      s:= 'error';
      errorMsgBuffer:=PString(AnsiString(e.Message));
      Result := false;
    end;
  end;
end;

In c# then:

    const int stringBufferSize = 1024;

    var  str = new    IntPtr(stringBufferSize);

    string loginResult = Marshal.PtrToStringAnsi(str);
1
  • 1
    You are returning a pointer to a local variable on the stack. Once DelphiFunction returns, that variable won't be valid. It may still work by luck, but you shouldn't rely on it.
    – dan-gph
    Commented Nov 12, 2015 at 4:53
0

If somebody experienced this error in 2022 or later, use VisualStudio 2010 to compile DLLImport and P/Invoke operations, the future versions (may be except for Visual Studio 2012) do not allow to load managed code from Delphi in C# application that target x64 machiene system from an unmanaged x86 DLL library. Use .Net Framework 4.0 instead of .Net Framework 4.8 or later, also avoid using .Net Core, Standard and .Net when dealing with / exporting from RAD Studio and Delphi compilers.

Not the answer you're looking for? Browse other questions tagged or ask your own question.