title: Import Delphi DLL function into C++ Builder , transfer PAnsiChar
body: <p>I want to use Delphi code , export via DLL from C++ Builder </p>
  
  <p>Delphi Code fragment goes like this </p>
  
  <pre><code>//  function declare 
  function NameData(ItemIndex: Integer;
    Buffer: PAnsiChar; var BufSize: DWORD): DWORD; stdcall;
    external 'database.dll' 
  
  
  // function calling code 
    s1, S2: AnsiString;
  begin
  
  
    for i := 1 to  ...  do
    begin
      BufSize := 0;
      NameData(i, nil, BufSize);
      SetLength(s1, BufSize);
      NameData(i, PAnsiChar(s1), BufSize);
  
      mmo_dll.lines.Add(' name -&gt;  ' + string(s1));
  </code></pre>
  
  <p>relevant DLL code </p>
  
  <pre><code>library DLLCode; 
  
  
  function NameData(ItemIndex: Integer;
    Buffer: PAnsiChar; var BufSize: DWORD): DWORD; stdcall;
  var
    returnString: Ansistring;
  begin
    returnString := ' call some other functions .....';
  
    if BufSize &lt; Length(returnString) then
      result := ERROR_BUFFER_TOO_SMALL
    else
    begin
      StrPCopy(Buffer, returnString);
      result := ERROR_NO_ERROR;
    end;
    BufSize := Length(returnString);
  end;
  </code></pre>
  
  <p>this and a lot of more stuff works fine, Delphi and Delphi DLL.
  Now here is my not working C++ code :</p>
  
  <pre><code>//  function prototype 
  typedef void (__stdcall*IntCharIntIn_VoidOut)(int, PAnsiChar, int);
  
  // DLL prototype 
  extern "C" __declspec(dllimport)
      IntCharIntIn_VoidOut  __stdcall NameData(int, PAnsiChar, int);
  
  //  instance declaration 
  IntCharIntIn_VoidOut   NameData;
  
  
  //  load library data, no error raise,  other simpler function call already working
  
  ........
  NameData = (IntCharIntIn_VoidOut)::GetProcAddress(load,
              "NameData");
  
  
  ///  calling code 
      int  Bufsize;
      PAnsiChar DataName;
  
  
      for (i = 0; i &lt; count - 1; i++) {
  
          *Bufsize = 0;
  
          NameData(i, NULL, Bufsize);
  
          StrLen(SignalName);
  
          NameData(i, DataName, Bufsize );
  
  
          Memo1-&gt;Lines-&gt;Add(IntToStr(i));  // for test only 
      }
  </code></pre>
  
  <p>In the second call I get an access violation, but can't see why/where  I'm wrong </p>