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
Where did you get this "HookCOMFunction" thing from? That's where you MIGHT get help with it. It is very unlikely you're ever going to get an answer to this question here as what you're doing is so far off the beaten path. You're in uncharted territory with hooking an Excel function. You are the only person I have ever heard asking any kind of question like this in 12 years on CodeProject.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
Where did you get this "HookCOMFunction" thing from? That's where you MIGHT get help with it. It is very unlikely you're ever going to get an answer to this question here as what you're doing is so far off the beaten path. You're in uncharted territory with hooking an Excel function. You are the only person I have ever heard asking any kind of question like this in 12 years on CodeProject.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave KreskowiakHi Dave, Thanks for answering I didn't get the HookCOMFunction from anywhere .. It is my code .. Matt Curland's book "advanced Visual Basic" touches on the subject of overriding COM Methods This is similar to hooking Windows API functions which can be done with VB as well .. unfortunately, unlike when redirecting API calls , overrinding COM Methods works ONLY (at least for me) when the hooked Method is called via code and not via the User Interface Do you know if there is a section on this website about COM/interfaces ? or any other place where I can ask ? Regards.
-
Hi Dave, Thanks for answering I didn't get the HookCOMFunction from anywhere .. It is my code .. Matt Curland's book "advanced Visual Basic" touches on the subject of overriding COM Methods This is similar to hooking Windows API functions which can be done with VB as well .. unfortunately, unlike when redirecting API calls , overrinding COM Methods works ONLY (at least for me) when the hooked Method is called via code and not via the User Interface Do you know if there is a section on this website about COM/interfaces ? or any other place where I can ask ? Regards.
Look down the left side navigation and you'll see the COM forum under General Programming. I haven't read the book at all, but what I think the book is touching on is just providing an implementation of COM interface methods. If you're talking about Windows Hooks, that's not API "hooking". I think what you're calling "hooking" is actually called "API Interception", usually done with a technique called "DLL Redirection". Windows Hooks are a very different concept where Windows provides various opportunities to participate in various function "pipelines", such as processing and altering keyboard and mouse messages before the messages are sent to their destinations for processing.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
Look down the left side navigation and you'll see the COM forum under General Programming. I haven't read the book at all, but what I think the book is touching on is just providing an implementation of COM interface methods. If you're talking about Windows Hooks, that's not API "hooking". I think what you're calling "hooking" is actually called "API Interception", usually done with a technique called "DLL Redirection". Windows Hooks are a very different concept where Windows provides various opportunities to participate in various function "pipelines", such as processing and altering keyboard and mouse messages before the messages are sent to their destinations for processing.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave KreskowiakYes. you are right Dave .. "DLL Redirection" is the right name and not "API hooking" such as when subclassing windows to intercept sent messages or when installing Mouse/Keyboard or CBT hooks etc The question still remains open about how to make the code I provided for overriding COM Methods work when the Methods are called via the User Interface I have searched everywhere but without any luck .. Hopefully someone knowledgeable can shed a light on this Regards.
-
Yes. you are right Dave .. "DLL Redirection" is the right name and not "API hooking" such as when subclassing windows to intercept sent messages or when installing Mouse/Keyboard or CBT hooks etc The question still remains open about how to make the code I provided for overriding COM Methods work when the Methods are called via the User Interface I have searched everywhere but without any luck .. Hopefully someone knowledgeable can shed a light on this Regards.
Like I said, you're in uncharted, or at the very most, very rarely ever tried, territory. Good Luck.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
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 else ?