Thread: Hook problem

  1. #1
    Registered User
    Join Date
    Jan 2003

    Unhappy Hook problem

    why we must decalare the Hook API function in a DLL file? Is this possible declare the Hook API in an exe file?

    i always see someone declare the following function in the hook dll file, exactly what is this?

    #pragma data_seg(".hello")
    HHOOK g_hHkKeyboard = NULL;
    #pragma data_seg()
    #pragma comment(linker, "/section:.hello,rws")

    evenif i don't declare the #pragma data_seg(), my program also can call the Hook API.

  2. #2
    Registered User
    Join Date
    Dec 2002
    All content of a dll is always copied to the memory space of every program and so all the variables doesn't share the same value, if they haven't been given the same value that is. The #pragma data_seg(".hello") tells the compiler to register all the following code in the data segment called "Hello". The #pragma data_seg() tells the compiler that the current data segment has ended. The pragma comment(linker, "/section:.hello,rws") tells the compiler to use all the code in the "Hello" data segmnet as public code and share it between all applications that use this dll. In others words the g_hHkKeyboard variable has the same value in every program that uses this dll. This is often used in hook dlls becouse when other applications want to call the hook function (like ex. KeboardProc) they must have the same hook handle value to send the hook message to the next hook function in the hook chain. That is the part that makes hook apis more advanced all programs have to be able to call the hook function when they get the message that is hooked.

    LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
        return CallNextHookEx(hHook, nCode, wParam, lParam);
    But if you remove that shared data segment only your application can call the hook function and makes the hole idea to use the hook in a dll pointless. I hope you understod all of that, if you didn't please ask more.
    Last edited by Aidman; 02-09-2003 at 06:28 AM.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Keyboard Hook Problem
    By maxorator in forum Windows Programming
    Replies: 4
    Last Post: 09-08-2006, 09:10 AM
  3. Weird Hook Problem.
    By Mastadex in forum Windows Programming
    Replies: 0
    Last Post: 06-22-2006, 12:01 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. DLL problem
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 02:34 PM