Solved

List running processes

Posted on 1998-10-11
10
265 Views
Last Modified: 2013-12-03
Is there any way to list all running processes using from a console application using the API?
0
Comment
Question by:staticsnow
10 Comments
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1415184
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

0
 

Author Comment

by:staticsnow
ID: 1415185
what about processes without windows?
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1415186
For Win95 you can use the ToolHelp functions, for WinNT the "PsApi.dll" functions.

Regards, Madshi.
0
 
LVL 6

Expert Comment

by:WxW
ID: 1415187
Yes . CreateToolhelp32Snapshot is the function .
0
 

Author Comment

by:staticsnow
ID: 1415188
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.)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:BudVVeezer
ID: 1415189
If you would like a copy of working code, I can send you some.

~Aaron
0
 
LVL 23

Accepted Solution

by:
chensu earned 70 total points
ID: 1415190
1. Check out the SDK Windows NT Sample "Tlist: Task List". It is a Windows NT console application.

2. HOWTO: Enumerate Applications in Win32
http://support.microsoft.com/support/kb/articles/q175/0/30.asp

3. Enumerating Running Processes in Win95 and WinNT
http://www.codeguru.com/misc/win32proc.shtml
0
 

Author Comment

by:staticsnow
ID: 1415191
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.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1415192
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 BOOL (WINAPI *MODULEWALK)(HANDLE hSnapshot,     LPMODULEENTRY32 lpme);
typedef BOOL (WINAPI *THREADWALK)(HANDLE hSnapshot,     LPTHREADENTRY32 lpte);
typedef BOOL (WINAPI *PROCESSWALK)(HANDLE hSnapshot,     LPPROCESSENTRY32 lppe);
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 =
            (CREATESNAPSHOT)GetProcAddress(hKernel,
            "CreateToolhelp32Snapshot");
 
        pModule32First  = (MODULEWALK)GetProcAddress(hKernel,
            "Module32First");
        pModule32Next   = (MODULEWALK)GetProcAddress(hKernel,
            "Module32Next");
 
        pProcess32First = (PROCESSWALK)GetProcAddress(hKernel,
            "Process32First");
        pProcess32Next  = (PROCESSWALK)GetProcAddress(hKernel,
            "Process32Next");
 
        pThread32First  = (THREADWALK)GetProcAddress(hKernel,
            "Thread32First");
        pThread32Next   = (THREADWALK)GetProcAddress(hKernel,
            "Thread32Next");
 
        // 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 && 
                pCreateToolhelp32Snapshot;
    }
    else
        bRet = FALSE; // could not get the module handle of kernel
 
    return bRet;
}

0
 

Author Comment

by:staticsnow
ID: 1415193
Thanks!  Microsoft's documentation said to use kernel32.lib.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now