01-06-2011 01:16 PM
Hi,
I am writing a LabWindows program that will backup data. I will be adding this to my windows Scheduled Tasks, but I also want it to be able to detect if someone manually tries to execute it and open up an "Admin Panel".
So, is there a direct way to detect how the file was ran, whether scheduled or manually ran?
Or, is there a way to feed my program a "1" to tell it whether it was ran by a batch file and if it doesn't detect that "1" then it opens the admin panel? I suppose if there isn't the direct way, I would create a scheduled task that runs a batch file, puts a "1" in a text file, then runs the program. Then the program automatically checks upon running whether that "1" is there...? After program is ran, the "1" is removed again.
Thanks in advance!
Turbo
Solved! Go to Solution.
01-06-2011 03:52 PM
Turbo:
One way to do this would be to use a command line parameter.
When you set up the scheduled task, pass a command line parameter, e.g. /Quiet, to notify your program to run without the admin panel. You can add a command line parameter simply by going to the scheduled task list, right-clicking on your task, selecting properties, and adding the command line parameter after your exe file name (separated by a space). When a user runs it interactively, e.g. from a desktop shortcut or from the start menu, they won't use the command line parameter so the program won't run in the quiet mode.
Read the command line like this:
#include <utility.h>
#include <ansi_c.h>
int main (int argc, char *argv[])
{
int i;
int quiet = 0;
for (i=1; i<argc; i++)
// print parameters only if /Quiet was not passed
if (stricmp(argv[i], "/Quiet") == 0)
quiet = 1;
if (!quiet)
{
printf("Number of command line parameters: %d\n", argc);
printf("Parameters:\n");
for (i=0; i<argc; i++)
printf("%s\n", argv[i]);
printf("Press any key to continue...\n");
GetKey();
}
return 0;
}
The shortcoming of this method is that if they run the program from the scheduled task list, it will run in the quiet mode.
Another thing you could try is to use schtasks.exe, which ships with Windows XP, to get a list of the sceduled tasks, find your task within the list, check what time it is scheduled to run, check the current time, and assume that the task is run as scheduled, and not manually, if the current time is within a short time period of the scheduled time.
Use the system() function to run schtasks.exe, saving the output to a file, then open the file and read it.
system("cmd.exe /c schtasks.exe /query /fo list> c:\\temp\\tasklist.txt");
Using the list format option for a schtasks query, you would first look for the TaskName, and then for the Next Run Time.
To see command line options for querying the scheduled tasks using schtasks.exe, enter the following from the command line:
schtasks /query /?
01-07-2011 12:31 PM
Thanks for the help!