简单的 C++ 程序无法作为计划任务运行(交互式/非交互式问题?) [英] Simple c++ program fails to run as a scheduled-task (interactive/non-interactive issue?)

查看:77
本文介绍了简单的 C++ 程序无法作为计划任务运行(交互式/非交互式问题?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 Win 7 x64,我用 Microsoft Visual c++ 2010 express 编写了一个非常简单的 c++ 程序,作为任务调度程序中的任务运行.这是程序代码(没有资源文件或头文件):

I'm running Win 7 x64 and I've written a very simple c++ program with Microsoft Visual c++ 2010 express, to be run as a task in task scheduler. this is the programs code (there's no resource files or header files):

#include <windows.h>

int WINAPI WinMain(HINSTANCE inst,HINSTANCE prev,LPSTR cmd,int show)
{
      // Simulate numlock key press
         keybd_event( VK_NUMLOCK,
                      0x45,
                      KEYEVENTF_EXTENDEDKEY | 0,
                      0 );

      // Simulate numlock key release
         keybd_event( VK_NUMLOCK,
                      0x45,
                      KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
                      0);
      return 0;
}

我希望任务以 SYSTEM 帐户运行,不依赖于任何用户凭据或他们是否登录.但是,我无法使其作为任务调度程序中的任务成功运行.在做了一些研究之后,我现在怀疑作为 SYSTEM 运行的任务不能进行交互的事实是该程序无法正常工作的原因(通过使用 PsExec 工具,我可以确认 'PsExec -s -i my_program.exe' 有效,而 'PsExec -s my_program.exe' 无法更改 numlock 状态.).

I would like the task to be run as SYSTEM account to not rely on any users credentials or whether they are logged in or not. However, I am unable to make it successfully run as a task in task scheduler. after doing some research, i now suspect that the fact that tasks running as SYSTEM, can not be interactive, is the cause of this program not working correctly ( by using PsExec tool, i can confirm that 'PsExec -s -i my_program.exe' works, while 'PsExec -s my_program.exe' fails to change the numlock state.).

据我所知,该程序似乎根本没有交互性.谁能帮我弄清楚为什么会发生这种情况以及如何解决?提前致谢

From what i can see, the program doesn't appear to be interactive at all. Can anyone please help me to figure out why is this happening and how it can be fixed? Thanks in advance

推荐答案

如果我可以回答我自己的问题,这似乎是 Windows 的某种安全措施,以避免从用户桌面之外接收键盘/鼠标事件(请参阅此处.

If i may answer my own question, This, appears to be some sort of security measure by windows to avoid receiving keyboard/mouse events from outside of users desktop (See here).

这篇关于简单的 C++ 程序无法作为计划任务运行(交互式/非交互式问题?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆