От: | reider | ||
Дата: | 16.10.18 08:29 | ||
Оценка: |
bool CreateTask(LPCTSTR szTaskName, PCTSTR pszPath, PCTSTR pszArg)
{
ITaskService *pService;
ITaskFolder *pRootFolder;
ITaskDefinition *pTask;
IRegistrationInfo *pRegInfo;
IPrincipal *pPrincipal;
ITaskSettings *pSettings;
ITriggerCollection *pTriggerCollection;
ITrigger *pTrigger;
ILogonTrigger *pLogonTrigger;
IActionCollection *pActionCollection;
IAction *pAction;
IExecAction *pExecAction;
IRegisteredTask *pRegisteredTask;
VARIANT var = { VT_EMPTY };
bool triggersucc = false;
bool actionsucc = false;
bool succ = false;
HRESULT hr = S_OK;
hr = CoInitialize(NULL);
if (!SUCCEEDED(hr))
{
MessageBoxA(0, "1", 0, 0);
return false;
}
if (SUCCEEDED(CoCreateInstance(CLSID_TaskScheduler, NULL, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService)))
{
if (SUCCEEDED(pService->Connect(var, var, var, var)))
{
if (SUCCEEDED(pService->GetFolder(TEXT("\\"), &pRootFolder)))
{
if (SUCCEEDED(pService->NewTask(0, &pTask)))
{
if (SUCCEEDED(pTask->get_RegistrationInfo(&pRegInfo)))
{
pRegInfo->put_Author(L"SRVC_AUTHOR");
pRegInfo->Release();
}//end if
else
{
MessageBoxA(0, "Error 1", 0, 0);
}
/*
if (SUCCEEDED(pTask->get_Principal(&pPrincipal)))
{
//pPrincipal->put_RunLevel(TASK_RUNLEVEL_HIGHEST);
//pPrincipal->Release();
}//end if
else
{
MessageBoxA(0, "7", 0, 0);
}
*/
if (SUCCEEDED(pTask->get_Settings(&pSettings)))
{
pSettings->put_StartWhenAvailable(VARIANT_BOOL(true));
pSettings->put_DisallowStartIfOnBatteries(VARIANT_BOOL(false));
pSettings->put_StopIfGoingOnBatteries(VARIANT_BOOL(false));
pSettings->put_ExecutionTimeLimit(TEXT("PT0S"));
pSettings->Release();
}//end if
else
{
MessageBoxA(0, "Error 2", 0, 0);
}
if (SUCCEEDED(pTask->get_Triggers(&pTriggerCollection)))
{
if (SUCCEEDED(pTriggerCollection->Create(TASK_TRIGGER_LOGON, &pTrigger)))
{
if (SUCCEEDED(pTrigger->QueryInterface(IID_ILogonTrigger, (void**)&pLogonTrigger)))
{
if (SUCCEEDED(pLogonTrigger->put_Id(TEXT("AnyLogon"))) && SUCCEEDED(pLogonTrigger->put_UserId(NULL)))
{
triggersucc = true;
}//end if
else
{
MessageBoxA(0, "Error 3", 0, 0);
}
pLogonTrigger->Release();
}//end if
else
{
MessageBoxA(0, "Error 4", 0, 0);
}
pTrigger->Release();
}//end if
else
{
MessageBoxA(0, "Error 5", 0, 0);
}
pTriggerCollection->Release();
}//end if
else
{
MessageBoxA(0, "Error 6", 0, 0);
}
if (SUCCEEDED(pTask->get_Actions(&pActionCollection)))
{
if (SUCCEEDED(pActionCollection->Create(TASK_ACTION_EXEC, &pAction)))
{
if (SUCCEEDED(pAction->QueryInterface(IID_IExecAction, (void**)&pExecAction)))
{
if (SUCCEEDED(pExecAction->put_Path((BSTR)pszPath)) &&
SUCCEEDED(pExecAction->put_Arguments((BSTR)pszArg)))
{
actionsucc = true;
}//end if
else
{
MessageBoxA(0, "Error 7", 0, 0);
}
pExecAction->Release();
}//end if
else
{
MessageBoxA(0, "Error 8", 0, 0);
}
pAction->Release();
}//end if
else
{
MessageBoxA(0, "Error 9", 0, 0);
}
pActionCollection->Release();
}//end if
else
{
MessageBoxA(0, "Error 10", 0, 0);
}
if (triggersucc && actionsucc
&& SUCCEEDED(pRootFolder->RegisterTaskDefinition((BSTR)szTaskName, pTask, TASK_CREATE_OR_UPDATE, var,
var, TASK_LOGON_INTERACTIVE_TOKEN, var, &pRegisteredTask)))
{
succ = true;
pRegisteredTask->Release();
}//end if
else
{
if (!triggersucc)MessageBoxA(0, "TTP", 0, 0);
if (!actionsucc)MessageBoxA(0, "TTP2", 0, 0);
HRESULT hrr = pRootFolder->RegisterTaskDefinition((BSTR)szTaskName, pTask, TASK_CREATE_OR_UPDATE, var,
var, TASK_LOGON_INTERACTIVE_TOKEN, var, &pRegisteredTask);
DWORD AF = DWORD(hrr);
if (AF == /*E_ACCESS_DENIED*/0x80070005)
{
MessageBoxA(0, "ACCESS DENIED", 0, 0);
}
else
{
char TTP[128];
memset(TTP, 0, 128);
sprintf(TTP, "%x", AF);
MessageBoxA(0, TTP, 0, 0);
}
}
pTask->Release();
}//end if
else
{
MessageBoxA(0, "Error 11", 0, 0);
}
pRootFolder->Release();
}//end if
else
{
MessageBoxA(0, "Error 12", 0, 0);
}
}//end if
else
{
MessageBoxA(0, "Error 14", 0, 0);
}
pService->Release();
}//end if
else
{
MessageBoxA(0, "Error 15", 0, 0);
}
return succ;
}//end CreateTask
От: | VVV | ||
Дата: | 16.10.18 20:26 | ||
Оценка: |
The following code is incorrect:
BSTR MyBstr = L"I am a happy BSTR";
If you pass a wide-string literal as an argument to a COM function that is expecting a BSTR, the COM function behaves unexpectedly.