Windows服务中可以使用C ++ SendInput吗?

问题描述:

首先,我想说我已经使用*多年了,这是我第一次找不到现有答案.证明这是什么宝贵的资源.

First off, I'd like to say that I've used * for years, and this is the first time I haven't found an existing answer. A testament to what a valuable resource this is.

背景: 我正在尝试从已连接的设备捕获MIDI输入,并使其触发键盘事件到任何具有焦点的窗口.我能够在Windows控制台应用程序中使用SendInput成功完成此操作,但希望将其作为服务运行.我能够隐藏控制台窗口并使之运行,这是一个可行的解决方案,但我希望它在隐藏之前一秒钟都不会看到控制台,从而使其能够正常工作.

Background: I'm trying to catch MIDI input from a connected device and have it trigger keyboard events to whichever window has focus. I was able to do this successfully using SendInput in a Windows Console application, but would prefer to run this as a service. I was able to hide the console window and have it work, which is a passable solution, but I would prefer to get it working without seeing the console for a second before it hides.

问题: 在对制作Windows服务进行了一些研究之后,主要使用以下资源:

The problem: After doing some research on making a Windows Service, primarily using the following sources:

MSDN Windows服务教程

CodeProject Windows服务教程

给我的印象是,可能存在一些安全限制,阻止SendInput在Windows Service中工作(这是可以理解的).我决定先尝试概念验证,然后再进一步研究是否可以使它起作用.我从MSDN运行了示例代码,并能够按照本教程中的说明进行安装和工作.这样做之后,我将以下代码添加到类SampleService.cpp中的示例中:

I got the impression that there might be security restrictions that prevent SendInput from working in a Windows Service (and understandably so). I decided to try a proof of concept first before getting too far to see if I could get it to work. I ran the sample code from MSDN and was able to get it to install and work as described in the tutorial. After doing so, I added the following code to their example in the class SampleService.cpp:

    while (!m_fStopping)
{
    // Perform main service function here...
    INPUT keyEvents;
    keyEvents.type = INPUT_KEYBOARD;
    keyEvents.ki.wScan = 0;
    keyEvents.ki.time = 0;
    keyEvents.ki.dwExtraInfo = 0;

    keyEvents.ki.wVk = 0x41;
    keyEvents.ki.dwFlags = 0;
    SendInput(1, &keyEvents, sizeof(INPUT));
    ::Sleep(5000);  // Simulate some lengthy operations.
}

我没有更改任何其他代码.安装该服务后,我发现该服务已列在服务"中,在启动该服务之前,我进入属性",进入登录"选项卡,并将登录"设置为本地系统帐户",并选中了允许服务与桌面交互".运行该服务时,在任何接受文本的窗口中我都看不到小写字母a.

I did not change any other code. After I installed the service, I found it listed in Services, and before starting it, I went into Properties, to the Logon tab, and set Logon to Local System Account and checked Allow Service to Interact with Desktop. When running the service I don't see the lower case a I expect in any window that accepts text.

是否可以在服务中使用SendInput?如果是这样,怎么办?如果没有,还有其他方法让服务将键事件发送到任何活动窗口吗?

Is it possible to use SendInput in a service? If so, how? If not, is there another way to have a service send key events to any active window?

我正在使用Windows 8 64bit,IDE是VS2012 Professional.

I'm using Windows 8 64bit, IDE is VS2012 Professional.

无法通过Windows Services与用户进行任何交互.出于安全原因,服务隔离运行.它们在单独的会话中运行,会话0 . 因此,您尝试执行的操作将无法正常运行,并且是有原因的.

Its not possible to conduct any interaction with the user from Windows Services. Services run isolated for security reasons. They run in a separate session, session 0. So what you are trying to do, won’t work and for a reason.