Click here to Skip to main content
15,896,111 members
Articles / Web Development / HTML

A Comprehensive CE Class Library to Replace ATL and MFC

Rate me:
Please Sign up or sign in to vote.
4.48/5 (14 votes)
4 Oct 2000CPOL 282.5K   998   70  
A collection of classes for CE that do not use ATL or MFC, plus an FTP client, database viewer, and sample application that solves beam deflection equations.
#include "stdafx.h"
#include "process.h"


///////////////////////////////////////////////////////////////////////////////
// 
// Routine Description: 
//    Provides an API for getting a list of tasks running at the time of the 
//    API call.  This function uses Toolhelp32to get the task list and is  
//    therefore straight WIN32 calls that anyone can call. 
// 
// Arguments: 
//    dwNumTasks       - maximum number of tasks that the pTask array can hold 
// 
// Return Value: 
//    Number of tasks placed into the pTask array. 
// 
///////////////////////////////////////////////////////////////////////////////
PROCESSUTIL_API BOOL GetProcessList(PPROCESS_DESC_LIST pProcessList)
{ 
	if (NULL == pProcessList)
		return FALSE;

	pProcessList->m_nProcesses = 0; 
	pProcessList->m_pProcessList = NULL;

#ifdef _WIN32_WCE_EMULATION
	return TRUE;
#endif
	
	// function variables
	HANDLE			hProcessSnap; 
	PROCESSENTRY32	pe32			= {sizeof(PROCESSENTRY32)}; 
	PPROCESS_DESC	pProcess		= NULL;
	WORD			wAllocated		= 100;
	PVOID			pAlloc;
 
	// Take a snapshot of all processes currently in the system. 
	hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 
	if ((HANDLE)-1 == hProcessSnap)
		return FALSE; 
	
	if (NULL == (pAlloc = malloc(wAllocated * sizeof(PROCESS_DESC))))
		return FALSE;

	// Walk the snapshot of processes and for each process, get information 
	// to display. 
	pProcess = (PPROCESS_DESC) pAlloc;
	if (Process32First(hProcessSnap, &pe32)) 
	{ 
		do
		{ 
			// strip path and leave executabe filename splitpath 
			pProcess->m_dwId = pe32.th32ProcessID;
			pProcess->m_bstrProcess = SysAllocString(pe32.szExeFile);

			++pProcessList->m_nProcesses;   // keep track of how many tasks we've got so far 
			if (pProcessList->m_nProcesses >= wAllocated)
			{
				// realloc a block
				pAlloc = realloc(pAlloc, wAllocated *= 2);
				pProcess = (PPROCESS_DESC)pAlloc + pProcessList->m_nProcesses;
			}
			else
			{
				// get to next task info block. 
				++pProcess;
			}
		} 
		while (Process32Next(hProcessSnap, &pe32)); 
	} 
	
	// Don't forget to clean up the snapshot object... 
	CloseToolhelp32Snapshot(hProcessSnap); 

	pProcessList->m_pProcessList = (PPROCESS_DESC) pAlloc;

	return TRUE; 
} 
 

///////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////
PROCESSUTIL_API BOOL KillProcessId(DWORD dwId)
{
	// well, try Terminate..
	HANDLE hProcess = OpenProcess( PROCESS_TERMINATE, FALSE, dwId); 
	if (hProcess == NULL)
		return FALSE;
	
	BOOL bRet = TerminateProcess(hProcess, 1);
	CloseHandle(hProcess);
	return bRet;
}


///////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////
PROCESSUTIL_API void FreeProcessList(/*in*/ PROCESS_DESC_LIST* pProcessList)
{
	for (int ii = 0; ii < pProcessList->m_nProcesses; ii++)
	{
		if (pProcessList->m_pProcessList[ii].m_bstrProcess)
			SysFreeString(pProcessList->m_pProcessList[ii].m_bstrProcess);
	}

	free( pProcessList->m_pProcessList );
	pProcessList->m_nProcesses = 0;
	pProcessList->m_pProcessList = NULL;
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions