Enumerating Running Processes in Win95 and WinNT


While developing an application, I encountered the need to get a list of all running processes (programs). Ok, so all advanced Win32 programmers know that for such a task one uses the performance data in WinNT or the ToolHelp functions in Win95. BUT, In my case my application had to run both under NT and 95, without conditional compilation, i.e without having two seperate versions of the executable, one for win95 and one for NT. So, to solve this problem I wrote a C++ class, calles Win32Process that gets a list of all currently running applications, in a "platform independent" way (independent between windows 95 and NT, and of course it's using MFC, that's why I put it in quotes). More accuratly, the class detects if its running under NT or 95, and accordingly invokes the correct functions.

More-Over, the code for the class is written in such a way, that when platform-dependent functions from a dll are used, these functions are loaded dynamically, so that you do not have to link statically to a .lib file, thereby making your code (or at least your project settings) different if compiling on NT or on 95. for instance, if we are compiling on NT, but we need to use the Win 95-specific ToolHelp function Process32Next( ), there is no problem - the project will compile, link and run successfully.

So, in summary, all you need to do is drop this class into your current VC++ 5.0 project - without changing ANY of your current project settings, and then you'll be able to enumerate all running processes without worrying if the code will be executed/compiled on win95 or NT. The class will automatically do this for you.

Using Win32Process.cpp

To use this class, include it's header where you want to use it. Then, instantiate it:

Win32Process m_win32proc;

Then, before using it (for example in OnInitDialog of you application), Initiate it:


The initiation gets the needed function pointers if running on 95. If running on NT it doesnt do much but still must be called.
Note that EVERY method returns false if it fails and true if it succeeds, and to get the cause of the error you simply call
GetLastError( ), in return for which you get a CString describing the nature of the error.

Now, to get a list of all the processes, do:

if (!m_win32proc.EnumAllProcesses())

And you're all set! Now a CStringArray member in Win32Process.cpp contains all the names of the currently running processes.
(Note that the AfxMessageBox is just for debugging. When actually using it you may do anything you like with the error string. (BTW I havent had an error yet using this class)).

To display these names (for example in a list box) you may do something like this:

//populate the list box
int size=m_win32proc.GetAllProcessesNames()->GetSize();
for (int i=0;i<size;i++)

Where the m_ctrlProcLB is a list box in this case. (See the enclosed Demo)
Simple, isnt it? and remember that you use the same code regardless if its running on NT or 95. Also, none of your project settings have to be changed.

It is also possible to check if one certain process is running using the member GetProcessStatus( ) as follows:

if (!m_win32proc.GetProcessStatus(&m_strProcessToCheck,&bStatus))
if (bStatus)
	AfxMessageBox(m_strProcessToCheck+" is running!");
	AfxMessageBox(m_strProcessToCheck+" is NOT running!");

Note that GetProcessStatus( ) does not return TRUE if the process is running, but rather, it returns TRUE if it was successful. You get the info about the process by passing a pointer to a boolean (see bStatus above)

The Demo

Enclosed is also a demo program that exemplifies the usage of this class. The demo is a simple VC++ 5.0 project. The demo also shows how you can test whether a specific process is running, using the Win32Process class.

To use the class, copy the file Win32Process.cpp and Win32Process.h from the demo directory into your project directory, and add them to your project.

Download Source Code and Example- 107KB

Last updated: 27 June 1998