从Delphi XE调用Delphi 7 DLL

Hei*_*inz 3 delphi dll interface delphi-7 delphi-xe2

我需要在Delphi 7中包含一些遗留代码,以便在Delphi XE2中使用.我的问题似乎很简单,但我尝试了所有我能找到的例子,但都失败了.基本上,我需要能够D7和DXE2之间传递一个字符串,而据我可以计算出,最安全的方法是使用PChar类型(因为我不想出货borlandmm DLL).所以用D7编写的DLL,由Delphi XE2调用

我的界面需要

在我的DLL中:

function d7zipFile(pFatFile,pThinFile : PChar) : integer; stdCall;
function d7unzipfile(pThinFile,pFatFile : PChar) : integer; stdCall;
Run Code Online (Sandbox Code Playgroud)

我需要在unzipfile函数中传递BACK pFatFile名称.

在我的调用代码中:

function d7zipFile(pFatFile,pThinFile : PChar) : integer; external 'd7b64zip.dll';

function d7unzipfile(pThinFile,pFatFile : PChar) : integer; external 'd7b64zip.dll';
Run Code Online (Sandbox Code Playgroud)

有人可以帮助您实施这些最好的方法吗?

显然我不是在寻找实际的zip/unzip代码 - 我在D7中工作得很好.我想知道如何声明和使用字符串/ pchar参数,因为我尝试的各种类型(PWideChar,WideString,ShortString等)都给出了错误.

因此,我很乐意能够在d7zipFile函数中为两个文件名执行showMessage.然后能够在pFatFile变量上的delphiXE2中执行showMessage,这意味着字符串双向都可以吗?

Dav*_*nan 6

到目前为止,最简单的方法是使用WideString.这是围绕COM BSTR类型的Delphi包装器.使用共享COM分配器完成字符串有效负载的动态分配.由于Delphi RTL管理它,它对您来说是透明的.

在Delphi 7代码中,您声明您的函数如下:

function d7zipFile(const FatFile, ThinFile: WideString): integer; stdcall;
function d7unzipfile(const ThinFile: WideString; var FatFile: WideString): 
    integer; stdcall;
Run Code Online (Sandbox Code Playgroud)

在您的调用代码中,您声明如下函数:

function d7zipFile(const FatFile, ThinFile: WideString): integer; stdcall; 
    external 'd7b64zip.dll';
function d7unzipfile(const ThinFile: WideString; var FatFile: WideString): 
    integer; stdcall; external 'd7b64zip.dll';
Run Code Online (Sandbox Code Playgroud)

这种方法的替代方案是使用PAnsiCharPWideChar.请注意,您无法使用,PChar因为该别名引用了不同的类型,具体取决于您使用的Delphi版本.在Delphi 7中PChar是别名PAnsiChar,在XE2中它是别名PWideChar.

PAnsiChar比方说,使用的一大缺点是调用者需要分配从DLL返回的字符串.但通常调用者不知道该字符串需要多大.该问题有多种解决方案,但最新的方法始终是使用共享分配器.您声明您不想依赖borlandmm.dll,因此下一个最明显的公共分配器是COM分配器.这WideString就是吸引人的原因.

  • @JensMühlenhoff主要是肯定的.它只是一个COM BSTR.但是如果你试图使用`WideString`作为函数返回值,则会有一个陷阱,因为Delphi的返回值语义与许多其他常见的Windows语言实现(如C,C++,C#等)使用的ABI不兼容.请参阅http ://stackoverflow.com/questions/9349530/why-can-a-widestring-not-be-used-as-a-function-return-value-for-interop (2认同)