Jaafar Tribak
Well-known Member
- Joined
- Dec 5, 2002
- Messages
- 9,779
- Office Version
- 2016
- Platform
- Windows
Hi all,
I have been reading on the layout of COM objects and Virtual Tables and I have managed to hack Excel Methods and Properties .. It's surprisingly easier than I had thought but just as I was getting excited about this idea and it's enormous potential for adding new functionality to Excel (particularly new & useful events ) , I found out , to my dismay, that this only works when Methods/Properties are called via code and not via the user interface
Here is an example of how to redirect the Application 'Calculate' Method to our own function
Try running the HookCOMFunction Macro above and then execute the following Test to see what happens :
As I said, If you try calculating Excel via the User Interface (Alt+Ctl+F9) the 'OverrideFunction' redirect is not called and excel performs a calculation as normal - Very disappointing .. Obviously, Excel must be calling its Methods and Properties under the hood differently .. This hacking trick remains, however, an interesting curiosity that may prove useful in some coding situations
I have been reading on the layout of COM objects and Virtual Tables and I have managed to hack Excel Methods and Properties .. It's surprisingly easier than I had thought but just as I was getting excited about this idea and it's enormous potential for adding new functionality to Excel (particularly new & useful events ) , I found out , to my dismay, that this only works when Methods/Properties are called via code and not via the user interface
Here is an example of how to redirect the Application 'Calculate' Method to our own function
Code:
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&
Private Const lFuncOffset As Long = 84 'Excel Application.calculate Method VTable Offset
Private pVTable As Long
Private initAddr As Long
Private bHooked As Boolean
Sub HookCOMFunction()
If bHooked = False Then
CopyMemory pVTable, ByVal ObjPtr(Application), 4
CopyMemory initAddr, ByVal pVTable + lFuncOffset, 4
VirtualProtect pVTable + lFuncOffset, 4&, PAGE_EXECUTE_READWRITE, 0&
CopyMemory ByVal pVTable + lFuncOffset, AddressOf OverrideFunction, 4
bHooked = True
End If
End Sub
Sub UnHookComFunction()
CopyMemory ByVal pVTable + lFuncOffset, initAddr, 4
bHooked = False
End Sub
Private Function OverrideFunction(ByVal voObjPtr As Long, ByVal Param As Long) As Long
MsgBox "Excel 'Calculate' Method has been Hooked !!"
End Function
Try running the HookCOMFunction Macro above and then execute the following Test to see what happens :
Code:
Sub Test()
Application.Calculate
End Sub
As I said, If you try calculating Excel via the User Interface (Alt+Ctl+F9) the 'OverrideFunction' redirect is not called and excel performs a calculation as normal - Very disappointing .. Obviously, Excel must be calling its Methods and Properties under the hood differently .. This hacking trick remains, however, an interesting curiosity that may prove useful in some coding situations