Solved

Overloading a Delphi4 DLL Function

Posted on 2000-05-10
4
240 Views
Last Modified: 2010-04-04
I have a Delphi 4 application which calls a function in a DLL also written in Delphi 4. I need to overload the DLL function but have not been able to find a way to do this. The normal method for overloading a function inside of a Delphi project does not appear to work. Anyone out there know how to do this ?
0
Comment
Question by:mferdock
  • 3
4 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 2798008
Did you try this?

  procedure DoSomething(x: Integer); overload; external 'MYLIB.DLL' name 'function1';
  procedure DoSomething(x: Double); overload; external 'MYLIB.DLL' name 'function2';


Regards,

Epsylon.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2798040
I just tried this and it works!

----------------------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  procedure DoSomething(x: Integer); overload; external 'MYDLL.DLL' name 'procedure1';
  procedure DoSomething(x: Double); overload; external 'MYDLL.DLL' name 'procedure2';

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  DoSomething(1);
  DoSomething(1.0);
end;

end.

----------------------------------------

library mydll;

uses
  SysUtils, Classes, Dialogs;

{$R *.RES}


procedure procedure1(x: Integer);
begin
  ShowMessage('procedure1');
end;

procedure procedure2(x: Double);
begin
  ShowMessage('procedure2');
end;

exports procedure1, procedure2;

begin
end.
0
 

Author Comment

by:mferdock
ID: 2800700
Your example does not employ "true" function overloading since you declare two procedures in your DLL export clause which are identified by different names. I was always under the impression that for true function overloading you could use the same function name and the correct version of the function would be called based on the parameter list. I know this works if the overloaded function is part of the same project. Maybe it does not work this way when the overloaded function is part of a DLL. Any suggestions ?
Thanks....
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 200 total points
ID: 2801577
Overloading is a feature that is handled by the compiler. That's why it is not possible to use overloading by declaring a single external procedure. Because we want the overloading to occur at runtime here, the DLL has to recognize the parameter types, to decide which procedure has to be called. This is not possible.

Below a variant of my example.

----------------------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  procedure DoSomething(x: Integer); overload; external 'MYDLL.DLL' name 'DS_Integer';
  procedure DoSomething(x: Double); overload; external 'MYDLL.DLL' name 'DS_Double';

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  DoSomething(1);
  DoSomething(1.0);
end;

end.

----------------------------------------

library mydll;

uses
  SysUtils, Classes, Dialogs;

{$R *.RES}


procedure DoSomething(x: Integer); overload;
begin
  ShowMessage('Integer');
end;

procedure DoSomething(x: Double); overload;
begin
  ShowMessage('Double');
end;

exports
  DoSomething(x: Integer) name 'DS_Integer',
  DoSomething(x: Double) name 'DS_Double';

begin
end.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question