• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 611
  • Last Modified:

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;
}
0
Crestline
Asked:
Crestline
  • 4
  • 4
1 Solution
 
jkrCommented:
>>HINSTANCE hDll = GetModuleHandle(NULL);

This is the problem - you need to pass the DLL's instance handle, not the one of the process. Use

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

         hH = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,hModule,NULL);
    }
    else if (reason == DLL_PROCESS_DETACH)
    {
         MessageBox(0,"Detaching", "Debug", 0);
         UnhookWindowsHookEx(hH);
    }
      return TRUE;
}
0
 
CrestlineAuthor Commented:
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);

0
 
jkrCommented:
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
);
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
AlexFMCommented:
switch(hCode)

should be

switch(wParam)
0
 
jkrCommented:
Or, actually

switch(ww)

*duck* :o)
0
 
CrestlineAuthor Commented:
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.
0
 
jkrCommented:
>>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);
0
 
CrestlineAuthor Commented:
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.



0
 
CrestlineAuthor Commented:
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now