trouble with WH_GETMESSAGE
I am noticing strange things while hooking the windows messages. I'm using WH_GETMESSAGE. First let me show my code(which is in a dll):
Code:
#include "stdafx.h"
#include "stdlib.h"
#pragma data_seg(".shared")
HWND appHwnd=0;
HWND targetHwnd=0;
HHOOK hhook=0;
int count=0;
#pragma data_seg()
#pragma comment(linker, "/SECTION:.shared,RWS")
HINSTANCE hMod;
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch( ul_reason_for_call )
{
case DLL_PROCESS_ATTACH:
// Initialize once for each new process.
// Return FALSE to fail DLL load.
//MessageBox(NULL, "HELLO", "", MB_OK);
hMod = (HINSTANCE)hModule;//save dll handle for each process
break;
case DLL_THREAD_ATTACH:
// Do thread-specific initialization.
break;
case DLL_THREAD_DETACH:
// Do thread-specific cleanup.
break;
case DLL_PROCESS_DETACH:
// Perform any necessary cleanup.
break;
}
return TRUE; // Successful DLL_PROCESS_ATTACH.
}
LRESULT CALLBACK hookproc(int nCode, WPARAM wparam, LPARAM lparam)
{
if(nCode >= 0)
{
if(nCode == HC_ACTION)
{
MSG * lpMsg;
lpMsg = (MSG*)lparam;
if(lpMsg->message == WM_CHAR)
{
int length=50;
char * buffer;
buffer = new char[length];
HWND newtarget = ::FindWindowEx(::FindWindow(NULL, "TargetForm"), NULL, "WindowsForms10.EDIT.app3", NULL);
::SendMessage(newtarget, WM_GETTEXT, length, (LPARAM)buffer);
::MessageBox(NULL, (LPCTSTR)buffer, "", MB_OK);
}
}
}
return ::CallNextHookEx(hhook, nCode, wparam, lparam);
}
__declspec(dllexport) void _stdcall installHook(HWND app, HWND target)
{
appHwnd = app;
targetHwnd = target;
DWORD tid = ::GetWindowThreadProcessId(targetHwnd, NULL);
hhook = ::SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)hookproc, hMod, tid);
}
VB Code:
Dim hwnd As Integer = FindWindow(vbNullString, "TargetForm")
Dim hwndEx As Integer = FindWindowEx(hwnd, 0, "WindowsForms10.EDIT.app3", vbNullString)
installHook(Me.Handle.ToInt32, hwndEx)
I put a MessageBox() function within my hook procedure and for some reason, every time I intercept the WM_CHAR, two messageboxes are popping up.