在Delphi中从C DLL获取字符串返回值 [英] Get string return value from C DLL in Delphi
问题描述
公共声明函数DecryptStr Libstrlib(Str As String)As String
我没有成功尝试以下操作:
声明:
函数DecryptStr(s:PChar):PChar;
CDECL;外部strlib.dll;
用法:
code> var
p1,p2:pchar;
begin
GetMem(p1,255);
StrPCopy(p2,'some string to decrypt');
p1:= DecryptStr(p2);
结束
这样一来就会使用一个访问冲突的DLL。我很失望。
任何建议?
考虑重写您的测试代码如下:
var
p1,p2:pchar;
begin
GetMem(p1,255); //初始化
GetMem(p2,255);
StrPLCopy(p2,'some string to decrypt',255); //阻止缓冲区溢出
StrPLCopy(p1,DecryptStr(p2),255); // make a copy,因为dll将释放其内部缓冲区
end;
如果在调用DecryptStr之前仍然失败,请阅读 http://support.microsoft.com/kb/187912 。
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:
Public Declare Function DecryptStr Lib "strlib" (Str As String) As String
I've tried the following without success:
Declaration:
function DecryptStr(s: PChar): PChar; cdecl; external 'strlib.dll';
Usage:
var
p1, p2 : pchar;
begin
GetMem( p1, 255 );
StrPCopy( p2, 'some string to decrypt' );
p1 := DecryptStr( p2 );
end;
This consistently crashes the DLL with an Access Violation. I'm at a loss.
Any suggestions ?
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;
If still fails within a call to DecryptStr, then read http://support.microsoft.com/kb/187912 carefully.
这篇关于在Delphi中从C DLL获取字符串返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!