tags:

views:

90

answers:

1

Hi, 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 ?

+1  A: 

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'));
dark_charlie
Thanks it allmost worked the way you sayd. First of Transfer is a procedure so it should not return a string so : TTransferPtr = procedure (sMessage: PChar); stdcall. And for if Trans <> nil to work it needs to be a pointer address: if @Trans <> nil. But other then that its right. Thanks again
Roise
Ah, sorry for the mistakes, I should've first tested it before posting.
dark_charlie
If the DLL is written in pre-D2009, then the app should use `PAnsiChar` instead of `PChar` to ensure compatibility if the app is written/upgraded to D2009+.
Remy Lebeau - TeamB