【发布时间】:2012-09-28 22:57:55
【问题描述】:
我一直在尝试基于documentation 在 delphi 中重用 C dll 文件。
服务器运行良好,我可以访问和使用本地服务器上的数据库 java和php。
在 delphi 上,我使用了动态加载,并且在所有返回变量的函数上运行良好,但在返回接口的函数上失败了。
unit for library :
unit SQLDBC_C;
interface
uses windows, classes, sysutils;
type
SQLDBC_IRuntime = interface
end;
var
getSDKVersion : function :Pchar; stdcall;
ClientRuntime_GetClientRuntime: function (errorText:Pchar; errorTextSize:Integer) : SQLDBC_IRuntime; stdcall;
implementation
var
libhandle : THandle;
procedure initLibrary;
begin
libhandle := LoadLibrary('libSQLDBC_C.dll');
if libhandle>=23 then begin
@getSDKVersion:=GetProcAddress(libhandle,'getSDKVersion');
@ClientRuntime_GetClientRuntime:=
GetProcAddress(libhandle,'ClientRuntime_GetClientRuntime');
end;
end;
initialization
begin
initLibrary;
end;
finalization
begin
if libhandle>=32 then
FreeLibrary(libhandle);
end;
end.
这是测试程序:
procedure TForm1.Button1Click(Sender: TObject);
var
err : array [0..200] of char;
rt : SQLDBC_IRuntime;
begin
Memo1.Clear;
FillChar(err, sizeof(err), 0);
Memo1.Lines.Add(getSDKVersion); //this function successed
rt := ClientRuntime_GetClientRuntime(@err,200);
//this function had no return value, (rt always nil) but no error return at err variable
if assigned(rt) then begin
......
end;
end;
我已经阅读了geskill、Dan Hacker、max 和Ron 提出的类似问题,但它无法解决我的问题。
谁能告诉我这里出了什么问题?
【问题讨论】:
-
Delphi 的哪个版本?你确定它是stdcall吗?很可能是 cdecl。
-
这让我想起了这个问题:stackoverflow.com/questions/9349530/…
-
@DavidHeffernan 感谢您的回复。 stdcall 和 cdecl 之间没有区别。我在 Windows XP 机器上,正在尝试 Delphi 6、7 和 XE2。
-
确保您将 AnsiChar 用于 XE2。最好始终将它用于此库。尝试一个 C++ 程序并检查它是否有效。如果有,有什么不同?
-
这就是问题所在。它适用于 C++、java 和 php。在 delphi 6、7 和 XE2 上,库成功加载了返回字符串和数字的函数,除了返回接口的所有函数。如果我使用接口它返回零。如果我使用指针,它会返回指针但会引发异常。