hey guys i have written a code to retrive a task schedular information which displays the names of tasks and its states. the code works well. but the problem i am facing now is in windows server 2003 i found that my code is not running as ITaskService cannot be instantiated or it is not recognisable. hence forth i have written another code. what it does is it retrives the list of all task inside
C:\WINDOWS\Tasks
but now i want to print its states as well which is not getting displayed through my code. how must i retrive it?
here my code :
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <windows.h>
#include <windows.h>
#pragma warning (disable : 4996)
#include "dirent.h"
int main()
{
DIR *dirp;
struct dirent *ent;
if ((dirp = opendir("C:\\WINDOWS\\Tasks")) != NULL) {
while ((ent = readdir(dirp)) != NULL) {
printf("%s\n", ent->d_name);
}
closedir(dirp);
}
else {
perror("");
return EXIT_FAILURE;
}
}
along with this i also want to put the condition that for win server 2003 the above code should run where as for other OS's the following code must run:
TASK_STATE taskState;
void Taskschedular()
{
try
{
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hr))
{
printf("\nCoInitializeEx failed: %x", hr);
outputFile << "\nCoInitializeEx failed: %x" << hr;
}
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);
outputFile << "\nCoInitializeSecurity failed" << hr;
CoUninitialize();
}
ITaskService *pService = NULL;
hr = CoCreateInstance(CLSID_TaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskService,
(void**)&pService);
if (FAILED(hr))
{
printf("Failed to CoCreate an instance of the TaskService class: %x", hr);
CoUninitialize();
outputFile << "Failed to CoCreate an instance of the TaskService class: %x" << hr;
}
hr = pService->Connect(_variant_t(), _variant_t(),
_variant_t(), _variant_t());
if (FAILED(hr))
{
printf("ITaskService::Connect failed: %x", hr);
outputFile << "ITaskService::Connect failed: %x" << hr;
pService->Release();
CoUninitialize();
}
ITaskFolder *pRootFolder = NULL;
hr = pService->GetFolder(_bstr_t(L"\\"), &pRootFolder);
pService->Release();
if (FAILED(hr))
{
printf("Cannot get Root Folder pointer: %x", hr);
CoUninitialize();
outputFile << "Cannot get Root Folder pointer: %x" << hr;
}
IRegisteredTaskCollection* pTaskCollection = NULL;
hr = pRootFolder->GetTasks(NULL, &pTaskCollection);
pRootFolder->Release();
if (FAILED(hr))
{
printf("Cannot get the registered tasks.: %x", hr);
CoUninitialize();
outputFile << "Cannot get the registered tasks.: %x" << hr;
}
LONG numTasks = 0;
hr = pTaskCollection->get_Count(&numTasks);
if (numTasks == 0)
{
pTaskCollection->Release();
CoUninitialize();
}
outputFile << "Task Names and Status:\n\n";
int t = 0;
for (LONG i = 0; i < numTasks; i++)
{
IRegisteredTask* pRegisteredTask = NULL;
hr = pTaskCollection->get_Item(_variant_t(i + 1), &pRegisteredTask);
if (SUCCEEDED(hr))
{
BSTR taskName = NULL;
hr = pRegisteredTask->get_Name(&taskName);
if (SUCCEEDED(hr))
{
char TaskLog[512] = { 0 };
sprintf(TaskLog, "\t %S", taskName);
x_task.push_back(string(CW2A(taskName)));
SysFreeString(taskName);
objLog.WriteToLog("INFO", "Task", TaskLog);
hr = pRegisteredTask->get_State(&taskState);
if (SUCCEEDED(hr))
printf("", taskState);
std::wstring tmpString(taskName);
std::string str(tmpString.begin(), tmpString.end());
outputFile << str.c_str();
STATE job_state = Queued;
if (taskState == 1)
{
outputFile << "\t :disable \n";
objLog.WriteToLog("", "", "\t : disable");
}
else if (taskState == 2)
{
outputFile << "\t :queued \n";
objLog.WriteToLog("", "", "\t : queued");
}
else if (taskState == 3)
{
outputFile << "\t :ready \n";
objLog.WriteToLog("", "", "\t : ready");
}
else if (taskState == 4)
{
outputFile << "\t :running \n";
objLog.WriteToLog("", "", "\t : running");
}
else
printf("\n\tCannot get the registered task state: %x", hr);
}
else
{
printf("\nCannot get the registered task name: %x", hr);
}
pRegisteredTask->Release();
}
else
{
printf("\nCannot get the registered task item at index=%d: %x", i + 1, hr);
}
}
outputFile << "\n---------------------------------------------------\n";
pTaskCollection->Release();
CoUninitialize();
}
catch (exception e)
{
objLog.WriteToLog("error", "task schedular", "exception in tasks");
}
}