Results 1 to 5 of 5

Thread: System-wide hook

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    System-wide hook

    I am trying to install a Shell system-wide hook but because I have to put the application-defined procedure (ShellProc) in a dll, so I have created a simple dll using ATL and so far it's working all right.
    Here is the procedure which is in a dll:
    PHP Code:
    LRESULT WINAPI ShellProc(int nCodeWPARAM wParamLPARAM lParam)
    {
        
    // TODO: Add your implementation code here
        
    switch(nCode)
        {
        case 
    HSHELL_WINDOWCREATED:
            
    MessageBox(NULL,"","Window Created"MB_OK);
            break;
        case 
    HSHELL_WINDOWDESTROYED:
            
    MessageBox(NULL,"","Window Destroyed"MB_OK);
            break;
        default:
            break;
        }
            return 
    CallNextHookEx(hhooknCodewParamlParam);

    There's also a function called "StartHook" in the same dll which actually sets the the WH_SHELL hook using SetWindowsHookEx(). So whenever I call the StartHook function, I start getting the message boxes above correctly but....
    I'll have to perform on the messages (ex HSHELL_WINDOWSCREATED) inside the dll because the procedure is inside the dll. How can the calling thread (or me if I am using this dll) be notified which message is recieved? Will have to write my own for getting the address of a CALLBACK message from the programmer and then call that function from the dll? Or will it actually work?
    Baaaaaaaaah

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You created the DLL with ATL???

    As for your question: either retrieve a function pointer or a window handle to send messages to (would be passed to StartHook)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    I am creating a simple DLL - no COM.
    Thanks for the ideas. I guess sending the message to the provided window will be the easiest thing.
    I'll try that.
    Baaaaaaaaah

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Still, a simple Win32 DLL would be better.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Ok, it is working but not quite right. Whenever I recieve a message inside my hook procedure, I simply send two messages to the window hosting the dll (the client).
    Here isn't the hook procedure inside the DLL:
    PHP Code:
    HRESULT DLLEXPORT MyhookProc(int codeWPARAM wParamLPARAM lParam)
    {
    SendMessage(hookparentWM_HOOKMESSAGE, (WPARAM)code0);
    return 
    SendMessage(hookparentWM_HOOKMESSAGEPARAMSwParamlParam);

    So in the first message, I send which message was recieved. Then the second message I simply send the wParam and lParam values recieved. Then it's up to the client to either call "CallNextHookEx" or block the message. That's why I am returning the value I get from sending WM_HOOKMESSAGEPARAMS. Everything sounds good so far...

    In my actual program, I install a hook of type WH_KEYBOARD. It says it installed the hook correctly and then I also recieve a WM_HOOKMESSAGE from the dll to my window procedure telling me about a key pressed. Here's the code I am using two handle about two messages:
    PHP Code:
        case WM_HOOKMESSAGE:
            
    msghook = (int)wParam//Store the message
            
    return 0;
        case 
    WM_HOOKMESSAGEPARAMS:
            
    //    MessageBox(NULL,"Message Recieved","hello",MB_OK);

            
    if ((msghook == HC_ACTION) || (msghook == HC_NOREMOVE))
            {
                
    MessageBox(NULL,"Key pressed","hello",MB_OK);
            }
            return 
    CallNextHookEx(hhkmsghookwParamlParam); 
    I am simply checking if a key is pressed or not. Whenever I press a key I recieve a message saying "Message Recieved" ONLY while my window has the focus although I installed a system-wide hook which is supposed to monitor every thread's message. I also see a message saying "Key pressed" so it means my message value (recieved from the DLL) is wrong. What's going wrong with it?
    Baaaaaaaaah

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