Overriding Excel Methods and Properties (VTable hacking)

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,779
Office Version
  1. 2016
Platform
  1. 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
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
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top