Solved

Overloading a Delphi4 DLL Function

Posted on 2000-05-10
4
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month4 days, 8 hours left to enroll

635 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