?
Solved

Problem with SetWindowsHookEx to another application's window

Posted on 2000-04-11
4
Medium Priority
?
1,921 Views
Last Modified: 2008-03-04
hello.

I'm trying to hook the messages of a window from external application.
I have an exe file which use FindWindow to get the external application's window handle, and in my hook dll, i use GetWindowThreadProcessId, to get the thread id, and then use SetWindowsHookExlike this:
temp1 = GetWindowThreadProcessId(hHookedWindow, &temp2);
hHookGETMESSAGE      = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, hdll,temp1);

Using the debugger i see that hHookGETMESSAGE isn't NULL, which means the hook succeded, but the problem is that i don't reach the hook procedure which is in my dll.

The hook dll looks like this:

#include <windows.h>
#include <stdio.h>

#pragma data_seg(".shared")
HHOOK hHookGETMESSAGE=NULL;
HINSTANCE      hdll;
#pragma data_seg()
#pragma comment(linker,"/SECTION:.shared,RWS")

extern "C" bool FAR PASCAL SetTheHook(HWND phHookedWindow);
extern "C" bool FAR PASCAL RemoveTheHook(void);

LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
      MSG            *msg;

      FILE      *pr;
      pr = fopen("c:\\hook.log", "a");
      fprintf(pr, "MSG: %d %d %d\n", nCode, wParam, lParam);
      fclose(pr);

      if (nCode < 0) {
            return CallNextHookEx(hHookGETMESSAGE, nCode, wParam, lParam);
      }

      msg = (MSG FAR *)lParam;

      if (msg->hwnd == hHookedWindow) {
      }

      return CallNextHookEx(hHookGETMESSAGE, nCode, wParam, lParam);
}

int APIENTRY DllMain (HINSTANCE hInstance,DWORD dwReason,LPVOID lpReserved)
{
      BOOL Ret;
      WNDCLASSEX      wcex;

      hdll = hInstance;

      switch ( dwReason ) {
        case DLL_THREAD_ATTACH :
            return (TRUE);
        case DLL_PROCESS_ATTACH:
            Ret = DisableThreadLibraryCalls((HMODULE)hInstance);
            return (Ret);
        case DLL_THREAD_DETACH :                                             
              return( TRUE );      
        case DLL_PROCESS_DETACH:
              return( TRUE );      
      }
      return( FALSE );
}

extern "C" bool FAR PASCAL SetTheHook(HWND phHookedWindow)
{
      RECT      Rect;
      HWND      hButton2;
      DWORD      temp1;
      
      hHookedWindow = phHookedWindow;

      temp1 = GetWindowThreadProcessId(hHookedWindow, &temp2);

       hHookGETMESSAGE      = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, hdll,temp1);
      return true;
}

extern "C" bool FAR PASCAL RemoveTheHook(void)
{
   UnhookWindowsHookEx(hHookGETMESSAGE);
   return true;
}

Ofcause the logfile is empty which indicates i didn't enter the function.

So, can someone think of the reason ?
Maybe it concert to the DEF file ?

Thanks.
0
Comment
Question by:vagy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 2704418
In order to set a hook in anexternal application, the thread ID must be set to zero, e.g.

hHookGETMESSAGE = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, hdll,0);

See also

http://msdn.microsoft.com/library/techart/msdn_hooks32.htm
0
 

Author Comment

by:vagy
ID: 2704966
When i use 0, i get the message
"Initialization of the dynamic link library XXXX.dll failed. The process is terminating abnormaly".
I get this message infinitly, when XXXX are some process that run on my computer. I must terminate the application to avoid the endless message. If i use Keyboard hook for example instead of the GETMESSAGE, is doesn't happen. It also happen in WH_CALLWNDPROC.
Any idea ?
0
 
LVL 86

Expert Comment

by:jkr
ID: 2705029
This message is due to the last statement in your 'DllMain()':

return( FALSE );

A 'DllMain()' should *ONLY* return 'FALSE' if a *really* *severe* condition is met, not just 'by default'...
0
 

Author Comment

by:vagy
ID: 2706892
Well, the problem is solved. It indeed was because of return FALSE, but it wasn't in this line, it's because a RegisterClassEx i had in the DLL_PROCESS_ATTACH that i erased from the source i gave. And since the
Registerclass failed it return 0 and caused the error.
So thank you for showing the direction.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question