CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2001
    Posts
    73

    Accessing global variables inside a hook procedure in a simple win32 dll.

    HI,
    I have a system wide WH_CBT hook installed in a simple win32 dll. The way it works is, I have some global variables like so:


    UINT uArr[10];
    int iCount = -1;
    WinMain(.....)
    {
    //do stuff
    .....
    }




    Now, in the hook procedure, I am doing things like this:


    LRESULT CALLBACK MyCbtProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    if(some condition)
    uArr[++iCount] = some numeric value;





    The problem I have is: EVERY TIME, the hook procdure gets called, the value of iCount is -1. I want the values of these global variables to 'persist' because I need to reset some things when the hook gets un-installed. Am I missing some thing obvious?
    Any help appreciated.
    Nas.





    What matters in a fight, is not the size of the dog in the fight. It is the size of the fight...in the dog!

  2. #2
    Join Date
    Apr 2000
    Location
    San Francisco, California, USA
    Posts
    4,467

    Re: Accessing global variables inside a hook procedure in a simple win32 dll.

    DLL containing system wide hook is loaded into different processes
    in the system. By default each copy of the DLL gets private copy
    of the data. If you want some data to be shared across all instances
    of the DLL, put them into a shared section.

    Example:


    #pragma comment(linker, "/section:.shared,rws")
    #pragma data_seg(".shared")
    HHOOK gs_hHook = NULL;
    UINT gs_uArr[10] = { 0 };
    int gs_iCount = -1;
    #pragma data_seg()




    Note, all variables in the shared segment must be explicitly initialized,
    otherwise the compiler will put them into the BSS segment.


    Russian Software Development Network -- http://www.rsdn.ru

  3. #3
    Join Date
    Nov 2011
    Posts
    9

    Re: Accessing global variables inside a hook procedure in a simple win32 dll.

    At the risk of creating an internet feedback loop, I had this same issue more less and got help and solved it here:

    http://www.codeproject.com/Questions...10#cmt2_813987

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured