Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 24th, 2006, 2:58 PM   #11
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
Thanks DaWei and nnxion, that worked.

I have made it so that its writes all the running process names and the ID's to a .txt file. I would like to have a function read the file and look for a certain process name (could have multiple entries) and grab the ID (or ID's) which is next to it. Would GetLine(); or "find" be the best function to use? and then, how can i split off just the ID?

The .txt file is setup like:

process1.exe 123
<unknown> 04
process2.exe 1000
process2.exe 546
...

I would like to be able to search for process2 for example and have it return both 1000 and 546 (and any other entries it may have). I haven't got to this section of my C++ book yet :o

Thanks,
-BB98

** Actually, I noticed the function I wish to use afterward, TerminateProcess(); needs to be passed the process handle... Does anyone know how that is returned (what type) by the OpenProcess(); command? I am trying to write that to the file as well, however I get an error when trying to write it as a string, and the other type I have tried cannot be right since 90% are identical values.
__________________
Learning to use C++ and loving every minute of it.

Last edited by badbasser98; Feb 24th, 2006 at 3:23 PM.
badbasser98 is offline   Reply With Quote
Old Feb 24th, 2006, 11:53 PM   #12
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 259
Rep Power: 3 Cache is on a distinguished road
You've got almost evreything you need from the MSDN example.

The code in bold is what I've changed from the original MSDN example. As you can see, there is no need for text file tricks, or much else for that matter:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include "psapi.h"
 
void PrintProcessNameAndID( DWORD processID )
{
	TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
 
	// Get a handle to the process.
 
	HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
				PROCESS_VM_READ | PROCESS_TERMINATE,
				FALSE, processID );
 
	// Get the process name.
 
	if (NULL != hProcess )
	{
		HMODULE hMod;
		DWORD cbNeeded;
 
		if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
			 &cbNeeded) )
		{
			GetModuleBaseName( hProcess, hMod, szProcessName, 
							 sizeof(szProcessName)/sizeof(TCHAR) );
		}
	}
 
	if ( lstrcmp(szProcessName, "taskmgr.exe") == 0 )
	{
		 TerminateProcess( hProcess, 0 );
	}
 
	CloseHandle( hProcess );
}
 
int main( )
{
	// Get the list of process identifiers.
 
	DWORD aProcesses[1024], cbNeeded, cProcesses;
	unsigned int i;
 
	if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
		return 0;
 
	// Calculate how many process identifiers were returned.
 
	cProcesses = cbNeeded / sizeof(DWORD);
 
	for ( i = 0; i < cProcesses; i++ )
		PrintProcessNameAndID( aProcesses[i] );
	
	return 0;
}
Cache is offline   Reply With Quote
Old Feb 27th, 2006, 9:18 AM   #13
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
Thanks, but I cannot get that code to work. The compare works, however the TerminateProcess function does not. I will look into that function more.

Thanks for the help,
-BB98
__________________
Learning to use C++ and loving every minute of it.
badbasser98 is offline   Reply With Quote
Old Feb 27th, 2006, 12:04 PM   #14
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
I just made the program prompt the user that the screens are open and to close them before continuing. And to make sure, I looped that portion of the program untill a variable is set to zero (determining that all sessions of the Emulator have been closed). So they can click OK all day long and it will not go past the prompt untill they close the open sessions.

Thanks for the help, This will be sufficient.
-BB98
__________________
Learning to use C++ and loving every minute of it.
badbasser98 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC