Roise
Roise

Reputation: 364

How to calling DLL procedure with parameters?

I have problems sending using a Dll procedure with parameters, im not allowed to add parameters to the call of the dll method in my test project.

Im trying to call this dll method:

procedure Transfer(sMessage: PChar); stdcall;
begin
    MainForm.ShowThis(sMessage);
end;

exports
Transfer;

TestProj using this:

procedure TForm1.Button1Click(Sender: TObject);
var
DLLHandle : THandle;
begin
    DLLHandle := LoadLibrary ('C:\Program Files\Borland\Delphi5\Projects\Dll\MyLink.dll');
    if DLLHandle >= 32 then
        try
              @Trans := GetProcAddress (DLLHandle, 'Transfer');
              if @Trans <> nil then
                  Trans  //Would like to say something like: Trans('Hello')
              else
                Showmessage('Could not load method address');

        finally
            FreeLibrary(DLLHandle);
    end
    else
        Showmessage('Could not load the dll');
end;

The compile error i get if i use the "Trans('Hello')" is : [Error] Unit1.pas(51): Too many actual parameters.

Im allowed to run it without the parameters but then i only get jiberish in my showmessage box and a crash after, since i dont send any message.

So the question is how do i get to send a string as a parameter into the dll ? what am i doing wrong ?

Upvotes: 1

Views: 3505

Answers (1)

Karel Petranek
Karel Petranek

Reputation: 15164

You should not use the pointer sign (@) at the left side of the assignment, the Trans variable should look like this:

type
  TTransferPtr = procedure (sMessage: PChar); stdcall;

var
  Trans: TTransferPtr;

// Then use it like this:
Trans := TTransferPtr(GetProcAddress (DLLHandle, 'Transfer'));
if @Trans <> nil then
  Trans(PChar('Hello'));

Upvotes: 1

Related Questions