Solved

Overloading a Delphi4 DLL Function

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

734 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