Я попробовал приведенный ниже пример. Обратите внимание, что это было создано с использованием ИИ и множества вещей, которые я еще не до конца понимаю. Если у кого-нибудь есть информация о том, как это возможно в C++, буду благодарен за любую помощь.
Код: Выделить всё
#include
#include
#include
#include
#include
#pragma comment(lib, "taskschd.lib")
#pragma comment(lib, "comsupp.lib")
std::wstring GetCurrentTimePlusSeconds(int seconds) {
// Get current time
time_t now = time(0);
tm localTime;
localtime_s(&localTime, &now); // Use localtime_s instead of localtime
// Add seconds
now += seconds;
localtime_s(&localTime, &now); // Update localTime with the new time
// Format time to ISO 8601 (YYYY-MM-DDTHH:MM:SS)
wchar_t buffer[100];
std::wcsftime(buffer, sizeof(buffer) / sizeof(wchar_t), L"%Y-%m-%dT%H:%M:%S", &localTime);
return buffer;
}
int main() {
// Initialize COM
CoInitializeEx(NULL, COINIT_MULTITHREADED);
CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
ITaskService* pService = NULL;
HRESULT hr = CoCreateInstance(CLSID_TaskScheduler, NULL, CLSCTX_INPROC_SERVER,
IID_ITaskService, (void**)&pService);
if (SUCCEEDED(hr)) {
// Connect to the Task Scheduler service
pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
// Create a new task
ITaskDefinition* pTask = NULL;
pService->NewTask(0, &pTask);
// Set up task details
IRegistrationInfo* pRegInfo = NULL;
pTask->get_RegistrationInfo(&pRegInfo);
pRegInfo->put_Author(_bstr_t(L"SYSTEM"));
// Create a trigger for the task
ITriggerCollection* pTriggerCollection = NULL;
pTask->get_Triggers(&pTriggerCollection);
ITrigger* pTrigger = NULL;
pTriggerCollection->Create(TASK_TRIGGER_TIME, &pTrigger);
ITimeTrigger* pTimeTrigger = NULL;
pTrigger->QueryInterface(IID_ITimeTrigger, (void**)&pTimeTrigger);
// Set trigger time (current time + 3 seconds)
std::wstring startTime = GetCurrentTimePlusSeconds(3);
pTimeTrigger->put_StartBoundary(_bstr_t(startTime.c_str()));
// Set up action
IActionCollection* pActionCollection = NULL;
pTask->get_Actions(&pActionCollection);
IAction* pAction = NULL;
pActionCollection->Create(TASK_ACTION_EXEC, &pAction);
IExecAction* pExecAction = NULL;
pAction->QueryInterface(IID_IExecAction, (void**)&pExecAction);
// Set executable path
pExecAction->put_Path(_bstr_t(L"C:\\Windows\\notepad.exe")); // Change this to your executable path
// Get the root folder to register the task
ITaskFolder* pRootFolder = NULL; // Declare ITaskFolder
hr = pService->GetFolder(_bstr_t(L"\\"), &pRootFolder); // Get root folder
if (SUCCEEDED(hr)) {
BSTR bstrTaskName = SysAllocString(L"X");
hr = pRootFolder->RegisterTaskDefinition(bstrTaskName,
pTask,
TASK_CREATE_OR_UPDATE,
_variant_t(),
_variant_t(),
TASK_LOGON_SERVICE_ACCOUNT ,
_variant_t(),
nullptr);
if (SUCCEEDED(hr)) {
std::wcout Release();
if (pTimeTrigger) pTimeTrigger->Release();
if (pTrigger) pTrigger->Release();
if (pTriggerCollection) pTriggerCollection->Release();
if (pRegInfo) pRegInfo->Release();
if (pTask) pTask->Release();
if (pService) pService->Release();
}
else {
std::cerr
Подробнее здесь: [url]https://stackoverflow.com/questions/79124517/having-trouble-finding-ways-of-running-my-application-as-system-account[/url]