You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Delphi中重载DLL导出函数?解决E2191编译错误

Delphi DLL全局函数重载导出问题解决方法

问题根源分析

你遇到的E2191错误,本质是导出重载函数的语法错误,同时原代码还存在重载函数实现不规范的问题:

  1. exports语句中不能写参数列表,这种错误语法导致编译器误判代码不在全局作用域;
  2. Delphi的全局函数重载支持,但每个重载声明必须对应独立的实现,不能共用同一个begin-end块;
  3. 原代码存在逻辑错误(条件判断用or而非and、运算符优先级问题),且用Variant类型完全没必要。

正确实现代码示例

1. 函数声明与实现

unit qbRotateUnit;

interface

// 重载函数声明
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;

function qbDLLVersion: Integer;

implementation

// Byte版本实现
function qbRotateLeft(value: Byte; shift: Byte; RotateBits: Byte): Byte;
var
  temp1: Byte;
begin
  if (shift > 0) and (shift <= RotateBits - 1) then
  begin
    temp1 := shift and (RotateBits - 1); // 修正运算符优先级问题
    if temp1 = 0 then
      Result := value
    else
      Result := (value shl temp1) or (value shr (RotateBits - temp1));
  end
  else
    Result := 0;
end;

// Word版本实现
function qbRotateLeft(value: Word; shift: Byte; RotateBits: Byte): Word;
var
  temp1: Word;
begin
  if (shift > 0) and (shift <= RotateBits - 1) then
  begin
    temp1 := shift and (RotateBits - 1);
    if temp1 = 0 then
      Result := value
    else
      Result := (value shl temp1) or (value shr (RotateBits - temp1));
  end
  else
    Result := 0;
end;

// DWORD版本实现
function qbRotateLeft(value: DWORD; shift: Byte; RotateBits: Byte): DWORD;
var
  temp1: DWORD;
begin
  if (shift > 0) and (shift <= RotateBits - 1) then
  begin
    temp1 := shift and (RotateBits - 1);
    if temp1 = 0 then
      Result := value
    else
      Result := (value shl temp1) or (value shr (RotateBits - temp1));
  end
  else
    Result := 0;
end;

// QWord版本实现
function qbRotateLeft(value: QWord; shift: Byte; RotateBits: Byte): QWord;
var
  temp1: QWord;
begin
  if (shift > 0) and (shift <= RotateBits - 1) then
  begin
    temp1 := shift and (RotateBits - 1);
    if temp1 = 0 then
      Result := value
    else
      Result := (value shl temp1) or (value shr (RotateBits - temp1));
  end
  else
    Result := 0;
end;

// 其他函数实现
function qbDLLVersion: Integer;
begin
  Result := 1; // 示例版本号
end;

2. 正确的导出语句

DLL导出表不支持同名函数,必须通过name关键字给每个重载指定唯一导出名:

exports
  qbRotateLeft name 'qbRotateLeftByte',
  qbRotateLeft name 'qbRotateLeftWord',
  qbRotateLeft name 'qbRotateLeftDWord',
  qbRotateLeft name 'qbRotateLeftQWord',
  qbDLLVersion;

begin
end.

也可以用序号区分(调用方需知晓序号):

exports
  qbRotateLeft index 1,
  qbRotateLeft index 2,
  qbRotateLeft index 3,
  qbRotateLeft index 4,
  qbDLLVersion;

关键注意事项

  • 重载函数必须独立实现:每个重载声明对应单独的begin-end块,可将公共逻辑抽成私有辅助函数复用,但不能共用实现;
  • 导出语法规范:exports语句仅写函数名,附加name/index等关键字区分重载,禁止写参数列表;
  • 逻辑修正:原代码中(shift>0) or (shift<=(RotateBits-1))逻辑错误,应改为and;同时注意运算符优先级,给RotateBits-1加括号。

内容的提问来源于stack exchange,提问作者Skypilot65

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 06:14:56