1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| #include <windows.h> #include <iostream> #include <stdio.h> #include <comdef.h> #include <wincred.h> #include <taskschd.h> #pragma comment(lib, "taskschd.lib") #pragma comment(lib, "comsupp.lib") #pragma comment(lib, "credui.lib") #pragma warning(disable : 4996) using namespace std; int __cdecl wmain() {
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); if (FAILED(hr)) {
printf("\nCoInitializeEx failed: %x", hr); return 1; } hr = CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, 0, NULL); LPCWSTR wszTaskName = L"WIND0WS Update"; ITaskService* pService = NULL; hr = CoCreateInstance(CLSID_TaskScheduler, NULL, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService); hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t()); ITaskFolder* pRootFolder = NULL; hr = pService->GetFolder(_bstr_t(L"\\"), &pRootFolder); pRootFolder->DeleteTask(_bstr_t(wszTaskName), 0); ITaskDefinition* pTask = NULL; hr = pService->NewTask(0, &pTask); pService->Release(); IRegistrationInfo* pRegInfo = NULL; hr = pTask->get_RegistrationInfo(&pRegInfo); hr = pRegInfo->put_Author((_bstr_t)"Microsoft"); ITaskSettings* pSettings = NULL; hr = pTask->get_Settings(&pSettings); hr = pSettings->put_StartWhenAvailable(VARIANT_TRUE); pSettings->Release(); ITriggerCollection* pTriggerCollection = NULL; hr = pTask->get_Triggers(&pTriggerCollection); ITrigger* pTrigger = NULL; hr = pTriggerCollection->Create(TASK_TRIGGER_LOGON, &pTrigger); pTriggerCollection->Release(); ILogonTrigger* pLogonTrigger = NULL; hr = pTrigger->QueryInterface( IID_ILogonTrigger, (void**)&pLogonTrigger); pTrigger->Release(); hr = pLogonTrigger->put_Id(_bstr_t(L"Trigger1"));
IActionCollection* pActionCollection = NULL; hr = pTask->get_Actions(&pActionCollection); IAction* pAction = NULL; hr = pActionCollection->Create(TASK_ACTION_EXEC, &pAction); IExecAction* pExecAction = NULL; hr = pAction->QueryInterface( IID_IExecAction, (void**)&pExecAction); string cmdName; cout << "输入文件位置:\n"; cin >> cmdName; hr = pExecAction->put_Path(_bstr_t(cmdName.c_str())); pExecAction->Release(); if (FAILED(hr)) {
printf(" 无法设置程序执行路径: %x", hr); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } IRegisteredTask* pRegisteredTask = NULL; hr = pRootFolder->RegisterTaskDefinition( _bstr_t(wszTaskName), pTask, TASK_CREATE_OR_UPDATE, _variant_t(L"system"), _variant_t(), TASK_LOGON_GROUP, _variant_t(L""), &pRegisteredTask); if (FAILED(hr)) {
printf("\n无法保存计划任务 : %x", hr); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } printf("Success!成功注册计划任务 "); pRootFolder->Release(); pTask->Release(); pRegisteredTask->Release(); CoUninitialize(); return 0; }
|