dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: WH_CBT hook

  1. #1
    Join Date
    Sep 2010
    Location
    Romania
    Posts
    8

    Lightbulb WH_CBT hook

    I feel like the other thread i started "GetMessage() questions" has gone a bit off-topic and the name doesn't resemble what it contains. I checked with a moderator and it's fine to create a new thread.
    So now i've learned that the application i want to write needs hooks and not proper usage of the function GetMessage(),as i thought 2 days ago ). Well, i started using VC++ and making Win32 applications for less than a week, just because i really needed an application and i couldn't find it anywhere, so i apologize for my beginner questions. I managed to get a .exe that does what i want, but now i got caught up and i want to optimize it.

    Now i'm trying to make a simple hook-based program that will pop up a message box when i open a new window. I think if i'll manage to do this, i'll be a step closer.
    Off course, i'm having problems ...
    I'm posting the code, maybe you find the problems:

    For creating the hk.dll
    i used the following hk.cpp:
    Code:
    #include <windows.h>
    
    #pragma data_seg("SharedBlock")
    HHOOK hhook = NULL;
    #pragma data_seg()
    
    HINSTANCE g_hInstance = NULL;
    
    
    
    LRESULT __declspec(dllexport)__stdcall CALLBACK CBTProc(      
        int nCode,
        WPARAM wParam,
        LPARAM lParam
    )
    {
    	if ( nCode == HCBT_CREATEWND)
    	{
    		MessageBox(NULL,"New Window","text", MB_OK);
        } 
    	LRESULT RetVal = CallNextHookEx( hhook, nCode, wParam, lParam );	
    return  RetVal;
    }
    
    _declspec(dllexport) void SetHook()
    {
    hhook = NULL;
    	hhook = SetWindowsHookEx(WH_CBT, CBTProc, g_hInstance, 0);
    	if(hhook==NULL)
    		MessageBox(NULL,"Unable to install hook","Error!",MB_OK);
    }
    
    
    _declspec(dllexport) void KillHook()
    {
     if(hhook) UnhookWindowsHookEx(hhook);
    }
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
    					 )
    {
        switch (ul_reason_for_call)
    	{
    		case DLL_PROCESS_ATTACH:
    		case DLL_THREAD_ATTACH:
    		case DLL_THREAD_DETACH:
    		case DLL_PROCESS_DETACH:
    			break;
        }
    	g_hInstance = (HINSTANCE)hModule;
    	hhook = NULL;
        return TRUE;
    }
    and hk.def:
    Code:
    ; hk.def : Declares the module parameters for the DLL.
    
    LIBRARY      "hk"
    DESCRIPTION  'Hook Sample'
    
    EXPORTS
    SetHook
    KillHook
    
    SECTIONS
    SharedBlock READ WRITE SHARED

    For the .exe i used just the following .cpp:
    Code:
    #include <windows.h>
    
    
    HMODULE HookDLL;
    
    typedef void (*LPSetHook)(void);
    LPSetHook SetHook = NULL;
    
    typedef void (*LPKillHook)(void);
    LPKillHook KillHook = NULL;
    
    BOOL LoadHookDLL()
    {
     HookDLL = LoadLibrary("hk.dll");
     if (!HookDLL) return(0);
    
     SetHook = (LPSetHook)GetProcAddress(HookDLL, "SetHook");
     if (!SetHook) return(0);
    
    
     KillHook = (LPKillHook)GetProcAddress(HookDLL, "KillHook");
     if (!KillHook) return(0);
    
     SetHook();
    
     return(1);
    }
    
    void FreeHookDLL()
    {
     KillHook();
     FreeLibrary(HookDLL);
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
     	LoadHookDLL();
    	Sleep(10000);
    	FreeHookDLL();
    	return(1);
    }
    i think it might be from the compiler options for the DLL. What Common Language Runtime Support should i use ?
    Any suggestions?
    thanks

    PS: I apologize again for the beginner questions.
    Last edited by rzv; October 1st, 2010 at 09:25 PM.

  2. #2
    Join Date
    Sep 2010
    Location
    Romania
    Posts
    8

    Post Re: WH_CBT hook

    After i tried everything I could think of in VS2010, I transfered the code in VS2008 and now it's working ... :|.
    Now that i can load the DLL and install the hook, etc ... i'm getting into hooking my desired message
    The message i need to hook is:
    00031084 P message: 0x0118 [Unknown] wParam:0000FFF8 lParam:97B03836 (from SPY++)
    With the DLL that i'm posting now i can hook any S-type message, but it doesn't work for my desired message. (posted message)
    How can i make a hook that finds POSTED messages ?

    DLL code:
    Code:
    #include<windows.h>
    #pragma data_seg("SharedBlock")
    HHOOK hook=NULL;
    #pragma data_seg()
    
    HINSTANCE g_hInstance=NULL;
    
    LRESULT __declspec(dllexport)__stdcall CALLBACK CallWndProc(
    	int nCode,
        WPARAM wParam,
        LPARAM lParam)
    {
    	if (nCode == HC_ACTION)
    	{
    		 CWPSTRUCT *msg;
    	msg = (CWPSTRUCT *)lParam;
    	   if (msg->message==0x0118)
    		   MessageBox(NULL,"BUTTON FOUND","text",MB_OK);
    	}
     return CallNextHookEx(hook,nCode,wParam,lParam);
    }
    
    _declspec(dllexport) void SetHook()
    {
    	hook=NULL;
    	hook=SetWindowsHookEx(WH_CALLWNDPROC,CallWndProc,g_hInstance, 0);
    	if(hook==NULL)
    		MessageBox(NULL,"Unable to install hook","Error!",MB_OK);
    }
    
    _declspec(dllexport) void KillHook()
    {
    	if (hook) UnhookWindowsHookEx(hook);
    }
    
    
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
    					 )
    {
        switch (ul_reason_for_call)
    	{
    		case DLL_PROCESS_ATTACH:
    		case DLL_THREAD_ATTACH:
    		case DLL_THREAD_DETACH:
    		case DLL_PROCESS_DETACH:
    			break;
        }
    	g_hInstance = (HINSTANCE)hModule;
    	hook = NULL;
        return TRUE;
    }

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,603

    Re: WH_CBT hook

    SetWindowsHookEx

    WH_CALLWNDPROC
    4

    Installs a hook procedure that monitors messages before the system sends them to the destination window procedure. For more information, see the CallWndProc hook procedure.
    . . .
    WH_GETMESSAGE
    3


    Installs a hook procedure that monitors messages posted to a message queue. For more information, see the GetMsgProc hook procedure.
    Best regards,
    Igor

  4. #4
    Join Date
    Sep 2010
    Location
    Romania
    Posts
    8

    Re: WH_CBT hook

    i tried it, still doesn't work ...
    Code:
    #include<windows.h>
    #pragma data_seg("SharedBlock")
    HHOOK hook=NULL;
    #pragma data_seg()
    
    HINSTANCE g_hInstance=NULL;
    
    LRESULT __declspec(dllexport)__stdcall CALLBACK GetMsgProc(
    	int nCode,
        WPARAM wParam,
        LPARAM lParam)
    {
    	if (nCode == HC_ACTION)
    	{
    		CWPSTRUCT *msg;
    	msg = (CWPSTRUCT *)lParam;
    	   if (msg->message==0x0118)
    		   MessageBox(NULL,"BUTTON FOUND","text",MB_OK);
    	}
     return CallNextHookEx(hook,nCode,wParam,lParam);
    }
    
    _declspec(dllexport) void SetHook()
    {
    	hook=NULL;
    	hook=SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,g_hInstance, 0);
    	if(hook==NULL)
    		MessageBox(NULL,"Unable to install hook","Error!",MB_OK);
    }
    
    _declspec(dllexport) void KillHook()
    {
    	if (hook) UnhookWindowsHookEx(hook);
    }

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,603

    Re: WH_CBT hook

    Of course it won't until you gain a habit to pay some respect to reading technical documentation:
    Code:
        msg = (CWPSTRUCT *)lParam;
    GetMsgProc
    lParam [in]
    LPARAM

    A pointer to an MSG structure that contains details about the message.
    Best regards,
    Igor

  6. #6
    Join Date
    Sep 2010
    Location
    Romania
    Posts
    8

    Re: WH_CBT hook

    thanks,
    but it still doesn't work:

    Code:
    LRESULT __declspec(dllexport)__stdcall CALLBACK GetMsgProc(      
        int nCode,
        WPARAM wParam,
        LPARAM lParam
    )
    {
    	if ( nCode == HC_ACTION)
    	{
    		MSG *msg;
    		msg=(MSG *)lParam;
    		if (msg->message==0x0118)
    			MessageBox(NULL,"Message found","text",MB_OK);
    	} 
    	LRESULT RetVal = CallNextHookEx( hhook, nCode, wParam, lParam );	
    return  RetVal;
    }
    
    _declspec(dllexport) void SetHook()
    {
    hhook = NULL;
    	hhook = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, g_hInstance, 0);
    	if(hhook==NULL)
    		MessageBox(NULL,"Unable to install hook","Error!",MB_OK);
    	
    }

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)