在Delphi 6中如何调用这个特定的dll函数 [英] How should I call this particular dll function in Delphi 6

查看:241
本文介绍了在Delphi 6中如何调用这个特定的dll函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常喜欢从DLL调用函数(称为坏的编程习惯,但我从来不需要)。



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



int code externC__declspec(dllexport)int GetCid(const char * pid,char * cid);

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



我在过去几天尝试过在Delphi 6中调用此函数,但无效。我拼命地尝试了我的50+组合,我有一个相当接近,但我的电脑冻结,我失去了我所有的努力。由于它是基于运气,我不能重做它了。



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



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

 单位Unit1; 

接口

使用
Windows,消息,SysUtils,变体,类,图形,控件,表单,
对话框;

type
TForm1 = class(TForm)
procedure FormCreate(Sender:TObject);
private
{私人声明}
public
{公开声明}
end;

var
Form1:TForm1;

实现

{$ R * .dfm}

程序TForm1.FormCreate(发件人:TObject);

函数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');
如果Hbar> = 32 then
begin
@GetCID:= GetProcAddress(HBar,'GetCID');
如果分配(GetCID)然后
开始
i:= GetCID(pAnsiChar(prm),aCID);
uCID:= aCID;
结束
FreeLibrary(HBar);
end
else
begin
// ShowMessage('错误:找不到dll');
结束
result:= uCID;
结束

begin
ShowMessage(GenerateCID('1111111111118'));
结束

结束。

但似乎我错了。

解决方案

您使用错误的名称导入功能。它的名称是 GetCid ,但您尝试导入 GetCID 。当您致电 GetProcAddress 时,信件重要。如果仍然不会导致 GetProcAddress 调用成功,请使用Dependency Walker等工具重新检查导出该函数的名称。



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

  GetCID:function(pid, cid:PAnsiChar):整数; CDECL; 

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

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

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


I am absolutely new at calling functions from DLLs (call it bad programming habits, but I never needed to).

I have this C++ dll (CidGen32.dll at https://skydrive.live.com/redir?resid=4FA1892BF2106B62!1066) that is supposed to export a function with the following signature:

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

What it should do is to get a 13 char string such as '1111111111118' and return a 20 char hash.

I have tried for the last couple of days to call this function in Delphi 6 but to no avail. I have desperately tried I guess 50+ combinations and I got quite close on one occasion but my computer froze and I lost all my effort. Since it was based on luck, I could not redo it anymore.

I am also aiming not to register the DLL, but rather place it in the same folder.

Anyway, the plan was to have something like this:

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.

But it seems I am dead wrong.

解决方案

You are using the wrong name to import the function. Its name is GetCid but you are trying to import GetCID. Letter case matters when you call GetProcAddress. If that still doesn't result in the GetProcAddress call succeeding, double check the name with which the function is exported using a tool like Dependency Walker.

The function is cdecl so you should declare it like this:

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

And the other problem is that you are responsible for allocating the buffer behind cid. You did not do that. Do it like this:

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

And delete the aCID variable. And that >32 error check is wrong, compare against 0.

这篇关于在Delphi 6中如何调用这个特定的dll函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆