在Delphi中从C DLL获取字符串返回值 [英] Get string return value from C DLL in Delphi

查看:737
本文介绍了在Delphi中从C DLL获取字符串返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C编写的遗留DLL,其中包含一个返回一个字符串的函数,我需要从Delphi访问这个函数。关于DLL的唯一信息是VB声明访问该函数:



公共声明函数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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆