为什么挂钩不能用于WH_KEYBOARD之类的高级钩子,虽然它与WH_KEYBOARD_LL一起工作正常,但是我能做些什么来成功实现WH_KEYBOARD钩子呢? [英] why hooking doesn't work with high level hooks such as WH_KEYBOARD , although it works fine with WH_KEYBOARD_LL , what can I do to implement WH_KEYBOARD hook successfully ?

查看:81
本文介绍了为什么挂钩不能用于WH_KEYBOARD之类的高级钩子,虽然它与WH_KEYBOARD_LL一起工作正常,但是我能做些什么来成功实现WH_KEYBOARD钩子呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是dll的代码



  //   dllmain.cpp:定义DLL应用程序的入口点。 
#include < span class =code-preprocessor> stdafx.h
#include dll.h

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

HINSTANCE hinst = NULL;



BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved

{
FILE *文件;
fopen_s(& file, c:\\Users \\keyur \\\ \\\\\\\\\\ a +);
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
fprintf(file, dll attach \\\
);
break ;
case DLL_THREAD_ATTACH:
fprintf(file, thread attach \\\
);
break ;
case DLL_THREAD_DETACH:
fprintf(file, thread detach \\\
);
break ;
case DLL_PROCESS_DETACH:
fprintf(file, dll detach \\\
);
break ;
}
return TRUE;
}


__ declspec dllexport )BOOL WINAPI setMyHook(HWND hWnd)
{
if (hWndServer!= NULL)
返回错误;
hhook = SetWindowsHookEx(
WH_KEYBOARD, // 这是问题
(HOOKPROC)myhook,
hinst,
0 );
if (hhook!= NULL)
{ / * success * /
hWndServer = hWnd;
return TRUE;
} / * 成功* /
返回错误;
}


静态 LRESULT CALLBACK myhook( int ncode,WPARAM wparam,LPARAM lparam)
{
FILE * file;
fopen_s(& file, c:\\Users \\keyur \\\ \\ _doctop \\hook.txt a +);
fprintf(file, keystroked \\\
);

fclose(档案);
return CallNextHookEx(hhook,ncode,wparam,lparam);
}







这里是我的cpp文件,请忽略代码中的MyLowLevelHook



  #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)缓冲区,20);
// _ strlwr(buffer);

FILE * file;
fopen_s(& file, c:\\Users \\keyur \\\ \\ _doctop \\hook.txt a +);
fprintf(file, keystroked \\\
);

fclose(档案);
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为null);
getchar();
}

while (!GetMessage(& msg,NULL,NULL,NULL)){
TranslateMessage( &安培; MSG);
DispatchMessage(& msg);
}

UnhookWindowsHookEx(hhook);
}

解决方案

我的猜测是,原因是你没有窗口你的申请。请参阅KeyboardProc回调文档



KeyboardProc回调
[ ^ ]。

它说只有当应用程序调用GetMessage或PeekMessage时才会调用钩子函数功能,并有一个键盘消息(WM_KEYUP或WM_KEYDOWN)待处理。



在您的情况下,没有窗口,因此没有消息要处理。



相反,低级事件在没有有效消息队列的情况下工作;每当生成键盘事件时它们都会被触发。



分辨率:在主函数中打开一个窗口,让该窗口获得焦点,然后一切都应该正常工作。

here is the code for dll

// 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

#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);
} 

解决方案

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.


这篇关于为什么挂钩不能用于WH_KEYBOARD之类的高级钩子,虽然它与WH_KEYBOARD_LL一起工作正常,但是我能做些什么来成功实现WH_KEYBOARD钩子呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆