Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Override a Function/Procedure

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,311
0
36
US
Is there a way to replace or override a function or procedure that is in a commonly used unit within my own unit?

Say, the main program uses unit1 (which I do not have the source to) and unit2 (which I do have the source to). The main program calls function1 and function2. Is there a way within unit2 to force the call of a function1 within it when it is included in the main program along with unit1?

Changing the function call within the main program to a different name is not an option in this case.

My initial thought is maybe to reset procedure pointers (set the procedure pointer in unit1 to the function address in unit2). That's how it works in dynamic addressing, but I'm not sure how it would translate to static functions.

Any ideas?

----------
Measurement is not management.
 
you can always call unitx.functiony or am I missing the point?

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Normally you would simply change the order in which unit1 and unit2 appear in the main project's uses clause. If both unit1 and unit2 contain the same function with the same parameters, then without specifying the unit as per whosrdaddy's suggestion, then the main project will always call which unit is declared first (or last, can't remember).
 
Okay, what I was trying to do is replace some functions in some of the standard libraries. The problem is that I don't want to physically replace the libraries to be able to make the code change. I just want the replacement function to be called if the unit is specified instead of the standard library, like System.

----------
Measurement is not management.
 
just like I said, call newunit.overridefunction to make sure you are calling your function.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
I've done something similar by copying .pas files (temporarily) from ..\Lib\ and/or ..\Source\Rtl\ to my local project folder to expose the source in the debugger.

(Using System.pas literally is not a good example since it requires a special build - see source for details.)

You can then copy functions from the library to your local unit to make your own custom versions. Once you have the local unit working, delete or hide the local copies of the copied library source files.

If all is correct, the library calls will now be from your unit instead.

I hope that makes sense, it's early.

Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top