EXCEL - Hooking Excel VTABLE
-
The following code is supposed to redirect the call to the Excel Calculate Method to my own function (MeMsg) After running the HookCOMFunction routine , the Test Macro successfully executes the MeMsg replacement function as expected .. So far so good However, when executing an excel calculation via the User Interface (not via code) such as by pressing the F9 key , the MeMsg replacement function doesn't get called ... I thought that replacing the 'Calculate' VTable offset address with the address of my replacement function would also work everytime excel is calculated via the User Interface Any thoughts anyone ? My goal is to hook the excel Calculate Method via code as well as via the UI Regards. Code : Option Explicit Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" ( _ Destination As Any, _ Source As Any, _ ByVal Length As Long _ ) Private Declare Function VirtualProtect Lib "kernel32.dll" ( _ ByVal lpAddress As Long, _ ByVal dwSize As Long, _ ByVal flNewProtect As Long, _ lpflOldProtect As Long _ ) As Long Private Const PAGE_EXECUTE_READWRITE As Long = &H40& Sub HookCOMFunction() Dim pVTable As Long Const lFuncOffset As Long = 84 CopyMemory pVTable, ByVal ObjPtr(Application), 4 VirtualProtect pVTable + lFuncOffset, 4&, PAGE_EXECUTE_READWRITE, 0& CopyMemory ByVal pVTable + lFuncOffset, AddressOf MeMsg, 4 End Sub Private Function MeMsg(ByVal voObjPtr As Long, ByVal Param As Long) As Long MsgBox "Excel 'Calculate Method Hooked !!" End Function Sub Test() Application.Calculate End Sub
-
The following code is supposed to redirect the call to the Excel Calculate Method to my own function (MeMsg) After running the HookCOMFunction routine , the Test Macro successfully executes the MeMsg replacement function as expected .. So far so good However, when executing an excel calculation via the User Interface (not via code) such as by pressing the F9 key , the MeMsg replacement function doesn't get called ... I thought that replacing the 'Calculate' VTable offset address with the address of my replacement function would also work everytime excel is calculated via the User Interface Any thoughts anyone ? My goal is to hook the excel Calculate Method via code as well as via the UI Regards. Code : Option Explicit Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" ( _ Destination As Any, _ Source As Any, _ ByVal Length As Long _ ) Private Declare Function VirtualProtect Lib "kernel32.dll" ( _ ByVal lpAddress As Long, _ ByVal dwSize As Long, _ ByVal flNewProtect As Long, _ lpflOldProtect As Long _ ) As Long Private Const PAGE_EXECUTE_READWRITE As Long = &H40& Sub HookCOMFunction() Dim pVTable As Long Const lFuncOffset As Long = 84 CopyMemory pVTable, ByVal ObjPtr(Application), 4 VirtualProtect pVTable + lFuncOffset, 4&, PAGE_EXECUTE_READWRITE, 0& CopyMemory ByVal pVTable + lFuncOffset, AddressOf MeMsg, 4 End Sub Private Function MeMsg(ByVal voObjPtr As Long, ByVal Param As Long) As Long MsgBox "Excel 'Calculate Method Hooked !!" End Function Sub Test() Application.Calculate End Sub
Bump .. Any thoughts on this anyone ?