vagy
asked on
Problem with SetWindowsHookEx to another application's window
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(h HookedWind ow, &temp2);
hHookGETMESSAGE = SetWindowsHookEx(WH_GETMES SAGE, 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(hHookGETMES SAGE, nCode, wParam, lParam);
}
msg = (MSG FAR *)lParam;
if (msg->hwnd == hHookedWindow) {
}
return CallNextHookEx(hHookGETMES SAGE, 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)h Instance);
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(h HookedWind ow, &temp2);
hHookGETMESSAGE = SetWindowsHookEx(WH_GETMES SAGE, GetMsgProc, hdll,temp1);
return true;
}
extern "C" bool FAR PASCAL RemoveTheHook(void)
{
UnhookWindowsHookEx(hHookG ETMESSAGE) ;
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.
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(h
hHookGETMESSAGE = SetWindowsHookEx(WH_GETMES
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:.
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(hHookGETMES
}
msg = (MSG FAR *)lParam;
if (msg->hwnd == hHookedWindow) {
}
return CallNextHookEx(hHookGETMES
}
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(
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(h
hHookGETMESSAGE = SetWindowsHookEx(WH_GETMES
return true;
}
extern "C" bool FAR PASCAL RemoveTheHook(void)
{
UnhookWindowsHookEx(hHookG
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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'...
return( FALSE );
A 'DllMain()' should *ONLY* return 'FALSE' if a *really* *severe* condition is met, not just 'by default'...
ASKER
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.
Registerclass failed it return 0 and caused the error.
So thank you for showing the direction.
ASKER
"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 ?