//代碼複製到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;
}