Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
	hInst=(HINSTANCE)hModule;
	switch (ul_reason_for_call)
	{
		case DLL_PROCESS_ATTACH:
			::DisableThreadLibraryCalls((HMODULE)hModule);
			break;
		case DLL_THREAD_ATTACH:			
			break;
		case DLL_THREAD_DETACH:
			break;
		case DLL_PROCESS_DETACH:
			UnInstallKBHook();
			break;
			
    }
    return TRUE;
}

extern "C" __declspec(dllexport) void InstallKBHook()
{	
 	FILE *fp=fopen("D:\\KBHOOK.txt","a+");
	
	if(fp)
		fclose(fp);
	
	hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardHook,hInst,0);
	
}
int UnInstallKBHook()
{
	if(hkb)UnhookWindowsHookEx(hkb);
	return 1;
}



LRESULT __declspec(dllexport)__stdcall  CALLBACK KeyboardHook(int nCode,WPARAM wParam, LPARAM lParam)
{
	
	if (((DWORD)lParam & 0x40000000) &&(HC_ACTION==nCode))
	{
		FILE *fp=fopen("D:\\KBHOOK.txt","a+");
		if(fp)
		{
			fprintf(fp,"\n KeyPressed %c",(char)wParam);
			fclose(fp);
		}		
	}
	
	LRESULT RetVal = CallNextHookEx( hkb, nCode, wParam, lParam ); 
    return  RetVal;
}


What I have tried:

I am not seeing this issue on Windows 7 . If I remove the SetWindowsHookEx statement I am not seeing any issue

Do I need to enable any permission on windows 10 to use SetWindowsHookEx
Posted
Updated 25-Aug-19 2:44am
Comments
Richard MacCutchan 25-Aug-19 7:51am    
You have to be very careful using hooks as you are interrupting part of the normal processing. It may be that writing to a file at this point is causing the problem, and should be easy to eliminate.
Coder969 25-Aug-19 8:02am    
I have removed the file writing part and still seeing the same issue. Same code is working fine on windows 7

1 solution

I think your code is a little "thin".

Using Hooks - Windows applications | Microsoft Docs[^]
 
Share this answer
 
Comments
Coder969 25-Aug-19 8:51am    
Thanks for the response.I have gone through the link previously as well but I didnt find what causing the issue. Can you please explain what might be causing the issue

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900