Add or delete a Task Scheduler

Source: Internet
Author: User

# 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

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.