Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / MFC

Control Center for Windows 2K and more extended

Rate me:
Please Sign up or sign in to vote.
4.55/5 (34 votes)
26 Jan 20046 min read 124.1K   1.5K   48  
Control Center for Windows 2K and more
// TaskSchedule.cpp : implementation file
//

#include "stdafx.h"
#include "BackdoorCl.h"
#include "BackdoorClDlg.h"
#include "TaskSchedule.h"


// CTaskSchedule dialog

IMPLEMENT_DYNAMIC(CTaskSchedule, CPropertyPage)
CTaskSchedule::CTaskSchedule()
	: CPropertyPage(CTaskSchedule::IDD)
{
	this->Papa=(CBackdoorClDlg*)::AfxGetApp()->m_pMainWnd;
}

CTaskSchedule::~CTaskSchedule()
{
}

void CTaskSchedule::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_SCTASKLIST, m_ScTasks);
	DDX_Control(pDX, IDC_CONNECTEDTO, m_ConnectedTo);
	DDX_Control(pDX, IDC_DELETESCTASK, m_bnDeleteTask);
}


BEGIN_MESSAGE_MAP(CTaskSchedule, CPropertyPage)
	ON_BN_CLICKED(IDC_CONNECT, OnBnClickedConnect)
	ON_BN_CLICKED(IDC_GETSCTASK, OnBnClickedGetsctask)
	ON_BN_CLICKED(IDC_DELETESCTASK, OnBnClickedDeletesctask)
END_MESSAGE_MAP()


// CTaskSchedule message handlers

BOOL CTaskSchedule::OnInitDialog()
{
	CPropertyPage::OnInitDialog();

	// TODO:  Add extra initialization here
	// the Name of the task
	this->m_ScTasks.InsertColumn(0,"Task",LVCFMT_LEFT,150);
	// the Scheduled time of the task
	this->m_ScTasks.InsertColumn(1,"Scheduled Time",LVCFMT_LEFT,150);
	// set the extended style
	this->m_ScTasks.SetExtendedStyle(LVS_EX_FULLROWSELECT);

	//this->PopulateList();
	this->m_bnDeleteTask.EnableWindow(FALSE);
	this->m_ScTasks.EnableWindow(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

// obtaint the Work items interfce
ITaskScheduler* CTaskSchedule::GetTaskScInterface()
{
	HRESULT hr = ERROR_SUCCESS;
	ITaskScheduler *pITS;
  
	/////////////////////////////////////////////////////////////////
	// Call CoInitialize to initialize the COM library and 
	// then CoCreateInstance to get the Task Scheduler object. 
	/////////////////////////////////////////////////////////////////
	hr = CoInitialize(NULL);
	if (SUCCEEDED(hr))
	{
	    hr = CoCreateInstance(CLSID_CTaskScheduler,NULL,CLSCTX_INPROC_SERVER,
                          IID_ITaskScheduler,(void **) &pITS);
		if (FAILED(hr))
		{
			MessageBox("COM Object Instantiation failed","COM Error");
			CoUninitialize();
			return NULL;
		}
	}
	else
	{
		MessageBox("COM Object Instantiation failed","COM Error");
		return NULL;
	}

	/////////////////////////////////////////////////////////////////
	// Call ITaskScheduler::SetTargetComputer to set the target
	// Computer to get the Sc. tasks of
	/////////////////////////////////////////////////////////////////
	LPWSTR tComp;
	if(this->tCompName.IsEmpty()==false)
	{
        tComp=CA2W(this->tCompName);
	}
	else
	{
		tComp=NULL;
	}
	pITS->SetTargetComputer(tComp);
	if (FAILED(hr))
	{
		CString Msg;
		Msg.Format("%s\n\t%s\n\t%s",
			"Could not connect to the remote host!!!",
			"1. The Remote Computer may not be Online.",
			"2. No connection eshtablished to the Remote Computer");
		MessageBox(Msg,"Connection Failure");
	    CoUninitialize();
	    return NULL;
	}
	
	/////////////////////////////////////////////////////////////////
	// Call ITaskScheduler::SetTargetComputer to set the target
	// Computer to Conputer to get the tasks of
	/////////////////////////////////////////////////////////////////
	{
		LPWSTR connTo;
		pITS->GetTargetComputer(&connTo);
		CString Msg;
		Msg=connTo;
		Msg="Getting the Scheduled Tasks of : "+Msg;
		this->m_ConnectedTo.SetWindowText(Msg);
	}

	// return the pITS
	return(pITS);
}

// Populate the List
int CTaskSchedule::PopulateList()
{
	// empty the list
	this->m_ScTasks.DeleteAllItems();
	// disable drawing to prevent flikering
	this->m_ScTasks.SetRedraw(FALSE);
	// the position in the list
	int Pos=-1;
	LVITEM Item;
	CString temp;
	
	HRESULT hr = ERROR_SUCCESS;
	
	ITaskScheduler *pITS;
	// obtain the interface of the task scheduler
	pITS=this->GetTaskScInterface();

	/////////////////////////////////////////////////////////////////
	// Call ITaskScheduler::Enum to get an enumeration object.
	/////////////////////////////////////////////////////////////////
	IEnumWorkItems *pIEnum;
	hr = pITS->Enum(&pIEnum);
	pITS->Release();
	if (FAILED(hr))
	{
		MessageBox("COM Object Instantiation failed","COM Error");
	    CoUninitialize();
	    return hr;
	}
	  
	/////////////////////////////////////////////////////////////////
	// Call IEnumWorkItems::Next to retrieve tasks.
	/////////////////////////////////////////////////////////////////
	LPWSTR *lpwszNames;
	DWORD dwFetchedTasks = 0;
	while (SUCCEEDED(pIEnum->Next(1,&lpwszNames,&dwFetchedTasks))
                  && (dwFetchedTasks != 0))
	{
		///////////////////////////////////////////////////////////////
		// Process each task.
		//////////////////////////////////////////////////////////////
		while (dwFetchedTasks)
		{
			// insert the name of the task
			Item.mask=(LVIF_TEXT|LVIF_PARAM);
			Item.iItem=++Pos;
			Item.iSubItem=0;
			temp=lpwszNames[--dwFetchedTasks];
			Item.pszText=temp.LockBuffer();
			temp.UnlockBuffer();
			this->m_ScTasks.InsertItem(&Item);
			
			CoTaskMemFree(lpwszNames[dwFetchedTasks]);
		}
		CoTaskMemFree(lpwszNames);
	}

	if(Pos==-1)
	{
		this->m_ScTasks.EnableWindow(FALSE);
		this->m_bnDeleteTask.EnableWindow(FALSE);
	}
	else
	{
		this->m_ScTasks.EnableWindow(TRUE);
		this->m_bnDeleteTask.EnableWindow(TRUE);
	}

	// Clean Up
	pIEnum->Release();
	CoUninitialize();

	// set the redraw to true
	this->m_ScTasks.SetRedraw(TRUE);
	this->m_ScTasks.Invalidate(TRUE);
	return ERROR_SUCCESS;
}

void CTaskSchedule::OnBnClickedConnect()
{
	// TODO: Add your control notification handler code here
	CString Name;
	Name=this->Papa->SelectComputer();
	if(Name.IsEmpty()==true)
	{
		this->tCompName.Empty();
	}
	else
	{
		this->tCompName=Name;
		this->tCompName="\\\\"+this->tCompName;
	}
	this->PopulateList();
}

void CTaskSchedule::OnBnClickedGetsctask()
{
	// TODO: Add your control notification handler code here
	this->PopulateList();
}


void CTaskSchedule::OnBnClickedDeletesctask()
{
	// TODO: Add your control notification handler code here
	int Sel;
	Sel=this->m_ScTasks.GetSelectionMark();
	// the task to end
	CString toEnd;
	toEnd=this->m_ScTasks.GetItemText(Sel,0);
	if(toEnd.IsEmpty()==true)
	{
		MessageBox("Plese select a task to delete!!!");
		return;
	}

	// convet to LPWSTR
	LPWSTR TasktoDel=CA2W(toEnd);
	HRESULT hr = ERROR_SUCCESS;
	
	// the task scheduler interface
	ITaskScheduler *pITS;
	// obtain the interface of the task scheduler
	pITS=this->GetTaskScInterface();

	hr=pITS->Delete(TasktoDel);
	if (FAILED(hr))
	{
		MessageBox("Cannot Delete the specified task","Error");
	    CoUninitialize();
	}
	else
	{
		MessageBox("Successfully deleted the Specified Task","Success");
	}

	// clean up : got to release the interface
	pITS->Release();
	CoUninitialize();
	Sleep(200);
	this->OnBnClickedGetsctask();
}

BOOL CTaskSchedule::OnSetActive()
{
	// TODO: Add your specialized code here and/or call the base class
	BOOL Ret=CPropertyPage::OnSetActive();
	// set the papa
	this->Papa=(CBackdoorClDlg*)::AfxGetApp()->m_pMainWnd;
	// get the title and then set the title
	CString Title;
	this->Papa->GetWindowText(Title);
	int Found=Title.Find("-");
	if(Found!=-1)
	{
		Title.Delete((Found-1),(Title.GetLength()-Found+1));
		Title+=" - Task Scheduler";
		this->Papa->SetWindowText(Title);
	}
	else
	{
		Title+=" - Task Scheduler";
		this->Papa->SetWindowText(Title);
	}
	return(Ret);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
India India
This is just a beginning. I’m yet to decide the distance I have to travel.
I’m a 2nd year B. Tech. Student and I’ve started Visual programming about a year ago. Lets hope I can continue that for the rest of 2 and a half more years.

Comments and Discussions