Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is the code for dll

C++
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "dll.h"

#pragma data_seg(".SHARE")
HWND hWndServer = NULL;
HHOOK hhook=NULL;
#pragma data_seg()
#pragma comment("linker, /section:.SHARE,rws")

HINSTANCE hinst=NULL;



BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	FILE *file;
	fopen_s(&file,"c:\\Users\\keyur\\desktop\\temp.txt","a+");	
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		fprintf(file," dll attach \n");
		break;
	case DLL_THREAD_ATTACH:
		fprintf(file," thread attach \n");
		break;
	case DLL_THREAD_DETACH:
		fprintf(file," thread detach \n");
		break;
	case DLL_PROCESS_DETACH:
		fprintf(file," dll detach \n");
		break;
	}
	return TRUE;
}


__declspec(dllexport) BOOL WINAPI setMyHook(HWND hWnd)
  {
   if(hWndServer != NULL)
      return FALSE;
   hhook = SetWindowsHookEx(
                           WH_KEYBOARD,//here is the problem
                           (HOOKPROC)myhook,
                           hinst,
                           0);
   if(hhook != NULL)
     { /* success */
      hWndServer = hWnd;
      return TRUE;
     } /* success */
   return FALSE;
  }


static LRESULT CALLBACK myhook(int ncode,WPARAM wparam,LPARAM lparam)
{
	FILE *file;
	fopen_s(&file,"c:\\Users\\keyur\\desktop\\hook.txt","a+");	
	fprintf(file," keystroked \n");
	
	fclose(file);
	return CallNextHookEx(hhook , ncode ,wparam , lparam);
}



and here is my cpp file , please ignore the MyLowLevelHook in the code

C++
#include <string.h>
#include "C:\Users\keyur\Desktop\keyur\HOOKING\dll\dll\dll.h"

HHOOK hhook = NULL;
HWND hwnd ;

LRESULT CALLBACK MyLowLevelHook ( int nCode , WPARAM wParam , LPARAM lParam)
{
	//GetKeyNameText(lParam,(LPWSTR)buffer,20);
	//_strlwr(buffer);
	
	FILE *file;
	fopen_s(&file,"c:\\Users\\keyur\\desktop\\hook.txt","a+");	
	fprintf(file," keystroked \n");
	
	fclose(file);
	return CallNextHookEx(hhook, nCode ,wParam , lParam);
}

int main()
{
    MSG msg;

	BOOL hook=setMyHook(hwnd);
	
	//hhook = SetWindowsHookEx(WH_MOUSE_LL, myhook , NULL,NULL);
	if(hook==FALSE)
	{
		printf("hook is null");
		getchar();
	}

    while(!GetMessage(&msg, NULL, NULL, NULL)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
	
    UnhookWindowsHookEx(hhook);
} 
Posted
Updated 25-Aug-14 21:44pm
v4
Comments
Richard MacCutchan 26-Aug-14 4:20am    
So what exactly happens?

1 solution

My guess would be that the reason is that you don't have a window in your application. See the documentation of KeyboardProc callback

KeyboardProc callback
[^].
There it says that the hook function is only called "whenever an application calls the GetMessage or PeekMessage function and there is a keyboard message (WM_KEYUP or WM_KEYDOWN) to be processed".

In your case there is no window, and hence no message to be processed.

In contrast, the low level events work without a valid message queue; they are fired whenever the keyboard event is being generated.

Resolution: Open a window in your main function, let that window get the focus and then everything should work fine.
 
Share this answer
 
Comments
Richard MacCutchan 26-Aug-14 5:37am    
+5, well spotted.
keyur chauhan 27-Aug-14 6:13am    
@nv3 , thanks man, it worked !! now i want to use CBT hook when a particular window (say Untitled Notepad) is created , if you can give me a little guidance that would be so helpful !!

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