user2125574
user2125574

Reputation: 85

Delphi DLL Issues - wrong integer value recevied and access violation issue

I'm having trouble getting to grips with DLLs in Delphi 7. I have two problems:

1) The procedure takes an integer parameter - but the dll receives a different value to the one I pass.

2) The application that called the dll crashes with an access violation after the function completes.

Here's my dll code:

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.

So when I call this with the code below I get:

'Sending - 1'

'You Sent - 1636532'

'Finished Now'

Then the calling application crashes with an access violation.

The calling application looks like this:

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.

Any ideas what I'm doing wrong here?

Upvotes: 2

Views: 670

Answers (1)

Ken White
Ken White

Reputation: 125757

You need stdcall both in the DLL and in the calling code declaration. You only have it in the DLL.

Calling conventions need to match on both sides. :-)

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

You should use the standard Windows MessageBox instead of ShowMessage, so that the DLL can be used from non-Delphi applications as well.

Upvotes: 4

Related Questions