// JP opened flex table

Click to See Complete Forum and Search --> : Hooking into programs in callback methods


flynny1st
April 26th, 2007, 08:29 AM
Hi,

I've created a system wide hook which i want to sit there until another program i want is opened. When this happens i want to hook into it. Is this possible? As at the moment my code doesnt seem to be working and the second hook is not being created.

The first system wide hook is working ok and getting called however, the second hook keeps returning zero. Heres the Callback method for the system wide hook.



LRESULT CALLBACK LauncherEventsHook (int nCode, int wParam, long lParam)
{
if ( nCode == HC_ACTION )
{
LPCWPSTRUCT pCwp = (LPCWPSTRUCT) lParam;

if(pCwp->message == WM_CREATE)
{
fHWnd = FindWindow(NULL, "Calculator");

if(fHWnd > 0 && found == false)
{

if( fHHook == NULL) //if hook not created then remove system wide hook and hook into window
{
fHInst = FindWindowHInstance(fHWnd);
fProcID = FindWindowProcess(fHWnd);

HOOKPROC fhkprc;
static HINSTANCE hinstDLL;

hinstDLL = LoadLibrary((LPCTSTR) ".\\LaunchDLL.dll");
fhkprc = (HOOKPROC)GetProcAddress(hinstDLL, "?FoundWndHookProc@@YGJHHJ@Z");

fHHook = (HHOOK)SetWindowsHookEx(WH_CALLWNDPROC, fhkprc, fHInst, fProcID);

//char buf[128];
//sprintf(buf, "CREATE NEW HOOK %d, fHwnd %d, fHInst %d, fProcID %d, hinstDLL %d, fhkprc %d", fHHook, fHWnd, fHInst, fProcID, hinstDLL, fhkprc);
//MessageBox(NULL, buf, NULL, 0);

}

found = true;
}
}
}

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


any ideas?

Matt.

Krishnaa
April 26th, 2007, 09:14 AM
What do you want to hook in that target process ?

flynny1st
April 26th, 2007, 11:09 AM
Hi,

thanks for the reply, I want to hook into a program specified by the user. so the user will set a global variable, say appName, and then the system wide event will sit there until this program is opened, triggering a hook into this program and removing the system wide hook.

I've managed to hook into the second program now using the following code (im using calculator as a test)


HHOOK__* SysHook = NULL;

DllExport BOOL WINAPI StartSystemHook(void)
{
//Start the system wide hook
if(SysHook == NULL)
{
HOOKPROC hkprc;
static HINSTANCE hinstDLL;

hinstDLL = LoadLibrary((LPCTSTR) ".\\LaunchDLL.dll");
hkprc = (HOOKPROC)GetProcAddress(hinstDLL, "?LauncherEventsHook@@YGJHHJ@Z");
SysHook = SetWindowsHookEx(WH_CALLWNDPROC,hkprc,hinstDLL,0);

char buf[128];
sprintf(buf, "The System wide hook is %d, hinstDLL %d, hkprc %d", SysHook, hinstDLL, hkprc);
MessageBox(NULL, buf, NULL, 0);

return TRUE;
}

return FALSE;
}

BOOL found = false; //set to true after we find the window

LRESULT CALLBACK LauncherEventsHook (int nCode, int wParam, long lParam)
{
if ( nCode == HC_ACTION )
{
LPCWPSTRUCT pCwp = (LPCWPSTRUCT) lParam;

if(pCwp->message == WM_CREATE)
{

fHWnd = FindWindow(NULL, "Calculator");


if(fHWnd > 0 && found == false)
{

if( fHHook == NULL) //if hook not created then remove system wide hook and hook into window
{
fProcID = FindWindowProcess(fHWnd);
fHHook = (HHOOK)SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)FoundWndHookProc, 0, fProcID);
BOOL unhooked = UnhookWindowsHookEx(SysHook);

char buf[128];
sprintf(buf, "unhooked syshook %d unhooked %d", SysHook, unhooked);
MessageBox(NULL, buf, NULL, 0);
}

found = true;
}
}
}

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



firstly is it possible to set global variables that the callback methods can see?

as the problem i'm having now is removing the global hook as SysHook is 0

Thanks Matt.

//JP added flex table