Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hey guys i am developing an mfc vc++ user iterface application in which i want to display values in edit control box with read only mode. the problem i am facing is i want the output to be displayed the list of all scheduled task.But through my code all values get override and hence forth it displays only last scheduled task instead of list of scheduled task. can any one tell me where i am mistaken.here is my code :

BOOL CoriginalDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	
	//------------------------------task-------------------------------------------------------------------------
	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 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 CoCreate an instance of the TaskService class: %x", hr);
		CoUninitialize();
		return 1;
	}

	//  Connect to the task service.
	hr = pService->Connect(_variant_t(), _variant_t(),
		_variant_t(), _variant_t());
	if (FAILED(hr))
	{
		printf("ITaskService::Connect failed: %x", hr);
		pService->Release();
		CoUninitialize();
		return 1;
	}

	//  ------------------------------------------------------
	//  Get the pointer to the root task folder.
	ITaskFolder *pRootFolder = NULL;
	hr = pService->GetFolder(_bstr_t(L"\\"), &pRootFolder);

	pService->Release();
	if (FAILED(hr))
	{
		printf("Cannot get Root Folder pointer: %x", hr);
		CoUninitialize();
		return 1;
	}

	//  -------------------------------------------------------
	//  Get the registered tasks in the folder.
	IRegisteredTaskCollection* pTaskCollection = NULL;
	hr = pRootFolder->GetTasks(NULL, &pTaskCollection);

	pRootFolder->Release();
	if (FAILED(hr))
	{
		printf("Cannot get the registered tasks.: %x", hr);
		CoUninitialize();
		return 1;
	}

	LONG numTasks = 0;
	hr = pTaskCollection->get_Count(&numTasks);

	if (numTasks == 0)
	{
		//printf("\nNo Tasks are currently running");
		pTaskCollection->Release();
		CoUninitialize();
		return 1;
	}

	//printf("\nNumber of Tasks : %d", numTasks);
	cout << "Task Names:";
	TASK_STATE taskState;


	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))
			{
				printf("\t  %S", taskName);
				SysFreeString(taskName);
			    
				m_users.SetWindowTextW(taskName);
				 

							
				
				
				hr = pRegisteredTask->get_State(&taskState);
				if (SUCCEEDED(hr))
					printf("", taskState);

				if (taskState == 1)
					printf(": disable \n");

				else	if (taskState == 2)
					printf(": queued \n ");

				else	if (taskState == 3)
					printf(": ready\n");

				else if (taskState == 4)
					printf(": running \n ");

				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);
		}
	}

	pTaskCollection->Release();
	CoUninitialize();
	
	//------------------------------------------------------------------------------------------------------
	return TRUE;  // return TRUE  unless you set the focus to a control
}



Note:
m_user is member variable
Posted

1 solution

The error is this code:

m_users.SetWindowTextW(taskName);


What you want is possibly something like:

CString str = m_users.GetWindowTextW();
str += "\r\n";
str += taskName;
m_users.SetWindowTextW(str);
 
Share this answer
 
v2
Comments
Member 11460314 21-May-15 9:26am    
i tried this as well but seems no output is being display on edit control box as only last task is being displayed instead of entire list of task
Leo Chapiro 21-May-15 9:41am    
But you understand the difference? In your code override you the "old" value, what you need is to add the new value to the "old" one - that's all!
Member 11460314 21-May-15 9:57am    
yes dude i found my mistake . it right i am getting output proper now.thank u but now when i try to print its status its not displaying.
str +=taskStates //this line brings out an error in "+="

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900