3

My Delphi application is calling a function from a C++ DLL that should be returning strings like this.

C++ DLL

__declspec( dllexport ) void sample(char* str1, char* str2)
{
    strcpy(str1, "123");
    strcpy(str2, "abc");
}

Delphi

procedure sample(Str1, Str2: pchar); cdecl; external 'cpp.dll';
var
  buf1 : Pchar;
  buf2 : Pchar;
begin
  sample(@buf1, @buf2);
  //display buf1 and buf2
  //ShowMessage(buf1); //it display random ascii characters 
end;

What is the correct way to do this?

1
  • It's not very clear what the problem is? Commented Mar 4, 2014 at 10:26

1 Answer 1

5

You need to allocate memory for the C++ code to write to. For instance:

var
  buf1, buf2: array [0..255] of Char;
begin
  sample(buf1, buf2);
end;

You should also re-design your interface to accept the length of the buffer and so allow the DLL code to avoid buffer overrun.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.