| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1758 人关注过本帖
标题:不带DLL 实现全局钩子
只看楼主 加入收藏
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
结帖率:79.17%
收藏
 问题点数:0 回复次数:1 
不带DLL 实现全局钩子
#include "windows.h"
#include "stdio.h"

#define _WIN32_WINNT 0400

DWORD g_main_tid = 0;
HHOOK g_kb_hook = 0;

bool CALLBACK con_handler (DWORD)
{
    PostThreadMessage (g_main_tid, WM_QUIT, 0, 0);
    return TRUE;
};

LRESULT CALLBACK kb_proc (int code, WPARAM w, LPARAM l)
{
    PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)l;
    const char *info = NULL;
    if (w == WM_KEYDOWN)
        info = "key dn";
    else if (w == WM_KEYUP)
        info = "key up";
    else if (w == WM_SYSKEYDOWN)
        info = "sys key dn";
    else if (w == WM_SYSKEYUP)
        info = "sys key up";
    printf ("%s - vkCode [%04x], scanCode [%04x]\n",
        info, p->vkCode, p->scanCode);
    // always call next hook
    return CallNextHookEx (g_kb_hook, code, w, l);
};

int main (void)
{
    g_main_tid = GetCurrentThreadId ();
    SetConsoleCtrlHandler (&con_handler, TRUE);
    g_kb_hook = SetWindowsHookEx (
        WH_KEYBOARD_LL,
        &kb_proc,
        GetModuleHandle (NULL), // 不能为NULL,否则失败
        0);
    if (g_kb_hook == NULL)
    {
        fprintf (stderr,
            "SetWindowsHookEx failed with error %d\n",
            ::GetLastError ());
        return 0;
    };
    // 消息循环是必须的,想知道原因可以查msdn
    MSG msg;
    while (GetMessage (&msg, NULL, 0, 0))
    {
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    };
    UnhookWindowsHookEx (g_kb_hook);
    return 0;
};
搜索更多相关主题的帖子: DLL 全局钩子 
2008-10-25 16:16
快速回复:不带DLL 实现全局钩子
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016612 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved