第一部分,添加高权限启动的进程
// Set up principal information:
hr = pPrincipal->put_Id(_bstr_t(L"Principal1"));
//if (FAILED(hr))
//DebugPrint(L"\nCannot put the principal ID: %x", hr);
hr = pPrincipal->put_LogonType(TASK_LOGON_INTERACTIVE_TOKEN);
//if (FAILED(hr))
//DebugPrint(L"\nCannot put principal logon type: %x", hr);
// Run the task with the least privileges (LUA)
hr = pPrincipal->put_RunLevel(TASK_RUNLEVEL_HIGHEST);//以最高权限
pPrincipal->Release();
if (FAILED(hr))
{
//DebugPrint(L"\nCannot put principal run level: %x", hr);
pRootFolder->Release();
pTask->Release();
//CoUninitialize();
return false;
}
第二部分,为所有用户添加触发器
//在触发器集合中创建触发器
hr = pTriggerCollection->Create(TASK_TRIGGER_LOGON, &pTrigger); //当用户启动时触发
if (FAILED(hr))
{
return false;
}
ILogonTrigger *pLogonTrigger = NULL;
hr = pTrigger->QueryInterface(
IID_ILogonTrigger, (void**)&pLogonTrigger);
pTrigger->Release();
if (FAILED(hr))
{
printf("\nQueryInterface call failed for ILogonTrigger: %x", hr);
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return 1;
}
hr = pLogonTrigger->put_Id(_bstr_t(L"Trigger1"));
if (FAILED(hr))
printf("\nCannot put the trigger ID: %x", hr);
// Set the task to start at a certain time. The time
// format should be YYYY-MM-DDTHH:MM:SS(+-)(timezone).
// For example, the start boundary below
// is January 1st 2005 at 12:05
hr = pLogonTrigger->put_StartBoundary(_bstr_t(L"2005-06-16T12:05:00"));
if (FAILED(hr))
printf("\nCannot put the start boundary: %x", hr);
hr = pLogonTrigger->put_EndBoundary(_bstr_t(L"2005-06-16T18:00:00"));
if (FAILED(hr))
printf("\nCannot put the end boundary: %x", hr);
// Define the user. The task will execute when the user logs on.
// The specified user must be a user on this computer.
hr = pLogonTrigger->put_UserId(NULL);
pLogonTrigger->Release();
if (FAILED(hr))
{
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return 1;
}
pTriggerCollection->Release();