全局键盘挂钩不起作用 [英] Global keyboard Hook not working

查看:109
本文介绍了全局键盘挂钩不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在Visual C ++中制作Global Keyboard钩子程序,该程序将击键写入文件"log.txt"..我是Windows编程的新手,我通过了msdn库来了解钩子. ...我认为我从理论上理解了这个概念,但是当我实现代码时,它似乎没有用..编译器在DLL文件和EXE文件中均未显示任何错误....文本文件"log.txt"永远不会创建...
这是代码文件

首先是DLL文件:

I have been trying to make Global Keyboard hook program in visual C++ that writes keystrokes to a file "log.txt"..I am new to windows programming and i have gone through the msdn library to get an understanding of hooks....I think i have understood the concept theoretically but when i implement the code, it doesn''t seem towork..The compiler doesn''t show any error in both the DLL file and EXE file....Moreover the text file "log.txt" never gets created...
Here are the code files

First the DLL file:

#include <windows.h>
#include <stdio.h>

HHOOK g_hhk;

__declspec(dllexport) LRESULT CALLBACK KeyProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	if(nCode>=0)
	{
		char ch;
		FILE *fp;
		fp=fopen("log.txt","a");
	if((wParam==VK_SPACE)||(wParam==VK_RETURN)||(wParam>=0x2f ) &&(wParam<=0x100))
	{
	if(wParam==VK_RETURN)
		ch='\n';
		fwrite(&ch,1,1,fp);
	}
	else
	{
		BYTE ks[256];
		GetKeyboardState(ks);

		WORD w;

		UINT scan;

		scan=0;

		ToAscii(wParam,scan,ks,&w,0);

		ch =char(w);

        fwrite(&ch,1,1,fp);  // copy character to log file
	}
	fclose(fp);
	}
return CallNextHookEx(g_hhk, nCode, wParam, lParam);
}




现在是EXE文件:




Now the EXE file:

#include <windows.h>

HOOKPROC hkprckb;
static HINSTANCE hinstDLL; 
static HHOOK hhookkb;

int WINAPI WinMain(HINSTANCE hInstance1,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
	hinstDLL=LoadLibrary(TEXT("C:\\Documents and Settings\\Attar Singh\\My Documents\\Visual Studio 2008\\Projects\\key\\Debug\\key.dll"));
	hkprckb=(HOOKPROC)GetProcAddress(hinstDLL,"KeyProc");
	hhookkb=SetWindowsHookEx( 
                    WH_KEYBOARD_LL,
                    hkprckb,
                    hinstDLL,
                    0); 


	
	MessageBox(NULL,NULL,NULL,MB_OK);
	return 1;
}



该程序给我带来噩梦...任何帮助将不胜感激...在此先感谢... !!



This program is giving me nightmares...Any sort of help will be greatly appreciated...thanks in advance...!!

推荐答案

您需要存储共享数据段中的HHOOK变量.
使用#data_seg指令创建它.
看看我的文章-鼠标!滚动并停放 [ ^ ]
You need to store the HHOOK variable in a shared data segment.
Create it using the #data_seg directive.
Take a look at my article - Mousey! Roll Over and Park[^]


尝试一下:
Try this:
////////////////////
// DLL

HHOOK        __hhook = 0;

LRESULT FAR PASCAL KeyProc(int nCode, WPARAM wParam, LPARAM lParam)
{
  // do the things you want
  return CallNextHookEx(__hhook,nCode,wParam,lParam);
}

int FAR PASCAL DllMain(HANDLE hModule,unsigned long r,void* p)
{
  switch(r)
  {
    case DLL_PROCESS_ATTACH:
      __hhook = SetWindowsHookEx( WH_KEYBOARD_LL,KeyProc,hModule,0); 
    break;
    case DLL_PROCESS_DETACH:
      UnhookWindowsHookEx(__hhook);
    break;
    case DLL_THREAD_ATTACH: break;
    case DLL_THREAD_DETACH: break;
  }
  return 1;
}



////////////////////
// EXE
int FAR PASCAL WinMain(HINSTANCE h,HINSTANCE p,LPSTR c,int sw)
{
  HINSTANCE  hdll = LoadLibrary(TEXT("key.dll"));
  MessageBox(0,__TEXT("wait..."),__TEXT("keyhook"),MB_OK);
  FreeLibrary(hdll);
  return 1;
}


您没有要导出的内容,仅加载DLL.该函数将注入到每个进程,并调用DllMain.但是请记住,挂钩将一直保持到窗口关闭为止.否则,您应该发送一个信号(即事件对象或特殊的按键)来取消和终止.
祝你好运.


You have nothing to export only load the DLL. The function is injected to every process and DllMain is called. But remember the hook will stay until windows shut down. Otherwise you should send a signal (i.e. an event object or a special keystoke) to unkook and terminate.
Good luck.


这篇关于全局键盘挂钩不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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