| 35 | |
| 36 | |
| 37 | int TaskSche::TaskAdd(LPCWSTR wszTaskName, wstring wstrTaskTime, wstring wstrProgram, wstring args) |
| 38 | { |
| 39 | // https://docs.microsoft.com/zh-cn/windows/win32/taskschd/time-trigger-example--c--- |
| 40 | // https://docs.microsoft.com/zh-cn/windows/win32/taskschd/daily-trigger-example--c--- |
| 41 | |
| 42 | setlocale(LC_ALL, ""); |
| 43 | |
| 44 | // ��ʼ��COM��� |
| 45 | HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); |
| 46 | if (FAILED(hr)) |
| 47 | { |
| 48 | printf("\nCoInitializeEx failed: %x", hr); |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | // ���������ȫ�ȼ� |
| 54 | hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, 0, NULL); |
| 55 | if (FAILED(hr)) |
| 56 | { |
| 57 | printf("\nCoInitializeSecurity failed: %x", hr); |
| 58 | CoUninitialize(); |
| 59 | return 1; |
| 60 | } |
| 61 | |
| 62 | // ���üƻ��������� |
| 63 | // LPCWSTR wszTaskName = L"StateGrid"; |
| 64 | wprintf(L"TaskName:%s\n", wszTaskName); |
| 65 | |
| 66 | // ����ִ��·�� |
| 67 | wstring wstrExePath = _wgetenv(_bstr_t(L"WINDIR")); // ��ȡ���ַ��Ļ������� |
| 68 | wstrExePath += L"\\SYSTEM32\\"; |
| 69 | wstrExePath += wstrProgram; |
| 70 | |
| 71 | |
| 72 | // ��������������� |
| 73 | // Link: https://docs.microsoft.com/en-us/windows/win32/api/taskschd/nn-taskschd-itaskservice |
| 74 | // https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cocreateinstance |
| 75 | ITaskService* pService = NULL; |
| 76 | hr = CoCreateInstance(CLSID_TaskScheduler, NULL, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService); |
| 77 | if (FAILED(hr)) |
| 78 | { |
| 79 | printf("Failed to create an instance of ITaskService: %x", hr); |
| 80 | CoUninitialize(); |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | // ����Ŀ�������ΪԶ�����ӻط����� https://docs.microsoft.com/en-us/windows/win32/api/taskschd/nf-taskschd-itaskservice-connect |
| 85 | hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t()); //Ĭ�ϱ��� |
| 86 | if (FAILED(hr)) |
| 87 | { |
| 88 | printf("ITaskService::Connect failed: %x", hr); |
| 89 | pService->Release(); |
| 90 | CoUninitialize(); |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | // ��ȡ�����ļ��в������д������� |