1

I wrote a simple program that check if a program run (e.g. Notepad). The compiled program should start in the Windows Task-Scheduler. Problem is, when I run my program normal, it works. When I run in Task-Scheduler it works too when I use the option: "Only run if the user is logged in". But when I use the option: "Execute independently of the user login" it doesn't work. My program can'tt find anything running programs (my program run, I can see in Task-Manager).

My Code:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void write_status(int state);
int isRunning(LPCSTR test);

int ret_val = 0;

int main(void)
{
    while(1)
    {
        if(kbhit()) if(getch() == ' ') return 0;
        Sleep(500);
        ret_val = isRunning("Unbenannt - Editor");
        printf("STATUS: %i\r", ret_val);
        write_status(ret_val);
    }
}

void write_status(int state)
{
    FILE *fp = fopen("V:/ARAMAM.txt", "w");
    
    if(state)
    fprintf(fp, "ACTIVE    ");
    else
    fprintf(fp, "NOT ACTIVE");
    
    fclose(fp);
}

int isRunning(LPCSTR test)
{
    HWND hwnd;
    hwnd = FindWindowA(NULL, test);
    if (hwnd != 0) {
        return 1;
    }
    else {
        return 0;
    }
}

In the dont working case no console window shows. For this case i wrote the output in a external file. The writing in file works. Only find the running program fail.

My settings in Task-Scheduler, it's in German, sorry: IMAGE

Can somebody help?

Umbrecht

Steve Friedl
  • 3,929
  • 1
  • 23
  • 30
Umbrecht
  • 35
  • 6
  • According to statements made in [this question](https://stackoverflow.com/questions/14642610/how-to-know-if-the-process-is-running-from-service), since Windows XP, processes running as a service can no longer use `FindWindow` to detect other programs. – Andreas Wenzel May 26 '21 at 20:28
  • 1
    Instead of using `FindWindow`, you may want to try using [`EnumProcesses`](https://learn.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumprocesses) to see whether `notepad.exe` is running. – Andreas Wenzel May 26 '21 at 20:34

0 Answers0