|
|
我用VC 6 新建一个Win32 Application工程
然后编写如下代码,我想用HOOK技术拦截键盘输入的字符,然后存到一个文本文件里,但是可能我很多概念都不清楚,写的有问题。
存出来的不对啊,请高手指点一下:
// hHook.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "windows.h"
#include "windowsx.h"
#include "fstream.h"
#include "stdlib.h"
LRESULT WINAPI HookProc(int nCode,WPARAM wParam,LPARAM lParam);
HWND hHook;
HWND hWnd;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
hHook = (HWND)SetWindowsHookEx(WH_JOURNALRECORD,HookProc,hInstance,0);
//进入消息循环
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
ofstream fout("TEST.TXT");
PEVENTMSG pMsg = (PEVENTMSG)lParam;
if(pMsg->message = WM_KEYUP)
{
fout << (char)pMsg->paramH;
}
fout.close();
return CallNextHookEx((HHOOK)hHook,nCode,wParam,lParam);
}
//******************************************************************
//函数:WinProc()
//功能:处理主窗口消息
//******************************************************************
LRESULT CALLBACK WinProc( HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam )
{
switch( message )
{
case WM_KEYDOWN://击键消息
switch( wParam )
{
case VK_ESCAPE:
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
}
break;
case WM_DESTROY://退出消息
PostQuitMessage( 0 );
break;
}
//调用缺省消息处理过程
return DefWindowProc(hWnd, message, wParam, lParam);
}
|
|