【问题标题】:How should I call this particular dll function in Delphi 6我应该如何在 Delphi 6 中调用这个特定的 dll 函数
【发布时间】:2013-07-10 20:33:31
【问题描述】:

我对从 DLL 调用函数完全陌生(称之为不良编程习惯,但我从来不需要这样做)。

我有这个 C++ dll(https://skydrive.live.com/redir?resid=4FA1892BF2106B62!1066 上的 CidGen32.dll)应该导出具有以下签名的函数:

extern "C" __declspec(dllexport) int GetCid(const char* pid, char* cid); 

它应该做的是获取一个 13 字符的字符串,例如 '1111111111118' 并返回一个 20 字符的哈希。

过去几天我尝试在 Delphi 6 中调用此函数,但无济于事。我拼命尝试了 50 多种组合,有一次我非常接近,但我的电脑死机了,我失去了所有的努力。由于它是基于运气,我不能再重做。

我也不打算注册 DLL,而是将它放在同一个文件夹中。

无论如何,计划是这样的:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);

  function GenerateCID(Prm: string): string;
  var
    aCID: PAnsiChar;
    uCID: AnsiString;
    i: integer;
    Hbar: Thandle;
    GetCID: function (X: PAnsiChar; Y: PAnsiChar): integer; {$IFDEF WIN32} stdcall; {$ENDIF}
  begin
    ucid := '';
    hbar := LoadLibrary('CidGen32.dll');
    if Hbar >= 32 then
    begin
      @GetCID := GetProcAddress(HBar, 'GetCID');
      if Assigned(GetCID) then
      begin
        i := GetCID(pAnsiChar(prm), aCID);
        uCID := aCID;
      end;
      FreeLibrary(HBar);
    end
    else
    begin
      //ShowMessage('Error: could not find dll');
    end;
    result := uCID;
  end;

begin
  ShowMessage(GenerateCID('1111111111118'));
end;

end.

但看来我大错特错了。

【问题讨论】:

标签: delphi dll loadlibrary


【解决方案1】:

您使用了错误的名称来导入函数。它的名称是GetCid,但您正在尝试导入GetCID。当您致电GetProcAddress 时,字母大小写很重要。如果这仍然没有导致 GetProcAddress 调用成功,请使用 Dependency Walker 等工具仔细检查导出函数的名称。

函数是 cdecl 所以你应该这样声明它:

GetCID: function(pid, cid: PAnsiChar): Integer; cdecl;

另一个问题是你负责分配 cid 后面的缓冲区。你没有那样做。这样做:

SetLength(uCID, 20);
i := GetCID(pAnsiChar(prm), pAnsiChar(uCID));

并删除 aCID 变量。并且 >32 错误检查是错误的,与 0 比较。

【讨论】:

  • 嗨,大卫,感谢您的意见。进行了您建议的更改,但结果相同(未分配该功能)。莫非是我的命名不对?
  • 好的,我也解决了这个问题,名字错了。这就是为什么你需要告诉我们某事是如何失败的。你没有那样做。你刚刚告诉我们它不起作用。
  • 您,先生,是最伟大的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-15
  • 2017-12-14
相关资源
最近更新 更多