【发布时间】:2014-06-11 06:24:58
【问题描述】:
对于我们的 Delphi (XE5) 应用程序,我们正在开发一个 API。为了将来自 Delphi DLL 函数的数据与主程序(基于 C;(控制台)C 或 C++ 代码应用程序或 Matlab 和 Simulink)进行通信,调用者分配的数组需要由 DLL 填充双精度值。
我知道开放数组(Delphi 特有的)对于这个目的不是很方便,因为它们包含额外的数据,然后你必须在 C 中模仿这些数据。相反,我打算使用指针算法(参见 dll 函数:inc(APDouble))通过直接指向正确的地址。我的问题是,如果您的软件开发人员会这样做。
下面包含一个演示(完整来源)。
DLL(在 DXE5 中制作):
library PDA;
uses
System.StrUtils,
System.SysUtils,
Vcl.Dialogs;
{$R *.res}
function ShowArrayContents( APDouble: PDouble;
size: Integer): integer; cdecl; export;
var
i: Integer;
begin
Result := 0;
for i := 0 to size-1 do
begin
// Show value!
MessageDlg(Format('%p -> %p -> %f', [@APDouble, APDouble, APDouble^]), mtWarning, [mbOK], 0);
Inc(APDouble);
end;
end;
exports
ShowArrayContents;
begin
end.
C 代码调用者(在 C++ builder XE4 中制作):
#include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <windows.h>
typedef int (*_ShowArrayContents) (double *, int);
char *dllname = "PDA.dll";
static HINSTANCE hInstanceControl;
_ShowArrayContents ShowArrayContents = NULL;
int _tmain(int argc, _TCHAR* argv[])
{
double DVals[3] = {1.23, 4.56, 7.89};
int i;
hInstanceControl = LoadLibrary(dllname);
if( hInstanceControl != NULL){
ShowArrayContents =(_ShowArrayContents)GetProcAddress(hInstanceControl, "ShowArrayContents");
} else {
return 0;
}
// test program:
(*ShowArrayContents)(&DVals[0], 3);
FreeLibrary(hInstanceControl);
system("pause");
return 0;
}
【问题讨论】: