2

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?

1
  • 1
    careful with calling convention Commented Apr 24, 2013 at 22:44

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yay! Thank you. It had been driving me nuts. (The showmessage was only there to try to debug what was happening with the integer - my actual dll doesn't show anything).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.