Calling a Delphi DLL from a C# .NET application

前端 未结 4 806
南笙
南笙 2020-12-02 07:46

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

4条回答
  •  盖世英雄少女心
    2020-12-02 08:33

    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.

提交回复
热议问题