# Include <windows. h>
# Include <initguid. h>
# Include <ole2.h>
# Include <mstask. h>
# Include <msterr. h>
# Include <wchar. h>
# Include <stdio. h>
//////////////////////////////////////// //////////////////////////////////
// Function name: deletetask
// Function: deletes a specified task scheduler.
// Parameter description: lpcwsztaskname: name of the task plan to be deleted
//////////////////////////////////////// //////////////////////////////////
Hresult deletetask (maid );
//////////////////////////////////////// //////////////////////////////////
// Function name: createtask
// Function: Create a New Task Scheduler
// Parameter description: lpcwsztaskname: name of the task scheduler to be created
// Lpcwszapplicationname: Application Path of the task scheduler to be created
//////////////////////////////////////// //////////////////////////////////
Hresult createtask (maid );
Int main (INT argc, char ** argv)
{
Hresult hR = s_ OK;
Lpcwstr lpcwsztaskname = l "test task ";
Lpcwstr lpcwszapplicationname = l "C: // windows // notepad.exe ";
// HR = createtask (lpcwsztaskname, lpcwszapplicationname); // create a new Task Scheduler
HR = deletetask (lpcwsztaskname); // delete a specified Task Scheduler
If (failed (HR ))
{
Wprintf (L "failed calling itask: Run, error = 0x % x/N", HR );
}
Return 0;
}
// Delete a specified scheduled task
Hresult deletetask (maid)
{
Hresult hR = s_ OK;
//////////////////////////////////////// ///////////////////////////
// Call coinitialize to initialize the com library and then
// Cocreateinstance to get the task scheduler object.
//////////////////////////////////////// ///////////////////////////
Itaskscheduler * pits;
HR = coinitialize (null );
If (succeeded (HR ))
{
HR = cocreateinstance (clsid_ctaskscheduler,
Null,
Clsctx_inproc_server,
Iid_itaskscheduler,
(Void **) & pits );
If (failed (HR ))
{
Couninitialize ();
Return 1;
}
}
Else
{
Return 1;
}
// Delete the specified Task Scheduler
HR = pits-> Delete (lpcwsztaskname );
If (failed (HR ))
{
// Wprintf (L "failed calling itask: Run, error = 0x % x/N", HR );
Couninitialize ();
Return hr;
}
Couninitialize ();
Return s_ OK;
}
Hresult createtask (maid, maid)
{
Hresult hR = s_ OK;
//////////////////////////////////////// ///////////////////////////
// Call coinitialize to initialize the com library and then
// Cocreateinstance to get the task scheduler object.
//////////////////////////////////////// ///////////////////////////
Itaskscheduler * pits;
HR = coinitialize (null );
If (succeeded (HR ))
{
HR = cocreateinstance (clsid_ctaskscheduler,
Null,
Clsctx_inproc_server,
Iid_itaskscheduler,
(Void **) & pits );
If (failed (HR ))
{
Couninitialize ();
Return 1;
}
}
Else
{
Return 1;
}
// Create a new blank scheduled task
Itask * pitask;
Ipersistfile * pipersistfile;
HR = pits-> newworkitem (lpcwsztaskname, // name of task
Clsid_ctask, // Class Identifier
Iid_itask, // interface identifier
(Iunknown **) & pitask); // address of task interface
If (failed (HR ))
{
Couninitialize ();
Return hr;
}
HR = pitask-> QueryInterface (iid_ipersistfile,
(Void **) & pipersistfile );
Pitask-> release ();
If (failed (HR ))
{
Couninitialize ();
Return hr;
}
HR = pipersistfile-> Save (null,
True );
Pipersistfile-> release ();
If (failed (HR ))
{
Couninitialize ();
Return hr;
}
// Specify the application path for the task scheduler created above
HR = pits-> activate (lpcwsztaskname,
Iid_itask,
(Iunknown **) & pitask );
// Release the itaskscheduler interface.
Pits-> release ();
If (failed (HR ))
{
Couninitialize ();
Return hr;
}
HR = pitask-> setapplicationname (lpcwszapplicationname );
If (failed (HR ))
{
Couninitialize ();
Return hr;
}
HR = pitask-> QueryInterface (iid_ipersistfile,
(Void **) & pipersistfile );
// Release the itask interface.
Pitask-> release ();
HR = pipersistfile-> Save (null,
True );
If (failed (HR ))
{
Couninitialize ();
Return hr;
}
// Release the ipersistfile interface.
Pipersistfile-> release ();
Couninitialize ();
Return hr;
}
// (Windows7) the code above (the 1.0 Interface) is created in the Windows/tasks directory. Delete. the job file is not added to the task plan of the control panel. Other steps are required (the complete example is not found). The following is the MS Code (the 2.0 interface) http://msdn.microsoft.com/en-us/library/aa381915 (V = vs.85 ). aspx