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