Search code examples
delphivcl

Delphi access private function in VCL unit


How do I access private function in a VCL unit?

I need to call the function ColorToPrettyName from ExtCtrls.pas.

Now I copied this to my source but IMO nicer would be to use that function instead.

Some more details: The function is used in TColorBox, but I only need the pretty name. I tried to instantiate TColorBox and get the pretty name from it, but this is only possible when I have some TWinControl to assign to its Parent. But that TWinControl I don't have in the place where I want to use the pretty name and I don't want to do any hacks.


Solution

  • You cannot readily call this function from outside the unit since it is not exposed.

    You could create an instance of the control that calls the function, and persuade it to do the dirty work. That's perfectly feasible as another another answer demonstrates.

    You could use a disassembler to find the address of the function and call it using a procedural variable. The madExcept source code is a great source of examples of that technique.

    On balance, copying the source to your code is, in my view, the best option. All of your available options have drawbacks and this seems the simplest.