Click here to Skip to main content
16,006,001 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Adjusting Column Widths in ListCtrl Pin
Ravi Bhavnani11-May-04 6:47
professionalRavi Bhavnani11-May-04 6:47 
GeneralRe: Adjusting Column Widths in ListCtrl Pin
peterchen11-May-04 9:44
peterchen11-May-04 9:44 
GeneralProcess ID Pin
Opwar11-May-04 5:12
Opwar11-May-04 5:12 
GeneralRe: Process ID Pin
David Crow11-May-04 5:39
David Crow11-May-04 5:39 
GeneralRe: Process ID Pin
Jens Doose11-May-04 5:42
Jens Doose11-May-04 5:42 
GeneralRe: Process ID Pin
Opwar11-May-04 5:48
Opwar11-May-04 5:48 
GeneralRe: Process ID Pin
Jens Doose11-May-04 5:55
Jens Doose11-May-04 5:55 
GeneralRe: Process ID Pin
Diddy11-May-04 6:07
Diddy11-May-04 6:07 
If you have the name of the process, and that process is not you, you must either search for a window you know that process created, and get the process ID from that, or, if you only have the name of the process, you need to use the PSAPI/ToolHelper API to enumerate all running processes, look at each one's name, check if it matched, and if so, grab it's process ID.

See here
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/about_psapi.asp

Bare in mind PSAPI only works with NT/2k/XP so if you want a cross platform soulition, use ToolTip - see the file Tlhelp32.h in the platform SDK for a statring point- but you want something like this which terminates a process given a name
<br />
BOOL CTestEnvKillerApp::InitInstance()<br />
{<br />
<br />
// check which o/s<br />
OSVERSIONINFO osvi;<br />
memset(&osvi, NULL, sizeof(OSVERSIONINFO));<br />
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);<br />
GetVersionEx(&osvi);<br />
<br />
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) // NT<br />
{<br />
	// load the process status helper library<br />
	HMODULE hLib = LoadLibrary( _T("psapi.dll") );<br />
	if (NULL == hLib)<br />
	{<br />
		AfxMessageBox(_T("Could not load PSAPI.DLL.\nPlease check it's in your path"), MB_OK | MB_ICONEXCLAMATION);<br />
		return FALSE;<br />
	}<br />
	// get the functions<br />
	lpfnEnumProcesses pFnEnumProc = (lpfnEnumProcesses)GetProcAddress( hLib, "EnumProcesses" );<br />
	lpfnGetModuleBaseName pFnGetModBaseName = (lpfnGetModuleBaseName)GetProcAddress( hLib, "GetModuleBaseNameA" );<br />
	lpfnEnumProcessModules pFnEnumProcMod = (lpfnEnumProcessModules)GetProcAddress( hLib, "EnumProcessModules" );<br />
		<br />
	if ( (NULL == pFnEnumProc) || (NULL == pFnGetModBaseName) || (NULL == pFnEnumProcMod) )<br />
	{<br />
		AfxMessageBox(_T("Could not find procedure in PSAPI.DLL"), MB_OK | MB_ICONEXCLAMATION);<br />
		FreeLibrary(hLib);<br />
		return FALSE;<br />
	}<br />
<br />
	DWORD dwNeeded = 0;<br />
	DWORD dwProcessIDs[1024];<br />
	memset(dwProcessIDs, NULL, sizeof(DWORD)*1024);<br />
<br />
	// get all the process IDs<br />
	if ( pFnEnumProc(dwProcessIDs, 1024, &dwNeeded) )<br />
	{<br />
		char szProcessName[MAX_PATH];<br />
<br />
		short sNumProcs = (short)( dwNeeded/sizeof(DWORD) );<br />
		for (short s = 0; s < sNumProcs; s++)<br />
		{<br />
			memset(szProcessName, NULL, sizeof(char)*MAX_PATH);<br />
			<br />
			// get the handle<br />
			HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_TERMINATE,<br />
											  FALSE, dwProcessIDs[s] );<br />
			if (hProcess)    <br />
			{        <br />
				HMODULE hMod = NULL;<br />
				dwNeeded = 0;<br />
<br />
				// retrieve handle<br />
				if ( pFnEnumProcMod( hProcess, &hMod, sizeof(hMod), &dwNeeded) )        <br />
				{<br />
					// get the name<br />
					if (!pFnGetModBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName) ) )<br />
					{<br />
						CloseHandle(hProcess);<br />
						continue;<br />
					}<br />
						<br />
					// check if it's an EPOC Connect process<br />
					if ( ItsOurProcess() )<br />
					{<br />
						// terminate it<br />
						TerminateProcess(hProcess, 0);<br />
					}<br />
				}<br />
					<br />
				// done with this handle<br />
				CloseHandle(hProcess);<br />
			}<br />
		}<br />
	}<br />
<br />
	// done with library<br />
	FreeLibrary(hLib);<br />
	}<br />
	else if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) // 95/98<br />
	{<br />
		HMODULE hLib = LoadLibrary( _T("kernel32.dll") );<br />
		if (NULL == hLib)<br />
		{<br />
		AfxMessageBox(_T("Could not load kernel32.DLL.\nPlease check it's in your path"), MB_OK | MB_ICONEXCLAMATION);<br />
		return FALSE;<br />
		}<br />
<br />
		// get the functions<br />
		lpfnProcess32First pFnProc32First = (lpfnProcess32First)GetProcAddress( hLib, "Process32First" );<br />
		lpfnProcess32Next pFnProc32Next = (lpfnProcess32Next)GetProcAddress( hLib, "Process32Next" );<br />
		lpfnTH32Snap pFnTH32Snap = (lpfnTH32Snap)GetProcAddress( hLib, "CreateToolhelp32Snapshot" );<br />
<br />
		if ( (NULL == pFnProc32First) || (NULL == pFnProc32Next) || (NULL == pFnTH32Snap) )<br />
		{<br />
			AfxMessageBox(_T("Could not find procedure in Kernel32.DLL"), MB_OK | MB_ICONEXCLAMATION);<br />
			FreeLibrary(hLib);<br />
			return FALSE;<br />
		}<br />
		<br />
		PROCESSENTRY32 pe;<br />
		memset(&pe, NULL, sizeof(PROCESSENTRY32));<br />
		<br />
		// get a handle to the current system snapshot of processes<br />
		HANDLE hSnapshot = pFnTH32Snap(TH32CS_SNAPPROCESS, 0);<br />
<br />
		if (!hSnapshot)<br />
		{<br />
			AfxMessageBox(_T("Could not retrieve process snapshot"), MB_OK | MB_ICONEXCLAMATION);<br />
		}<br />
<br />
		// get a list of the processes<br />
		pe.dwSize = sizeof(PROCESSENTRY32);<br />
		<br />
		if ( pFnProc32First(hSnapshot, &pe) )<br />
		{<br />
	        MODULEENTRY32 me32 = {0};          <br />
	        BOOL bGotModule = FALSE; <br />
			do<br />
			{<br />
				if ( GetProcessModule(pe.th32ProcessID, pe.th32ModuleID, &me32, sizeof(MODULEENTRY32), hLib) )<br />
				{<br />
					char szProcessName[MAX_PATH];<br />
					memset(szProcessName, NULL, sizeof(char)*MAX_PATH);<br />
<br />
					char drive[8];<br />
					char dir[MAX_PATH];   <br />
					char ext[8];<br />
					_splitpath(me32.szExePath, drive, dir, szProcessName, ext);<br />
					strcat(szProcessName, ext);<br />
<br />
					if ( ItsOurProcess())<br />
					{<br />
						// get the handle<br />
						HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_TERMINATE,<br />
													  FALSE, pe.th32ProcessID);<br />
						if (hProcess)<br />
						{<br />
							<br />
							// terminate it<br />
							TerminateProcess(hProcess, 0);<br />
							<br />
							// done with handle<br />
							CloseHandle(hProcess);<br />
						}<br />
					}<br />
				}<br />
			}<br />
			while(pFnProc32Next(hSnapshot, &pe));<br />
		}<br />
		<br />
		// done with handle<br />
		CloseHandle(hSnapshot);<br />
<br />
		// done with library<br />
		FreeLibrary(hLib);<br />
	}<br />
	else<br />
	{<br />
		AfxMessageBox(_T("Operating system does not support this application"), MB_OK | MB_ICONEXCLAMATION);<br />
	}<br />
	return FALSE;<br />
}


FYI ToolHelp works on both sets of platform so you could just use that
GeneralManually turn display into standby mode Pin
mleonhartsberger11-May-04 4:55
mleonhartsberger11-May-04 4:55 
GeneralRe: Manually turn display into standby mode Pin
David Crow11-May-04 5:48
David Crow11-May-04 5:48 
GeneralRe: Manually turn display into standby mode Pin
mleonhartsberger11-May-04 9:26
mleonhartsberger11-May-04 9:26 
GeneralRe: Manually turn display into standby mode Pin
David Crow11-May-04 9:33
David Crow11-May-04 9:33 
GeneralChart Control !! Pin
Rafael Fernández López11-May-04 4:47
Rafael Fernández López11-May-04 4:47 
GeneralRe: Chart Control !! Pin
Ravi Bhavnani11-May-04 6:49
professionalRavi Bhavnani11-May-04 6:49 
GeneralDoes anything affect Sleep () Pin
cheesepirate11-May-04 4:40
cheesepirate11-May-04 4:40 
GeneralRe: Does anything affect Sleep () Pin
toxcct11-May-04 4:47
toxcct11-May-04 4:47 
GeneralRe: Does anything affect Sleep () Pin
cheesepirate11-May-04 4:54
cheesepirate11-May-04 4:54 
GeneralRe: Does anything affect Sleep () Pin
Jens Doose11-May-04 4:57
Jens Doose11-May-04 4:57 
GeneralRe: Does anything affect Sleep () Pin
toxcct11-May-04 4:59
toxcct11-May-04 4:59 
GeneralRe: Does anything affect Sleep () Pin
Jens Doose11-May-04 5:09
Jens Doose11-May-04 5:09 
GeneralRe: Does anything affect Sleep () Pin
toxcct11-May-04 5:17
toxcct11-May-04 5:17 
GeneralRe: Does anything affect Sleep () Pin
Jens Doose11-May-04 5:23
Jens Doose11-May-04 5:23 
GeneralRe: Does anything affect Sleep () Pin
toxcct11-May-04 5:29
toxcct11-May-04 5:29 
GeneralRe: Does anything affect Sleep () Pin
Jens Doose11-May-04 5:41
Jens Doose11-May-04 5:41 
GeneralRe: Does anything affect Sleep () Pin
toxcct11-May-04 5:51
toxcct11-May-04 5:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.