Solved

Overloading a Delphi4 DLL Function

Posted on 2000-05-10
4
233 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now