Solved

Overloading a Delphi4 DLL Function

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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