3

I tried to replace Delphi build-in function with my own version on-the-fly.

function ShortCutToTextOverride(ShortCut: TShortCut): string;
begin
  if SomeCondition then
    Result := Menus.ShortCutToText // after patching the pointer equals ShortCutToTextOverride
  else
  begin
    // My own code goes here
  end;
end;

FastcodeAddressPatch(@Menus.ShortCutToText, @ShortCutToTextOverride);

After patching, the original function is no longer accessible. It is possible access it anyway?

nguthrie
  • 2,615
  • 6
  • 26
  • 43
stanleyxu2005
  • 8,081
  • 14
  • 59
  • 94
  • 1
    If you want reliable code hooking for win32 (x64 is still under development) then I'd recommend madCodeHook: http://madshi.net/madCodeHookDescription.htm – Marjan Venema May 15 '12 at 09:48
  • 3
    You can use something [`like this`](http://stackoverflow.com/a/6003579/960757) as well. – TLama May 15 '12 at 10:20

1 Answers1

6

I'm afraid not: the first bytes are overwritten by a jump to the new function.

You can use KOLDetours.pas: it returns the pointer to the trampoline (original first few bytes that are overwritten by the detour). http://code.google.com/p/asmprofiler/source/browse/trunk/SRC/KOLDetours.pas

For example:

type
  TNowFunction = function:TDatetime;
var
  OrgNow: TNowFunction;
function NowExact: TDatetime;
begin
  //exact time using QueryPerformanceCounter
end; 

initialization
  OrgNow := KOLDetours.InterceptCreate(@Now, @NowExact);
  Now()     -> executes NowExact() 
  OrgNow()  -> executes original Now() before the hook 
André
  • 8,920
  • 1
  • 24
  • 24
  • KOLDetours works perfectly for 32bit compiler, but have some problems with 64bit compiler. I have not found any updates on 64bit. – stanleyxu2005 Feb 04 '14 at 15:44
  • I found a patched version `Cromis.Detours` is compatible with x64. I hope the information is helpful to you all. – stanleyxu2005 Mar 07 '14 at 02:44
  • @stanleyxu2005 are you sure it is working on 64bit? Because it is the same unit (copy of koldetours) with no changes for handling 64bit specific stuff. Or can you post a link to the patched version? – André Mar 07 '14 at 13:22
  • Here is the link https://code.google.com/p/native-look-vcl/ I was managed use KOLDetours to get it work. But my application failed to be compiled at x64 platform, until I switched to Cromis.Detours. Its original homepage can be searched in Google. – stanleyxu2005 Mar 08 '14 at 13:26
  • @stanleyxu2005 ok so it compiles but does it also work in 64bit? I doubt it does... – André Mar 10 '14 at 13:59
  • Yes, the code compiles (and also works with) `Cromis.Detours`. Sorry for late reply. – stanleyxu2005 Nov 10 '14 at 08:16