#include #include #pragma comment(lib, "taskschd.lib") int __cdecl wmain(int argc, wchar_t** argv) { // ------------------------------------------------------ // Initialize COM. HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); if( FAILED(hr) ) { printf("\nCoInitializeEx failed: %x", hr ); return 1; } // Set general COM security levels. hr = CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, 0, NULL); if( FAILED(hr) ) { printf("\nCoInitializeSecurity failed: %x", hr ); CoUninitialize(); return 1; } // ------------------------------------------------------ // Create a name for the task. BSTR wszTaskName = SysAllocString(L"SessionIdSids"); // Get the windows directory and set the path to notepad.exe. BSTR wszExecutablePath = SysAllocString(L"F:\\Dev-Cpp\\Projects\\Test\\Adjustor\\Release\\InitDll.exe"); //BSTR wszExecutablePath = SysAllocString(L"f:\\Dev-Cpp\\Projects\\Test\\Adjustor\\InitDll\\initdll.exe"); // ------------------------------------------------------ // Create an instance of the Task Service. ITaskService *pService = NULL; hr = CoCreateInstance( CLSID_TaskScheduler, NULL, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService ); if (FAILED(hr)) { printf("Failed to create an instance of ITaskService: %x", hr); CoUninitialize(); return 1; } VARIANT vtEmpty; VariantInit(&vtEmpty); // Connect to the task service. hr = pService->Connect(vtEmpty, vtEmpty, vtEmpty, vtEmpty); if( FAILED(hr) ) { printf("ITaskService::Connect failed: %x", hr ); pService->Release(); CoUninitialize(); return 1; } // ------------------------------------------------------ // Get the pointer to the root task folder. This folder will hold the // new task that is registered. ITaskFolder *pRootFolder = NULL; BSTR root = SysAllocString(L"\\"); hr = pService->GetFolder(root, &pRootFolder ); SysFreeString(root); if( FAILED(hr) ) { printf("Cannot get Root Folder pointer: %x", hr ); pService->Release(); CoUninitialize(); return 1; } // If the same task exists, remove it. pRootFolder->DeleteTask(wszTaskName, 0 ); // Create the task builder object to create the task. ITaskDefinition *pTask = NULL; hr = pService->NewTask( 0, &pTask ); pService->Release(); // COM clean up. Pointer is no longer used. if (FAILED(hr)) { printf("Failed to create a task definition: %x", hr); pRootFolder->Release(); CoUninitialize(); return 1; } // ------------------------------------------------------ // Get the registration info for setting the identification. IRegistrationInfo *pRegInfo= NULL; hr = pTask->get_RegistrationInfo( &pRegInfo ); if( FAILED(hr) ) { printf("\nCannot get identification pointer: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } hr = pRegInfo->put_Author(L"me"); pRegInfo->Release(); if( FAILED(hr) ) { printf("\nCannot put identification info: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } // ------------------------------------------------------ // Create the settings for the task ITaskSettings *pSettings = NULL; hr = pTask->get_Settings( &pSettings ); if( FAILED(hr) ) { printf("\nCannot get settings pointer: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } // Set setting values for the task. hr = pSettings->put_AllowDemandStart(VARIANT_TRUE); pSettings->Release(); if( FAILED(hr) ) { printf("\nCannot put setting info: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } // ------------------------------------------------------ // Add an Action to the task. This task will execute notepad.exe. IActionCollection *pActionCollection = NULL; // Get the task action collection pointer. hr = pTask->get_Actions( &pActionCollection ); if( FAILED(hr) ) { printf("\nCannot get Task collection pointer: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } // Create the action, specifying that it is an executable action. IAction *pAction = NULL; hr = pActionCollection->Create(TASK_ACTION_EXEC, &pAction); pActionCollection->Release(); if( FAILED(hr) ) { printf("\nCannot create the action: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } IExecAction *pExecAction = NULL; // QI for the executable task pointer. hr = pAction->QueryInterface( IID_IExecAction, (void**) &pExecAction ); pAction->Release(); if( FAILED(hr) ) { printf("\nQueryInterface call failed for IExecAction: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } // Set the path of the executable to notepad.exe. hr = pExecAction->put_Path(wszExecutablePath); SysFreeString(wszExecutablePath); pExecAction->Release(); if( FAILED(hr) ) { printf("\nCannot set path of executable: %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } // ------------------------------------------------------ // Save the task in the root folder. IRegisteredTask *pRegisteredTask = NULL; BSTR wszUser = SysAllocString(L"Adrian"); VARIANT vtUser; VariantInit(&vtUser); V_VT(&vtUser) = VT_BSTR; V_BSTR(&vtUser) = wszUser; hr = pRootFolder->RegisterTaskDefinition( wszTaskName, pTask, TASK_CREATE_OR_UPDATE, vtEmpty, vtEmpty, TASK_LOGON_INTERACTIVE_TOKEN, vtEmpty, &pRegisteredTask); VariantClear(&vtUser); if( FAILED(hr) ) { printf("\nError saving the Task : %x", hr ); pRootFolder->Release(); pTask->Release(); CoUninitialize(); return 1; } pTask->Release(); PWTS_SESSION_INFO pSessions = NULL; DWORD numSessions = 0; if(WTSEnumerateSessions(WTS_CURRENT_SERVER, 0, 1, &pSessions, &numSessions)) { for(DWORD i = 0; i < numSessions; ++i) { const DWORD& dwSessionId = pSessions[i].SessionId; IRunningTask* pRunningTask = NULL; hr = pRegisteredTask->RunEx(vtEmpty, TASK_RUN_USE_SESSION_ID, dwSessionId, NULL, &pRunningTask); if(FAILED(hr) || (!pRunningTask)) { printf("\nError running the Task for session %d: %x", dwSessionId, hr); } else { printf("\nTask ran for session %d", dwSessionId); pRunningTask->Release(); } } WTSFreeMemory(pSessions); } else { IRunningTask* pRunningTask = NULL; hr = pRegisteredTask->RunEx(vtEmpty, TASK_RUN_USE_SESSION_ID, 1, NULL, &pRunningTask); if(FAILED(hr) || (!pRunningTask)) { printf("\nError running the Task : %x", hr ); pRegisteredTask->Release(); pRootFolder->Release(); CoUninitialize(); return 1; } pRunningTask->Release(); } pRegisteredTask->Release(); pRootFolder->DeleteTask(wszTaskName, 0); pRootFolder->Release(); SysFreeString(wszTaskName); puts("\nFinished\n"); // Clean up CoUninitialize(); return 0; }