
Q: subclassing problem, with hook and windows messages
Hi all,
here is a tough one. I am trying to manipulate the windows messages being
received by a third-party application. In Visual Studio C++ 6,
I have setup a dll with a hook procedure and a subclassing procedure. Both
are needed to be able to inject the dll
in the address space of the third-party application (hook needed), and be
able to preprocess the messages for it (subclass procedure needed).
This I have learned this from kind J. Thuemmler from Delphin Software, who
has given me some good pieces of advice, and
BTW is making software available on www.allapi.net. I have also read Jeffrey
Richter's book on "Programming Applications for MS Windows".
Now, below, I have included the dll source code. I can convince myself that
the hook is installed because I can see the target window
is being maximized with the test SendMessage.
But execution of the target application halts when the subclassing procedure
is installed. ARGH!
Does anybody know what could be wrong with the code?
Best regards and thanks in advance,
Christian Loft
----snip-------snip-------snip-------snip-------snip-------snip-------snip--
-----snip-------snip---
#include <windows.h>
#include <stdio.h>
#define HOOK_MAIN
#include "subclass.h"
// Static data that's shared across all instances of the DLL
#pragma data_seg("SHARED")
static HHOOK g_hHook = NULL;
static HWND g_hwnd = NULL;
#ifdef STRICT
static WNDPROC g_oldwinproc = NULL;
#else
static FARPROC g_oldwinproc = NULL;
#endif
#pragma data_seg()
#pragma comment(linker, "/Section:SHARED, rws")
// Static data that's unique to each instance of the DLL
static HINSTANCE g_hinstDLL;
LRESULT CALLBACK SubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM
lParam)
{
LRESULT ret;
// TEST
if ( (uMsg == WM_SYSCOMMAND) && (wParam == SC_MINIMIZE) )
{
uMsg = WM_SYSCOMMAND;
wParam = SC_MAXIMIZE;
};
//END OF TEST
if (uMsg == WM_CLOSE) uMsg = 0; // muligvis skal testes om hwnd==g_hwnd
ret = CallWindowProc( (WNDPROC) &g_oldwinproc, hWnd, uMsg, wParam,lParam );
return (ret);
Quote:
}
// CallWnd message hook proc
LRESULT CALLBACK GetMsgProc(int uMsg, WPARAM wParam, LPARAM lParam)
{
static BOOL fFirstTime = TRUE;
LRESULT ret;
if (fFirstTime) {
// The DLL just got injected.
fFirstTime = FALSE;
//TEST
SendMessage(g_hwnd,WM_SYSCOMMAND, SC_MAXIMIZE,0);
//END OF TEST
(LONG) g_oldwinproc = GetWindowLong( g_hwnd, GWL_WNDPROC );
SetWindowLong( g_hwnd, GWL_WNDPROC, (LONG) &SubclassProc );
}
// Call the next hook
ret = CallNextHookEx(g_hHook,uMsg,wParam,lParam);
return(ret);
Quote:
}
// Main DLL entry point
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpvReserved)
{
g_hinstDLL = hinstDLL;
if(dwReason == DLL_PROCESS_ATTACH)
{ // Attaching new process
}
else if(dwReason == DLL_PROCESS_DETACH)
{ // Detaching process }
}
return TRUE;
Quote:
}
// Set the sender window to recieve information
void SetTargetWindow(HWND hwnd)
{
g_hwnd = hwnd;
Quote:
}
void RestoreWinProc()
{
if(g_oldwinproc) SetWindowLong( g_hwnd, GWL_WNDPROC, (LONG)
&g_oldwinproc );
g_oldwinproc = NULL;
Quote:
}
// Reset the hook chain
void RemoveHook()
{
RestoreWinProc();
// Uninstall the hook
if(g_hHook) UnhookWindowsHookEx(g_hHook);
// Reset the handle
g_hHook = NULL;
Quote:
}
// Set the hook
int SetHook()
{
// Set the hook
g_hHook = SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC) &GetMsgProc,
g_hinstDLL,0);
SendMessage(g_hwnd,WM_NULL,0,0); // Provoke the hook to install the
subclass
// Return the result
return(g_hHook != NULL);
Quote:
}
----done-------done-------done-------done-------done-------done-------done--
-----done-------done-------