# Include <atlbase. h>
# Include <taskschd. h>
Void AddVistaTask () // create a scheduled task
{
CComPtr <ITaskService> service;
CComPtr <ITaskFolder> root_folder;
CComPtr <ITaskFolder> new_folder;
CComPtr <IRegisteredTask> new_task;
CComPtr <ITaskDefinition> task_def;
CComPtr <IActionCollection> actions;
CComPtr <IAction> act1;
CComPtr <ITriggerCollection> triggers;
CComPtr <ITrigger> trig1;
CComPtr <IPrincipal> principal;
// Create an instance
HRESULT hr = service. CoCreateInstance (_ uuidof (TaskScheduler ));
If (FAILED (hr ))
{
Return;
}
// Use the default user to connect to the Local Computer
Hr = service-> Connect (CComVariant (), // local computer
CComVariant (), // current user
CComVariant (), // current domain
CComVariant (); // no password
If (FAILED (hr ))
{
Return;
}
// Get the root task folder
Hr = service-> GetFolder (CComBSTR (L "\"), & root_folder );
If (FAILED (hr ))
{
Return;
}
// Open my task folder
Hr = root_folder-> GetFolder (CComBSTR (L "Feitian \ NetRockey4"), & new_folder );
If (FAILED (hr) // if the folder does not exist, create
{
Hr = root_folder-> CreateFolder (CComBSTR (L "Feitian \ NetRockey4 "),
CComVariant (), & new_folder); // use the default security descriptor
If (FAILED (hr ))
{
Return;
}
}
// Find the task named "RunAtOnce"
Hr = new_folder-> GetTask (CComBSTR (L "RunAtOnce"), & new_task );
If (FAILED (hr ))
{
// Create an empty task if it is not found
Hr = service-> NewTask (0, & task_def );
If (FAILED (hr ))
{
Return;
}
// Obtain the action set
Hr = task_def-> get_Actions (& actions );
If (FAILED (hr ))
{
Return;
}
// Create an action in the action set
Hr = actions-> Create (TASK_ACTION_EXEC, & act1 );
If (FAILED (hr ))
{
Return;
}
// Write the execution program to the action
CComQIPtr <IExecAction> exec_act (act1 );
WCHAR exe_path [400] = {0 };
GetModuleFileNameW (0, exe_path, 400 );
Hr = exec_act-> put_Path (CComBSTR (exe_path); // run the program
If (FAILED (hr ))
{
Return;
}
Hr = exec_act-> put_Arguments (CComBSTR (L "-ray"); // write the parameters of the execution program to the action
If (FAILED (hr ))
{
Return;
}
// Obtain the trigger set
Hr = task_def-> get_Triggers (& triggers );
If (FAILED (hr ))
{
Return;
}
// Create a trigger in the trigger set
Hr = triggers-> Create (TASK_TRIGGER_LOGON, & trig1); // triggered when the user starts
If (FAILED (hr ))
{
Return;
}
// Specify the highest permission
Hr = task_def-> get_Principal (& principal );
If (FAILED (hr ))
{
Return;
}
Hr = principal-> put_RunLevel (TASK_RUNLEVEL_HIGHEST );
If (FAILED (hr ))
{
Return;
}
// Add the task to the directory
Hr = new_folder-> RegisterTaskDefinition (CComBSTR (L "RunAtOnce"), // name of the new task
Task_def,
TASK_CREATE_OR_UPDATE,
CComVariant (), // user name
CComVariant (), // password
TASK_LOGON_INTERACTIVE_TOKEN,
CComVariant (), // sddl
& New_task );
If (FAILED (hr ))
{
Return;
}
}
Else // if the task is found, check whether the path is correct.
{
Else // if the task is found, check whether the path is correct.
{
// Get the task definition
Hr = new_task-> get_Definition (& task_def );
If (FAILED (hr ))
{
Return;
}
// Obtain the action set
Hr = task_def-> get_Actions (& actions );
If (FAILED (hr ))
{
Return;
}
// Get the action in the action set
Hr = actions-> get_Item (1, & act1 );
If (FAILED (hr ))
{
Return;
}
// Get the execution program in the action
CComQIPtr <IExecAction> exec_act (act1 );
CComBSTR exe_path2;
Hr = exec_act-> get_Path (& exe_path2 );
WCHAR exe_path [400] = {0 };
GetModuleFileNameW (0, exe_path, 400 );
CComBSTR exe_path3 (exe_path );
// Modify the path if the path is different.
If (exe_path3! = Exe_path2)
{
Hr = exec_act-> put_Path (exe_path3 );
If (FAILED (hr ))
{
Return;
}
// Modify the task
Hr = new_folder-> RegisterTaskDefinition (CComBSTR (L "RunAtOnce"), // name of the new task
Task_def,
TASK_CREATE_OR_UPDATE,
CComVariant (), // user name
CComVariant (), // password
TASK_LOGON_INTERACTIVE_TOKEN,
CComVariant (), // sddl
& New_task );
If (FAILED (hr ))
{
Return;
}
}
}
}
// Remove the scheduled task of Vista
Void RemoveVistaTask ()
{
CComPtr <ITaskService> service;
CComPtr <ITaskFolder> root_folder;
CComPtr <ITaskFolder> new_folder;
CComPtr <IRegisteredTask> new_task;
CComPtr <ITaskDefinition> task_def;
CComPtr <IActionCollection> actions;
CComPtr <IAction> act1;
CComPtr <ITriggerCollection> triggers;
CComPtr <ITrigger> trig1;
CComPtr <IPrincipal> principal;
// Create an instance
HRESULT hr = service. CoCreateInstance (_ uuidof (TaskScheduler ));
If (FAILED (hr ))
{
Return;
}
// Use the default user to connect to the Local Computer
Hr = service-> Connect (CComVariant (), // local computer
CComVariant (), // current user
CComVariant (), // current domain
CComVariant (); // no password
If (FAILED (hr ))
{
Return;
}
// Get the root task folder
Hr = service-> GetFolder (CComBSTR (L "\"), & root_folder );
If (FAILED (hr ))
{
Return;
}
// Open my task folder
Hr = root_folder-> GetFolder (CComBSTR (L "Feitian \ NetRockey4"), & new_folder );
If (FAILED (hr) // if the folder does not exist, create
{
Return;
}
// Locate the task
Hr = new_folder-> GetTask (CComBSTR (L "RunAtOnce"), & new_task );
If (FAILED (hr ))
{
Return;
}
// Delete a task
Hr = new_folder-> DeleteTask (CComBSTR (L "RunAtOnce"), 0 );
If (FAILED (hr ))
{
Return;
}
}
# Include <atlbase. h>
# Include <taskschd. h>
Void AddVistaTask () // create a scheduled task
{
CComPtr <ITaskService> service;
CComPtr <ITaskFolder> root_folder;
CComPtr <ITaskFolder> new_folder;
CComPtr <IRegisteredTask> new_task;
CComPtr <ITaskDefinition> task_def;
CComPtr <IActionCollection> actions;
CComPtr <IAction> act1;
CComPtr <ITriggerCollection> triggers;
CComPtr <ITrigger> trig1;
CComPtr <IPrincipal> principal;
// Create an instance
HRESULT hr = service. CoCreateInstance (_ uuidof (TaskScheduler ));
If (FAILED (hr ))
{
Return;
}
// Use the default user to connect to the Local Computer
Hr = service-> Connect (CComVariant (), // local computer
CComVariant (), // current user
CComVariant (), // current domain
CComVariant (); // no password
If (FAILED (hr ))
{
Return;
}
// Get the root task folder
Hr = service-> GetFolder (CComBSTR (L "\"), & root_folder );
If (FAILED (hr ))
{
Return;
}
// Open my task folder
Hr = root_folder-> GetFolder (CComBSTR (L "Feitian \ NetRockey4"), & new_folder );
If (FAILED (hr) // if the folder does not exist, create
{
Hr = root_folder-> CreateFolder (CComBSTR (L "Feitian \ NetRockey4 "),
CComVariant (), & new_folder); // use the default security descriptor
If (FAILED (hr ))
{
Return;
}
}
// Find the task named "RunAtOnce"
Hr = new_folder-> GetTask (CComBSTR (L "RunAtOnce"), & new_task );
If (FAILED (hr ))
{
// Create an empty task if it is not found
Hr = service-> NewTask (0, & task_def );
If (FAILED (hr ))
{
Return;
}
// Obtain the action set
Hr = task_def-> get_Actions (& actions );
If (FAILED (hr ))
{
Return;
}
// Create an action in the action set
Hr = actions-> Create (TASK_ACTION_EXEC, & act1 );
If (FAILED (hr ))
{
Return;
}
// Write the execution program to the action
CComQIPtr <IExecAction> exec_act (act1 );
WCHAR exe_path [400] = {0 };
GetModuleFileNameW (0, exe_path, 400 );
Hr = exec_act-> put_Path (CComBSTR (exe_path); // run the program
If (FAILED (hr ))
{
Return;
}
Hr = exec_act-> put_Arguments (CComBSTR (L "-ray"); // write the parameters of the execution program to the action
If (FAILED (hr ))
{
Return;
}
// Obtain the trigger set
Hr = task_def-> get_Triggers (& triggers );
If (FAILED (hr ))
{
Return;
}
// Create a trigger in the trigger set
Hr = triggers-> Create (TASK_TRIGGER_LOGON, & trig1); // triggered when the user starts
If (FAILED (hr ))
{
Return;
}
// Specify the highest permission
Hr = task_def-> get_Principal (& principal );
If (FAILED (hr ))
{
Return;
}
Hr = principal-> put_RunLevel (TASK_RUNLEVEL_HIGHEST );
If (FAILED (hr ))
{
Return;
}
// Add the task to the directory
Hr = new_folder-> RegisterTaskDefinition (CComBSTR (L "RunAtOnce"), // name of the new task
Task_def,
TASK_CREATE_OR_UPDATE,
CComVariant (), // user name
CComVariant (), // password
TASK_LOGON_INTERACTIVE_TOKEN,
CComVariant (), // sddl
& New_task );
If (FAILED (hr ))
{
Return;
}
}
Else // if the task is found, check whether the path is correct.
{
Else // if the task is found, check whether the path is correct.
{
// Get the task definition
Hr = new_task-> get_Definition (& task_def );
If (FAILED (hr ))
{
Return;
}
// Obtain the action set
Hr = task_def-> get_Actions (& actions );
If (FAILED (hr ))
{
Return;
}
// Get the action in the action set
Hr = actions-> get_Item (1, & act1 );
If (FAILED (hr ))
{
Return;
}
// Get the execution program in the action
CComQIPtr <IExecAction> exec_act (act1 );
CComBSTR exe_path2;
Hr = exec_act-> get_Path (& exe_path2 );
WCHAR exe_path [400] = {0 };
GetModuleFileNameW (0, exe_path, 400 );
CComBSTR exe_path3 (exe_path );
// Modify the path if the path is different.
If (exe_path3! = Exe_path2)
{
Hr = exec_act-> put_Path (exe_path3 );
If (FAILED (hr ))
{
Return;
}
// Modify the task
Hr = new_folder-> RegisterTaskDefinition (CComBSTR (L "RunAtOnce"), // name of the new task
Task_def,
TASK_CREATE_OR_UPDATE,
CComVariant (), // user name
CComVariant (), // password
TASK_LOGON_INTERACTIVE_TOKEN,
CComVariant (), // sddl
& New_task );
If (FAILED (hr ))
{
Return;
}
}
}
}
// Remove the scheduled task of Vista
Void RemoveVistaTask ()
{
CComPtr <ITaskService> service;
CComPtr <ITaskFolder> root_folder;
CComPtr <ITaskFolder> new_folder;
CComPtr <IRegisteredTask> new_task;
CComPtr <ITaskDefinition> task_def;
CComPtr <IActionCollection> actions;
CComPtr <IAction> act1;
CComPtr <ITriggerCollection> triggers;
CComPtr <ITrigger> trig1;
CComPtr <IPrincipal> principal;
// Create an instance
HRESULT hr = service. CoCreateInstance (_ uuidof (TaskScheduler ));
If (FAILED (hr ))
{
Return;
}
// Use the default user to connect to the Local Computer
Hr = service-> Connect (CComVariant (), // local computer
CComVariant (), // current user
CComVariant (), // current domain
CComVariant (); // no password
If (FAILED (hr ))
{
Return;
}
// Get the root task folder
Hr = service-> GetFolder (CComBSTR (L "\"), & root_folder );
If (FAILED (hr ))
{
Return;
}
// Open my task folder
Hr = root_folder-> GetFolder (CComBSTR (L "Feitian \ NetRockey4"), & new_folder );
If (FAILED (hr) // if the folder does not exist, create
{
Return;
}
// Locate the task
Hr = new_folder-> GetTask (CComBSTR (L "RunAtOnce"), & new_task );
If (FAILED (hr ))
{
Return;
}
// Delete a task
Hr = new_folder-> DeleteTask (CComBSTR (L "RunAtOnce"), 0 );
If (FAILED (hr ))
{
Return;
}
}