Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. EXCEL - Hooking Excel VTABLE

EXCEL - Hooking Excel VTABLE

Scheduled Pinned Locked Moved Visual Basic
designquestiondiscussion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jaafar tribak
    wrote on last edited by
    #1

    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

    D J 2 Replies Last reply
    0
    • J jaafar tribak

      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

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      J 1 Reply Last reply
      0
      • D 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 Kreskowiak

        J Offline
        J Offline
        jaafar tribak
        wrote on last edited by
        #3

        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.

        D 1 Reply Last reply
        0
        • J jaafar tribak

          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.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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

          J 1 Reply Last reply
          0
          • D 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 Kreskowiak

            J Offline
            J Offline
            jaafar tribak
            wrote on last edited by
            #5

            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.

            D 1 Reply Last reply
            0
            • J jaafar tribak

              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.

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • J jaafar tribak

                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

                J Offline
                J Offline
                jaafar tribak
                wrote on last edited by
                #7

                Bump .. Any thoughts on this anyone else ?

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups