1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Detecting WM_CREATE and hooking into window

Discussion in 'MFC' started by flynny1st, Apr 25, 2007.

  1. flynny1st

    flynny1st New Member

    Apr 25, 2007
    Likes Received:
    Trophy Points:

    In my program i've created a system wide hook that will sit there polling until it detects WM_CREATE (this is so i can be sure the program im trying to hook into exists.) I then want to hook into this window, remove the system wide hook and add a hook into this window.

    MY systemwide hook is working ok and i can hook detect the WM_CREATE no problem.

    however where do i go from there? i've tried doing a FindWindow(NULL, "NAME"); and then testing if a hook is returned if it is remove the hook and hook into the findwindow one. However this crashes my system for some reason.

    Is there any reason for this. My program is a console application which is using a DLL i created holding all the callback functions, etc.

    Heres the code

    	//hook into all events
    	HOOKPROC hkprcSysMsg; 
    	static HINSTANCE hinstDLL; 
    	static HHOOK hhookSysMsg; 
    	hinstDLL = LoadLibrary((LPCTSTR) ".\\LaunchDLL.dll"); 
    	hkprcSysMsg = (HOOKPROC)GetProcAddress(hinstDLL, "?LauncherEventsHook@@YGJHHJ@Z");		
    	hhookSysMsg = SetWindowsHookEx(WH_CALLWNDPROC,hkprcSysMsg,hinstDLL,0); 
    	char buf[128];
    	sprintf(buf, "hinstDLL %d, hhookSysMsg %d, hkprcSysMsg %d", hinstDLL, hkprcSysMsg, hkprcSysMsg);
    	MessageBox(NULL, buf, NULL, 0);
    the DLL code for LauncherEventsHook is this

    LRESULT CALLBACK LauncherEventsHook (int nCode, int wParam, long lParam)
    	LRESULT Result = CallNextHookEx(Hook, nCode, wParam, lParam);
    	if(nCode == HC_ACTION)	
    		tagCWPSTRUCT* eStruct = (tagCWPSTRUCT*) lParam;
    		//check if window is being activated and check if can hook into relevent window
    		if(eStruct->message == WM_CREATE)
    			foundHWnd = FindWindow(NULL, "Calculator");
    			if(foundHWnd > 0)
    				char buf[128];
    				sprintf(buf, "IN HERE SO CREATE NEW HOOK");
    				MessageBox(NULL, buf, NULL, 0);
    	return Result;
    thanks, matt.

Share This Page