Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access delphi function at DPR scope

Tags:

delphi

I have a problem with maintenance on an old Delphi program (D7). A lot of the program logic is in the DPR file (this is not a windowed program), with some units providing things like access to the database. We need to get some debug out of the DB unit, but the debug functionality is in the DPR. We can't easily strip the debug functionality out, because it uses stuff that's unique to the DPR, like its main pipe. Separating it out would be like trying to tease apart spaghetti and meatball sauce.

So how do we call a function that's declared at the DPR scope from a subordinate used unit? What's the equivalent of the :: operator in C++ ?

Please don't tell me to redesign the app. I'd love to, but we won't be given the necessary time. Plus if we redesigned this puppy, it wouldn't be in Delphi.

like image 875
Bob Moore Avatar asked Jul 27 '09 10:07

Bob Moore


2 Answers

You can declare a method variable in the unit that matches the signature of the function in the DPR. At the very beginning of the program you set the method variable to the function. Inside the unit you call the method variable.

Example:

(DPR)

uses
  Unit1;

function DoSomething(Par: Integer): Integer;
begin
...
end;

...
begin
  DoSomethingVar := DoSomething;
  ...
end;

(unit)

unit Unit1;

interface
...
var
  DoSomethingVar: function(Par1: Integer): Integer;
...
implementation
...
  SomeResult := DoSomethingVar(SomeParameter);
...
like image 130
Uwe Raabe Avatar answered Oct 13 '22 22:10

Uwe Raabe


You can't. The unit hierarchy is rigid.

There are two possible options:

  • pull out the relevant parts of the .dpr to a new unit. Keep in mind that moving uses to the implementation can break import cycles. The createform* stuff probably isn't safe to move, that would probably upset the project manager.
  • or define a few callback functions (function,method types, like functionpointer in C), and move code out of the relevant unit initialization to a procedure that you call from the .dpr if necessary.
like image 28
Marco van de Voort Avatar answered Oct 13 '22 21:10

Marco van de Voort