Windows Service中使用SendInput无效

scutth 2013-05-09 10:08:05
RT,我想用SendInput模拟键盘和鼠标事件,在Desktop Application中使用没有任何问题,但是在Service中调用SendInput没有任何效果,函数返回结果却是正常的。Service中的code如下:

void CSampleService::ServiceWorkerThread(void)
{
DWORD error;
BOOL retVal;
DWORD res;

HWINSTA hWinsta = OpenWindowStation(_T("WinSta0"),FALSE,MAXIMUM_ALLOWED);
retVal = SetProcessWindowStation(hWinsta);

/*HDESK hDesk = OpenDesktop(_T("Default"),0,FALSE,MAXIMUM_ALLOWED);*/
HDESK hDesk = OpenInputDesktop(0,FALSE,MAXIMUM_ALLOWED);
retVal = SetThreadDesktop(hDesk);

// Periodically check if the service is stopping.
while (!m_fStopping)
{
// Perform main service function here...
INPUT kbdinput[2] = {0};

kbdinput[0].type = INPUT_KEYBOARD;
kbdinput[0].ki.wVk = VK_SPACE;

res = SendInput(1,kbdinput,sizeof(INPUT));
if(res != 1)
{
error = GetLastError();
}

kbdinput[1].type = INPUT_KEYBOARD;
kbdinput[1].ki.wVk = VK_SPACE;
kbdinput[1].ki.dwFlags = KEYEVENTF_KEYUP;
res = SendInput(1,kbdinput+1,sizeof(INPUT));
if(res != 1)
{
error = GetLastError();
}

::Sleep(2000); // Simulate some lengthy operations.
}

// Signal the stopped event.
SetEvent(m_hStoppedEvent);

CloseDesktop(hDesk);
CloseWindowStation(hWinsta);

}

这个Service是参考MSDN上的一个Sample
http://code.msdn.microsoft.com/windowsdesktop/CppWindowsService-cacf4948

任何想法都欢迎 :)
...全文
268 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
youyou1912 2013-05-09
  • 打赏
  • 举报
回复
引用 2 楼 scutth 的回复:
[quote=引用 1 楼 youyou1912 的回复:] session 0 隔离
如何实现呢? MSDN上有一段: Important All services run in Terminal Services session 0. Therefore, if an interactive service displays a user interface, it is visible only to the user who connected to session 0. Because there is no way to guarantee that the interactive user is connected to session 0, do not configure a service to run as an interactive service under Terminal Services or on a system that supports fast user switching (fast user switching is implemented using Terminal Services). 但是没有明白怎么做。要把Interactive Service属性去掉么?[/quote] 在用户session里面起一个进程, 把相关逻辑放到这个进程里面做?
scutth 2013-05-09
  • 打赏
  • 举报
回复
引用 1 楼 youyou1912 的回复:
session 0 隔离
如何实现呢? MSDN上有一段: Important All services run in Terminal Services session 0. Therefore, if an interactive service displays a user interface, it is visible only to the user who connected to session 0. Because there is no way to guarantee that the interactive user is connected to session 0, do not configure a service to run as an interactive service under Terminal Services or on a system that supports fast user switching (fast user switching is implemented using Terminal Services). 但是没有明白怎么做。要把Interactive Service属性去掉么?
youyou1912 2013-05-09
  • 打赏
  • 举报
回复
session 0 隔离
scutth 2013-05-09
  • 打赏
  • 举报
回复
引用 3 楼 youyou1912 的回复:
session 0 隔离
如何实现呢? MSDN上有一段: Important All services run in Terminal Services session 0.[/quote] 在用户session里面起一个进程, 把相关逻辑放到这个进程里面做?[/quote] 这不行。我们的需求是根据底层硬件(比如传感器)发上来的数据模拟鼠标或者键盘的事件,再发送给应用程序。 这就要求应用程序在收到消息时就已经是鼠标或者键盘的事件了。

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧