i don’t work well with C++ but now i need build a function that call Delphi DLL and pass a string to DLL and get return new string.
here is my Delphi DLL code:
library testdll;
uses
System.Classes,Winapi.Windows,System.SysUtils;
{$R *.res}
function hello(name : PWideChar):PWideChar;
var
rs:PWideChar;
begin
rs:=PWideChar('Hello '+rs);
Result:=rs;
end;
exports
hello;
begin
end.
Anyone can help me create simple code in C++ to call and get result form hello function, thank for help.
You are trying to concat a PWideChar to a String literal and return it as another PWideChar. That will not work as-is. You should not be returning a PWideChar anyway. That leads to memory management nightmares. A better design is to let the caller pass a buffer into the DLL to fill in instead, eg:
Then, given this C++ declaration::
You can call it all kinds of different ways, depending on your needs:
The same kind of code can be translated to Pascal very easily if you ever need to use the same DLL in Delphi: