dave2k
November 20th, 2005, 01:30 PM
hi guys, i have a class mehod which tries to set a hook from my dll, but is throwing an invalid hook procedure error. What is the most likely cause of this?
my calling code and dll code is below
calling:bool Window::SetHook()
{
//SetCurrentDirectory(".");
// Find the child RICHEDIT control and return its handle
HWND hwndCntrl = ::FindWindowEx(hwnd, NULL, "RICHEDIT", NULL);
if(!::IsWindow(hwndCntrl)) return false;
// Load HookProc.dll
hinstDLL = LoadLibrary((LPCTSTR)"HP.dll");
//if(hinstDLL == NULL) return false;
HOOKPROC hkprcSysMsg = (HOOKPROC)GetProcAddress(hinstDLL, "CallWndProc");
// Set the hook
DWORD ThreadIdCntrl = GetWindowThreadProcessId(hwndCntrl, 0);
hh = ::SetWindowsHookEx(WH_CALLWNDPROC, hkprcSysMsg, hinstDLL, ThreadIdCntrl);
Util::ReportLastError();
return true;
}
hook procedure(dll):// HProc.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
//extern "C" __declspec(dllexport) LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam);
CWPSTRUCT *lpMsg;
LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
{
MessageBeep(0);
if(nCode < 0) return CallNextHookEx(0, nCode, wParam, lParam);
char* test = "dave";
lpMsg =(CWPSTRUCT *)lParam;
COPYDATASTRUCT cds = {0};
/* size of message */
cds.cbData = 200;
cds.dwData = 0;
cds.lpData = (char*)lpMsg->lParam;
HWND hGetLines = FindWindow(NULL, "GetLines");
//SendMessage(hGetLines, WM_COPYDATA, (WPARAM)lpMsg->hwnd, (LPARAM)&cds);
switch(lpMsg->message)
{
case EM_REPLACESEL:
MessageBeep(0);
SendMessage(hGetLines, WM_COPYDATA, (WPARAM)lpMsg->hwnd, (LPARAM)&cds);
//SendNotifyMessage(hGetLines, WM_COPYDATA, (WPARAM)lpMsg->hwnd, (LPARAM)&cds);
break;
default:
break;
}
return CallNextHookEx(0, nCode, wParam, lParam);
}
cheers
my calling code and dll code is below
calling:bool Window::SetHook()
{
//SetCurrentDirectory(".");
// Find the child RICHEDIT control and return its handle
HWND hwndCntrl = ::FindWindowEx(hwnd, NULL, "RICHEDIT", NULL);
if(!::IsWindow(hwndCntrl)) return false;
// Load HookProc.dll
hinstDLL = LoadLibrary((LPCTSTR)"HP.dll");
//if(hinstDLL == NULL) return false;
HOOKPROC hkprcSysMsg = (HOOKPROC)GetProcAddress(hinstDLL, "CallWndProc");
// Set the hook
DWORD ThreadIdCntrl = GetWindowThreadProcessId(hwndCntrl, 0);
hh = ::SetWindowsHookEx(WH_CALLWNDPROC, hkprcSysMsg, hinstDLL, ThreadIdCntrl);
Util::ReportLastError();
return true;
}
hook procedure(dll):// HProc.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
//extern "C" __declspec(dllexport) LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam);
CWPSTRUCT *lpMsg;
LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
{
MessageBeep(0);
if(nCode < 0) return CallNextHookEx(0, nCode, wParam, lParam);
char* test = "dave";
lpMsg =(CWPSTRUCT *)lParam;
COPYDATASTRUCT cds = {0};
/* size of message */
cds.cbData = 200;
cds.dwData = 0;
cds.lpData = (char*)lpMsg->lParam;
HWND hGetLines = FindWindow(NULL, "GetLines");
//SendMessage(hGetLines, WM_COPYDATA, (WPARAM)lpMsg->hwnd, (LPARAM)&cds);
switch(lpMsg->message)
{
case EM_REPLACESEL:
MessageBeep(0);
SendMessage(hGetLines, WM_COPYDATA, (WPARAM)lpMsg->hwnd, (LPARAM)&cds);
//SendNotifyMessage(hGetLines, WM_COPYDATA, (WPARAM)lpMsg->hwnd, (LPARAM)&cds);
break;
default:
break;
}
return CallNextHookEx(0, nCode, wParam, lParam);
}
cheers