Results 1 to 26 of 26

Thread: I'm Back!! Global Hook problem

Threaded View

  1. #1

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    I'm Back!! Global Hook problem

    I'ts been a long time. How are you CornedBee?

    I have been programming in C# for a while now and now am forced to make a C++ dll to set up a global keyboard hook for a clipboard program I am making.

    My code never enters DllMain!! Is that ok?
    Code:
    #include <windows.h>
    #include "Global Hook.h"
    #include <stdio.h>
    
    #pragma data_seg(".shared")
    #pragma data_seg()
    
    //extern "C" tells compiler not to change the name of the 
    //function so that it can be called directly from another program.
    extern "C" _declspec(dllexport) HHOOK SetHook();
    extern "C" _declspec(dllexport) void Unhook();
    _declspec(dllexport) int _WEP (int bSystemExit);
    
    HHOOK hKeyhook=0;
    HINSTANCE hDll = LoadLibrary((LPCTSTR)"WinHook.dll");
    
    //---------------------------
    BOOL APIENTRY DllMain (
      HINSTANCE hInst     /* Library instance handle. */ ,
      DWORD reason        /* Reason this function is being called. */ ,
      LPVOID reserved     /* Not used. */ )
    {
        if ( reason == DLL_PROCESS_ATTACH)
           hDll = hInst;
    
    MessageBox(NULL, TEXT("ENTERED DLLMAIN"), TEXT(""), MB_OK);
        //return (int)(DisableThreadLibraryCalls (hDll));
    
        /* Returns TRUE on success, FALSE on failure */
        return TRUE;
    }
    
    //---------------------------
    LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
       // This is where you want to decide what to do when receiving keyboard parameters.
       MessageBox(NULL, TEXT("In CallBack"), TEXT("KeyPress"), MB_OK);
    
        return CallNextHookEx(hKeyhook, nCode, wParam, lParam);  //CallNextHookEx returns long value.
    }
    
    //---------------------------
    extern "C" _declspec(dllexport) HHOOK SetHook()
    {
        if(hKeyhook == NULL)
        {
             hKeyhook = SetWindowsHookEx(WH_KEYBOARD, KeyBoardProc, hDll, 0);
            
             if (hKeyhook == NULL)
                MessageBox(NULL, TEXT("Failed"), TEXT("Failed"), MB_OK);
             else
                MessageBox(NULL, TEXT("Hooked!!!"), TEXT("Success!!!"), MB_OK);
        }
    
        return hKeyhook;
    }
    It works but how do I notify my C# app that the callback was called and get which key was pressed?
    Last edited by aewarnick; Jun 10th, 2003 at 11:06 PM.

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