<p>I have a legacy DLL written in C that contains a function that returns a string, and I need to access this function from Delphi. The only info I have about the DLL is the VB declare for accessing the function:</p>

<p>Public Declare Function DecryptStr Lib "strlib" (Str As String) As String </p>

<p>I've tried the following without success:</p>

<p>Declaration:</p>

<pre><code>function DecryptStr(s: PChar): PChar; cdecl; external 'strlib.dll';
</code></pre>

<p>Usage:</p>

<pre><code>var
  p1, p2 : pchar;
begin
  GetMem( p1, 255 );
  StrPCopy( p2, 'some string to decrypt' );
  p1 := DecryptStr( p2 );
end;
</code></pre>

<p>This consistently crashes the DLL with an Access Violation. I'm at a loss.</p>

<p>Any suggestions ?</p>

I'm guessing here, but are you sure it's cdecl? If the VB declare isn't mentioning it, I'd assume it's in fact a STDCALL function (STDCALL is quite common on Windows, as almost all of its native API uses it). Calling a function of one calling convention as if it were of another calling convention can really mess up the stack, usually leading to a crash.
Also, be sure to check whether the string is ANSI (LPSTR/LPCSTR) or UNICODE (LPWSTR/LPCWSTR). I don't know VB or Delphi, so I don't know what each one uses by default.