我使用 Delphi 编写了一个包含一堆不同过程和函数的 DLL。我想重载此 DLL 中的一个函数,但一旦重载,所有导出语句都会出现一系列错误:
E2191 仅在全球范围内允许出口
我尝试重载的函数不属于任何类。
以下是函数声明和代码:
function qbRotateLeft(value: byte; shift:byte; RotateBits: byte) : byte; overload;
function qbRotateLeft(value: word; shift:byte; RotateBits: byte) : word; overload;
function qbRotateLeft(value: dword; shift:byte; RotateBits: byte) : dword; overload;
function qbRotateLeft(value: qword; shift:byte; RotateBits: byte) : qword; overload;
var
temp1 : variant; // declare local variable temp1 as qword (64-bit)
begin
// make sure the shift value > 0 and <= RotateBits
if (shift>0) or (shift<=(RotateBits-1)) then
begin
// temp1 = shift and RotateBits-1
temp1:=(shift and RotateBits-1);
// if temp1 is zero, then there is nothing to do
// return the original value
if temp1 = 0 then qbRotateLeft:=value;
// otherswise return
// (value shifted left shift) or'd (value shifted right by
// (RotateBits-shift))
qbRotateLeft:=(value shl shift) or (value shr (RotateBits-shift));
end
else
// shift value <= 0 or >= RotateBits, return zero
qbRotateLeft:=0;
end;
exports
语句位于文件末尾之前的全局空间内,但每个语句都有如上所述的错误(我只显示重载的导出):
exports qbRotateLeft(value: byte; shift:byte; RotateBits: byte); overload;
exports qbRotateLeft(value: word; shift:byte; RotateBits: byte); overload;
exports qbRotateLeft(value: dword; shift:byte; RotateBits: byte); overload;
exports qbRotateLeft(value: qword; shift:byte; RotateBits: byte); overload;
exports qbDLLVersion;
begin
end.
在添加重载函数语句之前,一切都工作正常并编译得很好。事实上,如果您注释掉第一个函数语句下面的 (3) 个附加函数语句,并从第一个函数语句中删除单词
overload
,一切都会恢复正常,并且库编译时不会出现错误。
我已经阅读了有关重载函数的文档,有些人说我所做的是有效的,但其他来源说它应该在类中定义。很可能我没有正确使用重载功能。
重载函数是Delphi编译器的特性,因此不能指望它是可导出的。想要使用 DLL 的其他语言的代码将无法理解它。合理的解决方案是为这四个函数中的每一个指定一个单独的名称,并跳过重载。