DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 15 of 15

Thread: System Hook using C++

  1. #1
    Mike Guest

    System Hook using C++


    I am trying to write a function in C to get around VB's system hook limitation.
    To keep as much as possible in VB, I just have the hook function in the C++
    dll and it calls VB. I create and destroy the hook in VB. All works OK, but
    when I switch to another app and back again, the hook stops working. Any
    ideas?

    Also, in C i am using CallWindowProcA to call my VB function. Does anyone
    know the proper way to call a VB function from C. Here is some code and thanks
    in advance.

    Mike

    In VB:

    Private Declare Sub SetVBAddress Lib "TestDLL.dll" (ByVal Address As Long)
    Private Declare Function KeyProcAddress Lib "TestDLL.dll" () As Long

    Sub cmdStartHook
    SetVBAddress AddressOf SubWndProc
    HookId = SetWindowsHookEx(WH_MOUSE, KeyProcAddress, App.hInstance, 0)
    End Sub


    int VBHookAddress;
    LRESULT CALLBACK KeyboardProc(

    int code, // hook code
    WPARAM wParam, // virtual-key code
    LPARAM lParam // keystroke-message information
    )
    {
    return CallWindowProcA ((WNDPROC) VBHookAddress,0,code,wParam,lParam);
    //just pass it on to VB
    }

    extern "C" void _stdcall SetVBAddress(int VBAdd)
    {
    VBHookAddress =VBAdd;
    //Keep the address of the VB function
    }

    extern "C" int _stdcall KeyProcAddress()
    {
    return (int)KeyboardProc;
    //Return the address of KeyProc to VB
    }

  2. #2
    Eduardo A. Morcillo Guest

    Re: System Hook using C++

    Don't pass and call a pointer to a function, because the function is in
    another address space and the function cannot be called. Instead pass a
    window handle and use PostMessage or SendMessage to pass the data.

    --
    Eduardo A. Morcillo
    http://www.domaindlx.com/e_morcillo



  3. #3
    Eduardo A. Morcillo Guest

    Re: System Hook using C++

    Don't pass and call a pointer to a function, because the function is in
    another address space and the function cannot be called. Instead pass a
    window handle and use PostMessage or SendMessage to pass the data.

    --
    Eduardo A. Morcillo
    http://www.domaindlx.com/e_morcillo



  4. #4
    Mattias Sjögren Guest

    Re: System Hook using C++

    Mike,

    >All works OK, but when I switch to another app and back again,
    >the hook stops working. Any ideas?


    In addition to what Eduardo said, you should also store the window
    handle in a shared data segment in the DLL so it's accessible from all
    processes.

    // In code

    #pragma data_seg(".shared")
    HWND YourVBWindowHandle;
    #pragma data_seg()


    // Then , to make the ".shared" segement shared, you either
    // put this in a DEF file:

    SECTIONS
    .shared READ WRITE SHARED

    // or add this linker option:

    /SECTION:.shared,RWS


    Mattias

    __________________________________________________
    Mattias Sjögren (MCP) - mattiass @ hem.passagen.se
    VB+ http://hem.spray.se/mattias.sjogren/
    Please send questions/replies to the newsgroups

  5. #5
    Mattias Sjögren Guest

    Re: System Hook using C++

    Mike,

    >All works OK, but when I switch to another app and back again,
    >the hook stops working. Any ideas?


    In addition to what Eduardo said, you should also store the window
    handle in a shared data segment in the DLL so it's accessible from all
    processes.

    // In code

    #pragma data_seg(".shared")
    HWND YourVBWindowHandle;
    #pragma data_seg()


    // Then , to make the ".shared" segement shared, you either
    // put this in a DEF file:

    SECTIONS
    .shared READ WRITE SHARED

    // or add this linker option:

    /SECTION:.shared,RWS


    Mattias

    __________________________________________________
    Mattias Sjögren (MCP) - mattiass @ hem.passagen.se
    VB+ http://hem.spray.se/mattias.sjogren/
    Please send questions/replies to the newsgroups

  6. #6
    Mike Guest

    Re: System Hook using C++


    >the function is in another address space


    So a standard DLL does not run in the same process as the exe?

    >use PostMessage or SendMessage to pass the data.


    But many standard dlls call functions in VB, is there any way to do this?

    Mike

    "Eduardo A. Morcillo" <edanmo@geocities.com> wrote:
    >Don't pass and call a pointer to a function, because the function is in
    >another address space and the function cannot be called. Instead pass a
    >window handle and use PostMessage or SendMessage to pass the data.
    >
    >--
    >Eduardo A. Morcillo
    >http://www.domaindlx.com/e_morcillo
    >
    >



  7. #7
    Mike Guest

    Re: System Hook using C++


    >the function is in another address space


    So a standard DLL does not run in the same process as the exe?

    >use PostMessage or SendMessage to pass the data.


    But many standard dlls call functions in VB, is there any way to do this?

    Mike

    "Eduardo A. Morcillo" <edanmo@geocities.com> wrote:
    >Don't pass and call a pointer to a function, because the function is in
    >another address space and the function cannot be called. Instead pass a
    >window handle and use PostMessage or SendMessage to pass the data.
    >
    >--
    >Eduardo A. Morcillo
    >http://www.domaindlx.com/e_morcillo
    >
    >



  8. #8
    Eduardo A. Morcillo Guest

    Re: System Hook using C++


    > >the function is in another address space

    >
    > So a standard DLL does not run in the same process as the exe?


    Yes, it does.

    But DLLs used with system wide hooks have to be loaded by each process on
    the system to work. So the DLL is loaded and runs in other address space
    (except for your application).

    > >use PostMessage or SendMessage to pass the data.

    >
    > But many standard dlls call functions in VB, is there any way to do this?


    But they are not running in another process they are running on the VB
    process.

    HTH.

    --
    Eduardo A. Morcillo
    http://www.domaindlx.com/e_morcillo




  9. #9
    Eduardo A. Morcillo Guest

    Re: System Hook using C++


    > >the function is in another address space

    >
    > So a standard DLL does not run in the same process as the exe?


    Yes, it does.

    But DLLs used with system wide hooks have to be loaded by each process on
    the system to work. So the DLL is loaded and runs in other address space
    (except for your application).

    > >use PostMessage or SendMessage to pass the data.

    >
    > But many standard dlls call functions in VB, is there any way to do this?


    But they are not running in another process they are running on the VB
    process.

    HTH.

    --
    Eduardo A. Morcillo
    http://www.domaindlx.com/e_morcillo




  10. #10
    Mike Guest

    Re: System Hook using C++


    >HTH.


    Thanks, this has helped muchly.

    How do I load the dll on the system?

    Mike


    "Eduardo A. Morcillo" <edanmo@geocities.com> wrote:
    >
    >> >the function is in another address space

    >>
    >> So a standard DLL does not run in the same process as the exe?

    >
    >Yes, it does.
    >
    >But DLLs used with system wide hooks have to be loaded by each process on
    >the system to work. So the DLL is loaded and runs in other address space
    >(except for your application).
    >
    >> >use PostMessage or SendMessage to pass the data.

    >>
    >> But many standard dlls call functions in VB, is there any way to do this?

    >
    >But they are not running in another process they are running on the VB
    >process.
    >
    >HTH.
    >
    >--
    >Eduardo A. Morcillo
    >http://www.domaindlx.com/e_morcillo
    >
    >
    >



  11. #11
    Mike Guest

    Re: System Hook using C++


    >HTH.


    Thanks, this has helped muchly.

    How do I load the dll on the system?

    Mike


    "Eduardo A. Morcillo" <edanmo@geocities.com> wrote:
    >
    >> >the function is in another address space

    >>
    >> So a standard DLL does not run in the same process as the exe?

    >
    >Yes, it does.
    >
    >But DLLs used with system wide hooks have to be loaded by each process on
    >the system to work. So the DLL is loaded and runs in other address space
    >(except for your application).
    >
    >> >use PostMessage or SendMessage to pass the data.

    >>
    >> But many standard dlls call functions in VB, is there any way to do this?

    >
    >But they are not running in another process they are running on the VB
    >process.
    >
    >HTH.
    >
    >--
    >Eduardo A. Morcillo
    >http://www.domaindlx.com/e_morcillo
    >
    >
    >



  12. #12
    Eduardo A. Morcillo Guest

    Re: System Hook using C++


    > How do I load the dll on the system?


    Passing the DLL hModule and function pointer to SetWindowsHookEx:

    Dim hModule As Long, lFuncAddr As Long
    Dim hHook As Long

    hModule = LoadLibrary("mydll.dll")
    lFuncAddr = GetProcAddress(hModule, "myhookfunction")

    hHook = SetWindowsHookEx(..., lFuncAddr, hModule, 0)

    Then the DLL is automatically loaded by the other processes.

    --
    Eduardo A. Morcillo
    http://www.domaindlx.com/e_morcillo




  13. #13
    Eduardo A. Morcillo Guest

    Re: System Hook using C++


    > How do I load the dll on the system?


    Passing the DLL hModule and function pointer to SetWindowsHookEx:

    Dim hModule As Long, lFuncAddr As Long
    Dim hHook As Long

    hModule = LoadLibrary("mydll.dll")
    lFuncAddr = GetProcAddress(hModule, "myhookfunction")

    hHook = SetWindowsHookEx(..., lFuncAddr, hModule, 0)

    Then the DLL is automatically loaded by the other processes.

    --
    Eduardo A. Morcillo
    http://www.domaindlx.com/e_morcillo




  14. #14
    Mike Guest

    Re: System Hook using C++


    Eduardo,

    Thanks heaps! I am at work (so must concentrate on boring DB apps) but will
    try this tonight.

    Mike

    "Eduardo A. Morcillo" <edanmo@geocities.com> wrote:
    >
    >> How do I load the dll on the system?

    >
    >Passing the DLL hModule and function pointer to SetWindowsHookEx:
    >
    >Dim hModule As Long, lFuncAddr As Long
    >Dim hHook As Long
    >
    > hModule = LoadLibrary("mydll.dll")
    > lFuncAddr = GetProcAddress(hModule, "myhookfunction")
    >
    > hHook = SetWindowsHookEx(..., lFuncAddr, hModule, 0)
    >
    >Then the DLL is automatically loaded by the other processes.
    >
    >--
    >Eduardo A. Morcillo
    >http://www.domaindlx.com/e_morcillo
    >
    >
    >



  15. #15
    Mike Guest

    Re: System Hook using C++


    Eduardo,

    Thanks heaps! I am at work (so must concentrate on boring DB apps) but will
    try this tonight.

    Mike

    "Eduardo A. Morcillo" <edanmo@geocities.com> wrote:
    >
    >> How do I load the dll on the system?

    >
    >Passing the DLL hModule and function pointer to SetWindowsHookEx:
    >
    >Dim hModule As Long, lFuncAddr As Long
    >Dim hHook As Long
    >
    > hModule = LoadLibrary("mydll.dll")
    > lFuncAddr = GetProcAddress(hModule, "myhookfunction")
    >
    > hHook = SetWindowsHookEx(..., lFuncAddr, hModule, 0)
    >
    >Then the DLL is automatically loaded by the other processes.
    >
    >--
    >Eduardo A. Morcillo
    >http://www.domaindlx.com/e_morcillo
    >
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center