我在做键盘钩子程序时碰到这样的问题:
在钩子回调函数中进行参数转换时发现在winnt 中没问题,但在win98中编译时没事,但运行却总出错。发现问题出现在参数转换上:
大致过程是这样的:
下钩子:
BOOL __declspec(dllexport)__stdcall installhook()
{
if(IsNT)
hkb=SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)  KeyboardProc,hins,0);
else
hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hins,0);
return TRUE;
}
这没问题
钩子回调函数:
LRESULT __declspec(dllexport)__stdcall  CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam)
{ BOOL fEatKeystroke = FALSE;
PKBDLLHOOKSTRUCT p;
p = (PKBDLLHOOKSTRUCT) lParam;
         BOOL bControlKeyDown = 0;
if(nCode<0)
return CallNextHookEx(NULL, nCode, wParam,lParam);

switch (nCode)
    {
case HC_ACTION:
{
            
// Check to see if the CTRL key is pressed
bControlKeyDown = GetAsyncKeyState (VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1); //Disable Esc
if (p->vkCode == VK_ESCAPE)  // && p->flags & LLKHF_ALTDOWN)
return 1;

if (p->vkCode == VK_F1)  // && p->flags & LLKHF_ALTDOWN)
return 1;
// Disable CTRL+ESC
if (p->vkCode == VK_ESCAPE && bControlKeyDown)

return 1;

// Disable ALT+TAB

if (p->vkCode == VK_TAB && p->flags & LLKHF_ALTDOWN)
return 1;


//disable ALT+F4  (ZOU)
if (p->vkCode == VK_F4 && p->flags & LLKHF_ALTDOWN)
return 1;

// Disable ALT+ESC
if (p->vkCode == VK_ESCAPE && p->flags & LLKHF_ALTDOWN)
return 1;
if (p->vkCode == VK_LWIN)
return 1;
//disable ALT+CTL+DELETE
if (p->vkCode == VK_DELETE && LLKHF_ALTDOWN&& bControlKeyDown)
{
RestartComputer(2);
return 1;
}
}
default:
break;
}
    return CallNextHookEx(NULL, nCode, wParam,lParam);
}
钩子回调函数在NT下运行很好,但在98底下出错,发现问题出现在
p = (PKBDLLHOOKSTRUCT) lParam;  转换没成功,但在后边却对指针进行引用,我不明白为什么转换不成功,我应该如何作才能转换成功?

解决方案 »

  1.   

    不是转换不成功,而是lParam在win98下的定义如下:
    lParam Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag. This parameter can be a combination of the following values: Value 
     Description 
     
    0-15 
     Specifies the repeat count. The value is the number of times the keystroke is repeated as a result of the user’s holding down the key. 
     
    16-23 
     Specifies the scan code. The value depends on the original equipment manufacturer (OEM). 
     
    24 
     Specifies whether the key is an extended key, such as a function key or a key on the numeric keypad. The value is 1 if the key is an extended key; otherwise, it is 0. 
     
    25-28 
     Reserved. 
     
    29 
     Specifies the context code. The value is 1 if the ALT key is down; otherwise, it is 0. 
     
    30 
     Specifies the previous key state. The value is 1 if the key is down before the message is sent; it is 0 if the key is up. 
     
    31 
     Specifies the transition state. The value is 0 if the key is being pressed and 1 if it is being released.