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, 12:09 AM   #1
CodeWeasel
Newbie
 
Join Date: Jul 2004
Location: Blacksburg, VA
Posts: 13
Rep Power: 0 CodeWeasel is on a distinguished road
Windows API: How to obtain a list of currently running processes.

Haven't posted in a while, figured I'd give you guys something from my personal code stash I've built up over the years. It wouldn't let me create a new thread in the tutorials section, so I've posted it here...

Here's a little piece of code that'll get the exe name for each running process. You'll need to include windows.h.

HANDLE            hSnapShot=NULL;
	HANDLE            hResult = NULL;
	PROCESSENTRY32    processInfo;
	char*            pstrExeName;

	bool bFirst = true;
	::ZeroMemory(&processInfo, sizeof(PROCESSENTRY32));
	processInfo.dwSize = sizeof(PROCESSENTRY32);
	hSnapShot = CreateToolhelp32Snapshot(
		TH32CS_SNAPPROCESS, 0);
	if(hSnapShot == INVALID_HANDLE_VALUE)
		return NULL; 

	while((bFirst ? Process32First(hSnapShot, 
		&processInfo) : Process32Next(hSnapShot, 
		&processInfo)))
	{
		bFirst = false;
		pstrExeName = strrchr(processInfo.szExeFile, '\\');
		if(!pstrExeName)
			pstrExeName = processInfo.szExeFile;
		else
			pstrExeName++;
                          // Do whatever you want with the name here. In my
                          // BPK program it puts the string in the listbox on the left
	}
	if(hSnapShot)
		CloseHandle(hSnapShot);
Enjoy!

For an example of this piece of code in action, see my process utility here: http://www.freewebs.com/wombatex/bpk.htm
CodeWeasel is offline   Reply With Quote
Old Feb 24th, 2006, 4:52 AM   #2
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
I haven't looked at you program but it sounds very similar some something I made a whill back. There is another resent thread in here that shows how to list all processes the non toolhelp way. MSDN also has well a commented example for this approach.

Also, I think you forgot to mention to include: tlhelp32.h
Cache is offline   Reply With Quote
Old Feb 24th, 2006, 12:19 PM   #3
CodeWeasel
Newbie
 
Join Date: Jul 2004
Location: Blacksburg, VA
Posts: 13
Rep Power: 0 CodeWeasel is on a distinguished road
Yeah, my bad, forgot the toolhelp header. I think I pieced this together from MSDN code about a year or so ago, so that's why it uses toolhelp.
CodeWeasel is offline   Reply With Quote
Old Feb 25th, 2006, 8:06 AM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
What's the difference between the toolhelp (snapshot) way and the other way?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion 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 12:58 PM.

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