Delphi DLL问题 - 接收到错误的整数值和访问冲突问题

时间:2013-04-24 22:41:38

标签: windows delphi dll

我无法在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.

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

1 个答案:

答案 0 :(得分:4)

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

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

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

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