Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

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 EXEExchanging 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.

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.

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.

Source Link
vcldeveloper
  • 7.5k
  • 2
  • 34
  • 39

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.