I have a C++ program, that calls Delphi DLL to initialize a buffer that contains chars.
I am testing out the interface to make sure the data are passed correctly:
In C++ program:
char * pMsg = (char*)malloc(3); //allocate buffer
Init(pMsg * char); //Delphi DLL function
In Delphi DLL:
procedure Init(pMsg:PChar);
var
pHardcodedMsg:PChar;
begin
pHardcodedMsg:= '123';
CopyMemory(pMsg, pHardcodedMsg, Length(pHardcodedMsg));
end;
But, when I try to printf( (const char*)pMsg )
in C++,
It shows me "123
" followed by some rubbish characters.
Why is this so? How can I successfully place an array of char into the buffer and have the string printed out correctly?