tags:

views:

117

answers:

2

Trying to write a GLOBAL CBT HOOK,
this is My code, but my hooking app doesn't recieve any Messages, neither writes the dll something to a test file.

This is My Code:

#include "stdafx.h"

#include "GlobalHook.h"


#include "Stdafx.h"

#include <iostream>
#include <fstream>

#define DLL_EXPORT
#include "GlobalHook.h"
#include <windows.h>
using namespace std;

#pragma comment(lib, "User32.lib")
#pragma unmanaged

HINSTANCE dllHandle;  
#pragma data_seg("ASEG")  
    HWND prnt = 0;  
#pragma data_seg()  
#pragma comment(linker, "/section:ASEG,RWS")  
    HHOOK hHook;  
    int x = 0;  

BOOL APIENTRY DllMain(    
         HINSTANCE hinstDLL,  // handle to DLL module  
         DWORD fdwReason,     // reason for calling function  
         PVOID lpReserved )  // reserved  
{
    // Perform actions based on the reason for calling.  
    switch( fdwReason )  
    {  
    case DLL_PROCESS_ATTACH:    
        dllHandle = hinstDLL;    
        return TRUE;  
        break;  
    }  
}  
   LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)  
   {
        if(nCode < 0)
        {
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }
        if(nCode == HCBT_MINMAX)
        {
            SendNotifyMessage(prnt, WM_USER + 3, wParam, lParam);
            if(x == 0)
            {
                ofstream myfile;
                myfile.open ("C:\\example.txt");
                myfile << "Dies in ein File.";
                myfile.close();
                x = 1;
            }
        }
        return CallNextHookEx(hHook, nCode, wParam, lParam);
   }
extern "C"
{
   DECLDIR int SetHook( HWND parent )
   {
       prnt = parent;
        hHook = SetWindowsHookEx(WH_CBT, CBTProc, dllHandle, 0);
        return 1;
   }
   DECLDIR void UninstallHook( void )
   {
      if(UnhookWindowsHookEx(hHook))
      {

      }
   }
   DECLDIR int Add(int a, int b)
   {
       return (a + b);
   }
}
A: 

Using a global hook requires an "injection DLL". Your DLL will get injected into every running process (with a UI thread). When a DLL is injected, any global variables in the module will be NULL/0. You have to cache the window handle you want to send messages in a globally accessable area. The simplest place is to use a desktop window property.

See GetProp and SetProp.

Note that if you want your global hook to work on 64-bit Windows you need to have both a 32- and 64-bit injection DLL as well as a 32- and 64-bit injector application. Otherwise you will only hook applications with the same bitness as your injector/DLL.

Tergiver
I did share the window handle with #pragma data_seg("ASEG")
alex
That doesn't work like you think it does. It's for static initialized data. The HWND cannot be hard-coded into the DLL.
Tergiver
Ok I'll try later
alex
A: 

The general structure seems sound.

#pragma comment(linker, "/section:ASEG,RWS") RWS might need to be rws

I'd strip down CBTProc so that you just log to file for the moment on every minmax, this would allow you to see that your dll has been injected properly.

WM_USER is ment to be used within a single app not across apps (though you can get away with it). Consider RegisterWindowMessage()

If the injected App is a lower priv level you may need to force your recieving app to allow contact from the lower priv app using ChangeWindowMessageFilter

Also might try also hooking WH_CALLWNDPROC, WH_GETMESSAGE to see if you can get other messages through.

Your window will also be hooked, make sure you don't get into a message sending loop. You are using SendMessage to forward the message, it will cause the message pump of the app to block, if that app is also your own processing the message you will lock.

indllmain you could use outputdebug to log what apps the dll is being loaded into

Greg Domjan