List running processes

Is there any way to list all running processes using from a console application using the API?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can use EnumWindows to enumerate all windows, then GetWindowThreadProcessId to get process identifier for each one. Just in case one process might have more then one window, you'll have to check the list for double entries. If you need the process titles, when you find a new process, use GetWindowText to get the title from the window

staticsnowAuthor Commented:
what about processes without windows?
For Win95 you can use the ToolHelp functions, for WinNT the "PsApi.dll" functions.

Regards, Madshi.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Yes . CreateToolhelp32Snapshot is the function .
staticsnowAuthor Commented:
Forgive me if i sound stupid but i'm kinda new to C++ (I have been using Visual Basic) but could you explain a little more about the CreateToolhelp32Snapshot function? (how to declare it, what includes to use, parameters, etc.)
If you would like a copy of working code, I can send you some.

1. Check out the SDK Windows NT Sample "Tlist: Task List". It is a Windows NT console application.

2. HOWTO: Enumerate Applications in Win32

3. Enumerating Running Processes in Win95 and WinNT

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
staticsnowAuthor Commented:
I tried to use the Toolhelp functions, and it compiles fine, but when i try to run my project it won't link.  It says "error LNK2001: unresolved external symbol _Process32First@8" and the same for CreateToolhelp32Snapshot.  I am running windows 95 OSR2 and using MS Visual C++ 5.0.
Go to Visual C++ menu Project/Settings..., click the Link tab and add th32.lib to the Object/library modules edit box.

It is recommended to use Run-Time Dynamic Linking instead so that the applications can also run on Windows NT. See the Platform SDK documentation about Tool Help Library. The following was copied from the documentation.

Accessing the Tool Help Functions
The tool help functions reside in the operating system kernel. The following example provides a platform-independent approach to accessing the tool help functions.

#include <tlhelp32.h> // needed for tool help declarations
// Type definitions for pointers to call tool help functions.
typedef HANDLE (WINAPI *CREATESNAPSHOT)(DWORD dwFlags,     DWORD th32ProcessID);
// File scope globals. These pointers are declared because of the need
// to dynamically link to the functions.  They are exported only by
// the Windows 95 kernel. Explicitly linking to them will make this
// application unloadable in Microsoft(R) Windows NT(TM) and will
// produce an ugly system dialog box.

static CREATESNAPSHOT pCreateToolhelp32Snapshot = NULL;
static MODULEWALK  pModule32First  = NULL;
static MODULEWALK  pModule32Next   = NULL;
static PROCESSWALK pProcess32First = NULL;
static PROCESSWALK pProcess32Next  = NULL;
static THREADWALK  pThread32First  = NULL;
static THREADWALK  pThread32Next   = NULL;
// Function that initializes tool help functions.
BOOL InitToolhelp32 (void)
    BOOL   bRet  = FALSE;
    HMODULE hKernel = NULL;
    // Obtain the module handle of the kernel to retrieve addresses of
    // the tool helper functions.

    hKernel = GetModuleHandle("KERNEL32.DLL");
    if (hKernel)
        pCreateToolhelp32Snapshot =
        pModule32First  = (MODULEWALK)GetProcAddress(hKernel,
        pModule32Next   = (MODULEWALK)GetProcAddress(hKernel,
        pProcess32First = (PROCESSWALK)GetProcAddress(hKernel,
        pProcess32Next  = (PROCESSWALK)GetProcAddress(hKernel,
        pThread32First  = (THREADWALK)GetProcAddress(hKernel,
        pThread32Next   = (THREADWALK)GetProcAddress(hKernel,
        // All addresses must be non-NULL to be successful.
        // If one of these addresses is NULL, one of
        // the needed lists cannot be walked.

        bRet =  pModule32First && pModule32Next  && pProcess32First && 
                pProcess32Next && pThread32First && pThread32Next && 
        bRet = FALSE; // could not get the module handle of kernel
    return bRet;

staticsnowAuthor Commented:
Thanks!  Microsoft's documentation said to use kernel32.lib.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.