【发布时间】:2012-01-24 20:56:00
【问题描述】:
德尔福 Xe。
在模块 Windows.pas 中,我看到了一种方法:
function InterlockedExchangeAdd(Addend: PLongint; Value: Longint): Longint stdcall; overload;
{$EXTERNALSYM InterlockedExchangeAdd}
function InterlockedExchangeAdd(var Addend: Longint; Value: Longint): Longint stdcall; overload;
{$EXTERNALSYM InterlockedExchangeAdd}
...
function InterlockedExchangeAdd(Addend: PLongint; Value: Longint): Longint; external kernel32 name 'InterlockedExchangeAdd';
function InterlockedExchangeAdd(var Addend: Longint; Value: Longint): Longint; external kernel32 name 'InterlockedExchangeAdd';
意味着,DLL 可以导出具有相同名称的函数。
我试着重复:
我创建项目
Program TestMyDll;
{$APPTYPE CONSOLE}
uses SimpleShareMem, SysUtils;
Function MyFunc(const X:Integer):string; StdCall; External 'MyDll.dll' Name 'MyFunc'; Overload;
Function MyFunc(const X:Extended):string; StdCall; External 'MyDll.dll' Name 'MyFunc'; Overload;
begin
try
Writeln;
Writeln('MyDll test');
Writeln('Int: ' + MyFunc(10));
Writeln('Real: ' + MyFunc(10.55));
Readln;
except on E: Exception do Writeln(E.ClassName, ' : ', E.Message);end;
end.
正常编译。此外,我创建 DLL:
Library MyDll;
uses
SimpleShareMem,
DllUnit1 in 'DllUnit1.pas';
{$R *.res}
begin
//test
MyFunc(10);MyFunc(10.55);
end.
...和模块 DllUnit1.pas
Unit DllUnit1; Interface
Function MyFunc(const X:Integer):string; Overload; StdCall;
Function MyFunc(const X: Extended):string; Overload; StdCall;
Exports
MyFunc; // COMPILE ERROR
Implementation
Uses SysUtils;
Function MyFunc(const X:Integer):string;
begin
result:=Inttostr(x);
end;
Function MyFunc(const X: Extended):string;
begin
result:=Floattostr(x);
end;
end.
但在编译时我收到一个错误:[DCC Error] DllUnit1.pas(7): E2273 不存在具有此参数列表的“MyFunc”的重载版本。
在 Delphi 帮助中,我看到:
"Delphi Language Reference"/"The exports clause"
...
When you export an overloaded function or procedure from a dynamically loadable library, you must specify its parameter list in the exports clause. For example,
exports
Divide(X, Y: Integer) name 'Divide_Ints',
Divide(X, Y: Real) name 'Divide_Reals';
On Windows, do not include index specifiers in entries for overloaded routines.
问题:
如何在模块 DllUnit1 中正确导出这些函数,以及是否有可能在 Delphi 中(以一个名称导出)以从我的项目 TestMyDll 接收与开始时相同的调用(示例来自 windows.pas)?
如果这样的函数可以在一个名称下导出,那么从其他语言(VB、C++)调用DLL是否正确?还是做两个不同名字的函数比较好?
附:在这里发现了一点类似的问题(http://stackoverflow.com/questions/6257013/how-to-combine-overload-and-stdcall-in-delphi),但答案不适合我
附言英语不好
添加(已在答案后添加)
很清楚,谢谢。
已经这样做了:
在项目中:
Function MyFunc (const X:Integer):string; StdCall; External 'MyDll.dll' Name 'MyFunc'; Overload;
Function MyFunc (const X:Extended):string; StdCall; External 'MyDll.dll' Name ' MyFunc1'; Overload;
在 DllUnit1 中
Exports
MyFunc (const X:Integer) Name 'MyFunc',
MyFunc (const X:Extended) Name 'MyFunc1';
编译正常,运行正常。
还有问题:
喜欢作品,但是否正确?
是否有值怎么写“Function MyFunc (const X:Integer):string; Overload; StdCall;”或“函数 MyFunc (const X:Integer):string; StdCall; Overload;”?
其他语言(Vb、C++、C#)项目中的这个函数会正确导致吗?
【问题讨论】:
标签: delphi dll overloading