Link to home
Create AccountLog in
Avatar of Crestline
Crestline

asked on

SetWindowsHookEx - Why not working

This DLL gets injected in to a simple form application using a 'starter app' which uses CreateProccessEXA from Madshi's library (www.madshi.net).  For testing purposes I've set it up to hook the keyboard up and down arrows.  Because of this, I'm not sure if I'm setting the hook correctly or not.  

Can someone explain to me why this does not work?  I thought I had read somewhere that maybe the hook has to be in a loop or something...  I don't, the only example I find are very basic and they only seem to be in EXEs, not DLLs.  Does that make a difference?

I know that the DLL is getting injected and started because I get the "Attaching" messagebox when my starter app is used and "Detaching" messagebox when I close my form.  So the DLL is running in the simple form app  Ultimately what I would like to do is hook messages sent to controls with in the form.

Anyways, here's the code.  If more explanation is needed, just let me know.


/////////////// THE DLL ///////////////////

#include "stdafx.h"
#include "windows.h"
#include <stdlib.h>
#include "NSR_SM.h"

HHOOK hH;

LRESULT CALLBACK KeyboardProc(int hCode,WPARAM ww,LPARAM ll)
{
      if (hCode < 0)
            return CallNextHookEx(hH,hCode,ww,ll);
      else
      {
            switch(hCode)
            {
                  case VK_UP:
                        // log here info , ww has its HWND
                        MessageBox(0,"Up", "Hook",0);
                        break;
                  case VK_DOWN:
                        // same
                        MessageBox(0,"Down", "Hook",0);
                        break;
            }
            return CallNextHookEx(hH,hCode,ww,ll);
      }
}

BOOL WINAPI DllMain( HANDLE hModule,
                       DWORD reason,
                       LPVOID lpReserved)
{
      if (reason == DLL_PROCESS_ATTACH)
      {
            MessageBox(0,"Attaching", "Debug", 0);

            HINSTANCE hDll = GetModuleHandle(NULL);
            hH = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,hDll,NULL);
      }
      else if (reason == DLL_PROCESS_DETACH)
      {
            MessageBox(0,"Detaching", "Debug", 0);
            UnhookWindowsHookEx(hH);
      }
       return TRUE;
}



//////////// THE STARTER APP /////////////

#include "stdafx.h"
#include "windows.h"
#include "madCHook.h"


int _tmain(int argc, _TCHAR* argv[])

{
      STARTUPINFO si;
        PROCESS_INFORMATION pi;

      ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( &pi, sizeof(pi) );

      InitializeMadCHook();

      CreateProcessExA((LPCSTR)"C:\\NSR_Form.exe",
                              NULL,
                               NULL,
                               NULL,
                               FALSE,
                               0,
                               NULL,
                               NULL,
                               &si,
                                         &pi,
                               (LPCSTR)"C:\\NSR_SM.dll");
      FinalizeMadCHook();

      return 0;
}
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Crestline
Crestline

ASKER

Wow, thanks for the quick reply!

This is going to sound elementary, but  hH = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,hModule,NULL); errors out because it want's amd HINSTANCE, not a HANDLE.  

Should simply casting as HINSTANCE work?
hH = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,(HINTANCE)hModule,NULL);

Actually, you should provide the correct signature for your 'DllMain()', which is

BOOL WINAPI DllMain(
  HINSTANCE hinstDLL,  // handle to DLL module
  DWORD fdwReason,     // reason for calling function
  LPVOID lpvReserved   // reserved
);
switch(hCode)

should be

switch(wParam)
Or, actually

switch(ww)

*duck* :o)
hH = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,hinstDLL,NULL);

I only want this hook to occur in the form that this DLL is attached to, is this the case or do I need the dwThreadID as well?  Or is the dwThreadID the thread of the DLL and not the Form?

Thanks.
>>I only want this hook to occur in the form that this DLL is attached to

Oh, in this case, use

 hH = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,NULL,0);
Not sure what I'm doing wrong but when I use the old version the hook works, but it starts up in other windows.  Like when I bring another window to the forground and hit a key on the keyboard, my "Attaching" meesagebox comes up because my DLL is now in that process.
 hH = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,hinstDLL,NULL);

When I use the new version, nothing seems to happen, nothing is triggered by the key strokes.
hH = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,NULL,0);

I'm going to start a new Question as I didn't specifically state that I wanted this to only run in my Form's process and not system wide.  As I said, in the end, I want to monitor messages sent to controls in this form, specifically messages sent to a listbox.