//代码复制到Classview里编译即可,VC6++ SDK编译成功
//这个代码执行了当时会启动程序,后面开机并不会启动,有点还没弄好。
#include "stdafx.h>
#include <windows.h>
#include <initguid.h>
#include <ole2.h>
#include <mstask.h>
#include <msterr.h>
#include <wchar.h>
#include <stdio.h>
#pragma comment(lib, "Mstask.lib")
#pragma comment(lib, "ole32.lib")
int main(int argc, char **argv)
{
HRESULT hr = S_OK;
ITaskScheduler *pITS;
///
// Call CoInitialize to initialize the COM library and then
// CoCreateInstance to get the Task Scheduler object.
///
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;
}
///
// Call ITaskScheduler::NewWorkItem to get a new Task object.
///
LPCWSTR pwszTaskName;
ITask *pITask;
pwszTaskName = L"Test Task";
hr = pITS->NewWorkItem(pwszTaskName,
CLSID_CTask,
IID_ITask,
(IUnknown**)&pITask);
if (FAILED(hr))
{
wprintf(L"Failed calling ITaskScheduler::NewWorkItem: ");
wprintf(L"error = 0x%x\n",hr);
CoUninitialize();
return 1;
}
///
// Set the tasks application name.
///
LPCWSTR pwszApplicationName = L"C:\\windows\\notepad.exe";
hr = pITask->SetApplicationName(pwszApplicationName);
if (FAILED(hr))
{
wprintf(L"Failed calling ITask::SetApplicationName: ");
wprintf(L"error = 0x%x\n",hr);
pITS->Release();
pITask->Release();
CoUninitialize();
return 1;
}
pITask->SetAccountInformation(L"ADMINISTRATOR", NULL);//ADMINITRATOR改为你的用户名
pITask->SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON);
pITask->SetWorkingDirectory(L"C:\\Windows"); //notepad的起始目录
pITask->SetParameters(L"");
///
// Add the task to the sceduler
///
pITS->AddWorkItem(pwszTaskName, pITask);
pITS->Release(); // Release sceduler
///
// Call ITask::Run to start execution of "Test Task".
///
hr = pITask->Run();
if (FAILED(hr))
{
wprintf(L"Failed calling ITask::Run, error = 0x%x\n",hr);
pITask->Release();
CoUninitialize();
return 1;
}
pITask->Release();
CoUninitialize();
return 0;
}