我定义的全局钩子,安装函数如下:
DllExport void WINAPI InstallLaunchEv() 

Hook=(HHOOK)SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)LauncherHook, theApp.m_hInstance,0); 

钩子函数如下:
LRESULT CALLBACK LowLevelKeyboardProc (INT nCode, WPARAM wParam, LPARAM lParam)
{
    // By returning a non-zero value from the hook procedure, the
    // message does not get passed to the target window
    KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *)lParam;
    BOOL bControlKeyDown = 0;    switch (nCode)
    {
        case HC_ACTION:
        {
            // Check to see if the CTRL key is pressed
            bControlKeyDown = GetAsyncKeyState (VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1);
            
            // Disable CTRL+ESC
            if (pkbhs->vkCode == VK_ESCAPE && bControlKeyDown)
                return 1;            // Disable ALT+TAB
            if (pkbhs->vkCode == VK_TAB && pkbhs->flags & LLKHF_ALTDOWN)
                return 1;            // Disable ALT+ESC
            if (pkbhs->vkCode == VK_ESCAPE && pkbhs->flags & LLKHF_ALTDOWN)
                return 1;            break;
        }        default:
            break;
    }
    return CallNextHookEx (Hook, nCode, wParam, lParam);
}
编译出现一下问题:
error C2065: 'WH_KEYBOARD_LL' : undeclared identifier
 error C2065: 'KBDLLHOOKSTRUCT' : undeclared identifier
error C2065: 'pkbhs' : undeclared identifier
error C2059: syntax error : ')'
error C2227: left of '->vkCode' must point to class/struct/union
error C2227: left of '->vkCode' must point to class/struct/union
error C2227: left of '->flags' must point to class/struct/union
error C2065: 'LLKHF_ALTDOWN' : undeclared identifier
error C2227: left of '->vkCode' must point to class/struct/union
error C2227: left of '->flags' must point to class/struct/union
Error executing cl.exe.LaunchDLL.dll - 10 error(s), 0 warning(s)

解决方案 »

  1.   

    我也碰到过同样的问题,KBDLLHOOKSTRUCT和WH_KEYBOARD_LL是在winuser.h中定义的,它会检查当前windows版本号,只有在2000和某些NT4版本下才能有效,但我在2000下也碰到了同样问题,我通过改动winuser.h编译通过,不过不清楚这样会有什么害处
      

  2.   

    用WH_KEYBOARD不就行么?
    看看我的代码,是截获1、2、3健的
    CHookApp theApp;
    BOOL installhook()
    {
        hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,theApp.m_hInstance,0);
        return TRUE;
    }
    LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam)

        if(((DWORD)lParam&0x40000000) && (HC_ACTION==nCode))
        {
           switch(wParam) //键盘按键标识
            {
            case '1':sndPlaySound("D:\\WINDOWS\\Application Data\\Microsoft\\WELCOME\\WELCOM98.WAV",SND_ASYNC);break; //当数字键1被按下
             case '2':AfxMessageBox("2.wav");break;
            case '3':AfxMessageBox("3.wav");break;
            }
         }
         LRESULT RetVal = CallNextHookEx( hkb, nCode, wParam, lParam ); 
         return RetVal;
    }
      

  3.   

    ADD THIS
    #define _WIN32_WINNT  0x0500
      

  4.   

    我是想屏蔽掉系统键。所以没有采用KEYBOARD_HOOK.
    楼上的zhuwenzheng gg的方法,我用了,还是原来一样的错误。
      

  5.   

    加在winuser.h的第一行。
    出现了连接错误:
    rror LNK2001: unresolved external symbol "long __stdcall LauncherHook(int,unsigned int,long)" (?LauncherHook@@YGJHIJ@Z)
    Debug/LaunchDLL.dll : fatal error LNK1120: 1 unresolved externals
    加在dll的头文件的第一行,则出现与原来一样的问题。sign~~`
    好磨人啊!
      

  6.   

    加在 *.CPP的第一行
    还是原来的错误
      

  7.   

    可以在winuser.h中查找KBDLLHOOKSTRUCT,找到对应的结构后把前面的#if (_WIN32_WINNT >= 0x0400)挪后一点就行了,WH_KEYBOARD_LL和LLKHF_ALTDOWN可以用同样的方法,但改动它我不清楚会不会出问题,望考虑好了再这样用。
      

  8.   

    stdafx.h第一行还是afxstd.h第一行?记不得了
      

  9.   

    #ifndef WIN32_LEAN_AND_MEAN
    # define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
    #endif WIN32_LEAN_AND_MEAN