【问题标题】:Delphi DLL Issues - wrong integer value recevied and access violation issueDelphi DLL 问题 - 收到错误的整数值和访问冲突问题
【发布时间】:2013-04-24 22:41:38
【问题描述】:

我在处理 Delphi 7 中的 DLL 时遇到了麻烦。我有两个问题:

1) 该过程采用整数参数 - 但 dll 接收的值与我传递的不同。

2) 调用该 dll 的应用程序在函数完成后因访问冲突而崩溃。

这是我的 dll 代码:

library apmDLL;

uses
  Classes, Messages, Windows, Dialogs, sysutils ;

const

  WM_MY_MESSAGE = WM_USER + 1;

procedure sendtoACRPM (functionKey : integer); stdcall;
  begin
    showmessage('You sent -  '+inttostr(functionKey));
    showmessage('Finished Now');
  end;

exports sendtoACRPM;

end.

所以当我用下面的代码调用它时,我得到:

'发送 - 1'

'您已发送 - 1636532'

'已完成'

然后调用应用程序因访问冲突而崩溃。

调用应用程序如下所示:

unit Unit1;

interface

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

const

  WM_MY_MESSAGE = WM_USER + 1;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button2: TButton;
    procedure Button2Click(Sender: TObject);



  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  procedure sendtoACRPM (functionKey : integer) ; external 'apmDLL.dll';

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
var
  myInt: integer;
begin
  myInt := strtoint(edit1.text);
  showmessage('Sending - ' + inttostr(myInt));
  sendtoACRPM(myInt);
end;

end.

有什么想法我在这里做错了吗?

【问题讨论】:

  • 小心调用约定

标签: windows delphi dll


【解决方案1】:

在 DLL 和调用代码声明中都需要 stdcall。您只在 DLL 中拥有它。

调用约定需要双方匹配。 :-)

procedure sendtoACRPM (functionKey : integer); stdcall; external 'apmDLL.dll';

您应该使用标准的 Windows MessageBox 而不是 ShowMessage,这样 DLL 也可以在非 Delphi 应用程序中使用。

【讨论】:

  • 耶!谢谢你。它一直让我发疯。 (显示消息只是为了尝试调试整数发生的事情 - 我的实际 dll 没有显示任何内容)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-26
  • 1970-01-01
  • 2015-09-14
  • 1970-01-01
  • 2011-05-28
  • 2010-10-09
相关资源
最近更新 更多