从C DLL在Delphi中获取字符串返回值 [英] Get string return value from C DLL in Delphi
问题描述
我已经写在包含返回字符串的函数C遗留DLL,我需要从德尔福访问该功能。我对DLL的唯一信息是VB申报访问功能:
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:
公开声明函数库DecryptStrSTRLIB(str作为字符串)作为字符串
Public Declare Function DecryptStr Lib "strlib" (Str As String) As String
我试过下面没有成功:
声明:
function DecryptStr(s: PChar): PChar; cdecl; external 'strlib.dll';
用法:
var
p1, p2 : pchar;
begin
GetMem( p1, 255 );
StrPCopy( p2, 'some string to decrypt' );
p1 := DecryptStr( p2 );
end;
这始终崩溃访问冲突该DLL。我不知所措。
This consistently crashes the DLL with an Access Violation. I'm at a loss.
有什么建议?
推荐答案
考虑重写你的测试code如下:
Consider rewriting your test code as follows:
var
p1, p2 : pchar;
begin
GetMem( p1, 255 ); // initialize
GetMem( p2, 255 );
StrPLCopy( p2, 'some string to decrypt', 255 ); // prevent buffer overrun
StrPLCopy( p1, DecryptStr( p2 ), 255); // make a copy since dll will free its internal buffer
end;
如果要DecryptStr呼叫内仍然失败,那么阅读 http://support.microsoft.com/kb/187912小心。
If still fails within a call to DecryptStr, then read http://support.microsoft.com/kb/187912 carefully.
这篇关于从C DLL在Delphi中获取字符串返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!