I wrote a small code in C, and want to try to run it through Schtasks. It started, but a winapi function, which requires administrator rights, did not work.
For cmd
, I use this prompt (I launched it with administrator rights):
schtasks /create /ru SYSTEM /tn TestTask /tr "C:\Users\Koalko\Desktop\main.exe" /sc ONSTART
I also tried to add my profile to the Administrators group, but that didn't change anything:
net localgroup Administrators Koalko /add
My C code:
#include <stdio.h>
#include <Windows.h>
int main(int argc, char* argv[]) {
for (int i = 0; i <= 5; i++) {
printf("\rBLOCK INPUT AFTER %d SECONDS", 5-i);
Sleep(1000);
}
printf("\nBLOCK INPUT ON 5 SECONDS!!!\n");
BlockInput(TRUE);
Sleep(5000);
BlockInput(FALSE);
printf("RELEASE INPUT\n");
getchar();
return 0;
}
I don't think there is a problem in the code.
Thank a lot to IInspectable and Remy Lebeau.
The issue was that the program was running in a non-interactive session, therefore BlockInput
could not be executed.
After reviewing the schtasks
documentation, I found the /IT
attribute which allows tasks to run in an interactive session.
It is important to note that this attribute does not work with the SYSTEM
user and is not executed with /sc ONSTART
.
schtasks /create /ru Koalko /tn TestTask /tr "C:\Users\Koalko\Desktop\main.exe" /sc ONLOGON /RL HIGHEST /IT /f