Wednesday, March 23, 2011

Is it possible to have unit names added to each call to a function in a different unit?

One of the problems I often run into, is that I will include 'Windows' in my uses clause, and then I will later add 'JwaWinBase' for some specific calls.

However, many of the functions in the 'Windows' unit are the same as in JwaWinBase, and I start getting errors in my main unit, all over the place, until I fix all my calls by pre-pending the correct unit name, like this:

Old:

  • CreateProcessAsUser(...)

New:

  • Windows.CreateProcessAsUser(...)
  • JwaWinBase.CreateProcessAsUser(...)

What I want to know, is if there is a way to have the unit name automatically pre-pended to every call to a function in another unit? This way, before I added JwaWinBase to my uses clause, I could have the 'Windows' unit name pre-pended to any function calls. Then adding JwaWinBase wouldn't give me any errors.

I'm currently using Delphi 2007.

From stackoverflow
  • Maybe it's enough to just switch the order of the two units in the uses clause.

  • There isn't.

    However, the function calls are handled in the reverse order they're in the uses clause, so that if you have this:

    uses
      Windows, JwaWinBase;
    

    ... it will call Jwa functions by default. However, if you reverse them:

    uses
      JwaWinBase, Windows;
    

    ... it should call the Windows functions by default, and you can preface your Jwa functions as required.

    Mick : I did not know that. Thanks for the info!
  • If you really need the resolution on a per-routine base, you could try inline forwarders:

    procedure Blah; inline;
    begin
      Windows.Blah;
    end;
    
    procedure Blubb; inline;
    begin
      JwaWinBase.Blubb;
    end;
    
    // later:
    procedure UseThem;
    begin
      Blah; // calls Windows.Blah
      Blubb; // calls JwaWinBase.Blubb
    end;
    

    at the beginning of the implementation section (completely untested :-)).

  • If you use an editor like CodeRush (D7 and before) or Castalia or even the template feature of D2009, you can create templates that expand to what you want AS you enter them. Then you can keep the Windows, jwaWinBase order in the uses section. You can set jcpau to expand to jwaWinBase.CreateProcessAsUser while cpau expands to CreateProcessAsUser or to Windows.CreateProcessAsUser depending on your preference. You'd only have to go through the various functions in jwaWinBase and make templates for them to be safe.

    Otherwise, I think it's search and replace on a case-by-case basis.

0 comments:

Post a Comment