473,800 Members | 3,022 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how can I list all the processes in the system

xxs
I have writen some codes as follow:

#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>

// Forward declarations:

BOOL GetProcessList( );
BOOL ListProcessModu les( DWORD dwPID );
BOOL KillProcessFrom Name(LPCTSTR name);
void printError( TCHAR* msg );

//
void main( )
{
GetProcessList( );
}

//获取进程信 息
BOOL GetProcessList( )
{
HANDLE hProcessSnap;
HANDLE hProcess;
PROCESSENTRY32 pe32;
DWORD dwPriorityClass ;

// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp3 2Snapshot( TH32CS_SNAPPROC ESS, 0 );
if( hProcessSnap == INVALID_HANDLE_ VALUE )
{
printError( "CreateToolhelp 32Snapshot (of processes)" );
return( FALSE );
}

// Set the size of the structure before using it.
pe32.dwSize = sizeof( PROCESSENTRY32 );

// Retrieve information about the first process,
// and exit if unsuccessful
if( !Process32First ( hProcessSnap, &pe32 ) )
{
printError( "Process32First " ); // Show cause of failure
CloseHandle( hProcessSnap ); // Must clean up the snapshot
object!
return( FALSE );
}

// Now walk the snapshot of processes, and
// display information about each process in turn
do
{
printf( "\n
\n============= =============== =============== ==========" );
printf( "\nPROCESS NAME: %s", pe32.szExeFile );

printf( "\n-----------------------------------------------------" );

// Retrieve the priority class.
dwPriorityClass = 0;
hProcess = OpenProcess( PROCESS_ALL_ACC ESS, FALSE,
pe32.th32Proces sID );
if( hProcess == NULL )
printError( "OpenProces s" );
else
{
dwPriorityClass = GetPriorityClas s( hProcess );
if( !dwPriorityClas s )
printError( "GetPriorityCla ss" );
CloseHandle( hProcess );
}
//进程的相关 信息
printf( "\n process ID = 0x%08X", pe32.th32Proces sID );//id号
// List the modules and threads associated with this process
ListProcessModu les( pe32.th32Proces sID );
// ListProcessThre ads( pe32.th32Proces sID );

} while( Process32Next( hProcessSnap, &pe32 ) );

CloseHandle( hProcessSnap );
return( TRUE );
}
//模块信息
BOOL ListProcessModu les( DWORD dwPID )
{
HANDLE hModuleSnap = INVALID_HANDLE_ VALUE;
MODULEENTRY32 me32;
hModuleSnap = CreateToolhelp3 2Snapshot( TH32CS_SNAPMODU LE, dwPID );
if(hModuleSnap == INVALID_HANDLE_ VALUE)
{
printError( "CreateToolhelp 32Snapshot (of Modules)" );
return( FALSE );
}

// Set the size of the structure before using it.
me32.dwSize = sizeof( MODULEENTRY32 );

// Retrieve information about the first module,
// and exit if unsuccessful
if( !Module32First( hModuleSnap, &me32 ) )
{
printError( "Module32Fi rst" ); // Show cause of failure
CloseHandle( hModuleSnap ); // Must clean up the snapshot
object!
return( FALSE );
}

printf( "\n executable = %s\n", me32.szExePath );
CloseHandle( hModuleSnap );
return( TRUE );

}

//kill the special process
BOOL KillProcessFrom Name(LPCTSTR name)//name为*要 *的进程 名称,Win9X 则需包括路 径
{
PROCESSENTRY32 pe;//定义一个PRO CESSENTRY32结 型的变量
HANDLE hShot=CreateToo lhelp32Snapshot (TH32CS_SNAPPRO CESS,0);// 创建快照句 柄
HANDLE hProcess = INVALID_HANDLE_ VALUE;
pe.dwSize=sizeo f(PROCESSENTRY3 2);//一定要先为 dwSize赋值
if (Process32First (hShot,&pe))
{
do{
if (strcmp(pe.szEx eFile,name)==0) //判**进程 是否为*要 终*的进程
hProcess=OpenPr ocess(PROCESS_A LL_ACCESS,FALSE ,pe.th32Process ID);
//如果是就利 用其ID获得 柄
if( hProcess == INVALID_HANDLE_ VALUE )
{
printError( "OpenProces s (of processes)" );
return( FALSE );
}
TerminateProces s(hProcess,0);//终*该进程
}while(Process3 2Next(hShot,&pe ));
}
CloseHandle(hSh ot);//最后别忘记 Close
return( TRUE );
}
//错误处理
void printError( TCHAR* msg )
{
DWORD eNum;
TCHAR sysMsg[256];
TCHAR* p;

eNum = GetLastError( );
FormatMessage( FORMAT_MESSAGE_ FROM_SYSTEM |
FORMAT_MESSAGE_ IGNORE_INSERTS,
NULL, eNum,
MAKELANGID(LANG _NEUTRAL, SUBLANG_DEFAULT ), // Default
language
sysMsg, 256, NULL );

// Trim the end of the line and terminate it with a null
p = sysMsg;
while( ( *p 31 ) || ( *p == 9 ) )
++p;
do { *p-- = 0; } while( ( p >= sysMsg ) &&
( ( *p == '.' ) || ( *p < 33 ) ) );

// Display the message
printf( "\n WARNING: %s failed with error %d (%s)", msg, eNum,
sysMsg );
}

I can get some processes' location,but I can't get all,such as
svchost.
How can I get all processes' location?

Thank you!

Apr 17 '07 #1
3 3520
On 17 Apr, 13:11, xxs <llxx...@gmail. comwrote:
I have writen some codes as follow:

#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
Sorry, this is off-topic here, try one of the microsoft.publi c.*
groups, such as microsoft.publi c.win32.program mer.

--
Erik Wikström

Apr 17 '07 #2
xxs wrote:
I have writen some codes as follow:

#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>

// Forward declarations:

BOOL GetProcessList( );
BOOL ListProcessModu les( DWORD dwPID );
BOOL KillProcessFrom Name(LPCTSTR name);
void printError( TCHAR* msg );

//
void main( )
{
GetProcessList( );
}

//获取进程信 息
BOOL GetProcessList( )
{
HANDLE hProcessSnap;
HANDLE hProcess;
PROCESSENTRY32 pe32;
DWORD dwPriorityClass ;

// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp3 2Snapshot( TH32CS_SNAPPROC ESS, 0 );
if( hProcessSnap == INVALID_HANDLE_ VALUE )
{
printError( "CreateToolhelp 32Snapshot (of processes)" );
return( FALSE );
}

// Set the size of the structure before using it.
pe32.dwSize = sizeof( PROCESSENTRY32 );

// Retrieve information about the first process,
// and exit if unsuccessful
if( !Process32First ( hProcessSnap, &pe32 ) )
{
printError( "Process32First " ); // Show cause of failure
CloseHandle( hProcessSnap ); // Must clean up the snapshot
object!
return( FALSE );
}

// Now walk the snapshot of processes, and
// display information about each process in turn
do
{
printf( "\n
\n============= =============== =============== ==========" );
printf( "\nPROCESS NAME: %s", pe32.szExeFile );

printf( "\n-----------------------------------------------------" );

// Retrieve the priority class.
dwPriorityClass = 0;
hProcess = OpenProcess( PROCESS_ALL_ACC ESS, FALSE,
pe32.th32Proces sID );
if( hProcess == NULL )
printError( "OpenProces s" );
else
{
dwPriorityClass = GetPriorityClas s( hProcess );
if( !dwPriorityClas s )
printError( "GetPriorityCla ss" );
CloseHandle( hProcess );
}
//进程的相关 信息
printf( "\n process ID = 0x%08X", pe32.th32Proces sID );//id号
// List the modules and threads associated with this process
ListProcessModu les( pe32.th32Proces sID );
// ListProcessThre ads( pe32.th32Proces sID );

} while( Process32Next( hProcessSnap, &pe32 ) );

CloseHandle( hProcessSnap );
return( TRUE );
}
//模块信息
BOOL ListProcessModu les( DWORD dwPID )
{
HANDLE hModuleSnap = INVALID_HANDLE_ VALUE;
MODULEENTRY32 me32;
hModuleSnap = CreateToolhelp3 2Snapshot( TH32CS_SNAPMODU LE, dwPID );
if(hModuleSnap == INVALID_HANDLE_ VALUE)
{
printError( "CreateToolhelp 32Snapshot (of Modules)" );
return( FALSE );
}

// Set the size of the structure before using it.
me32.dwSize = sizeof( MODULEENTRY32 );

// Retrieve information about the first module,
// and exit if unsuccessful
if( !Module32First( hModuleSnap, &me32 ) )
{
printError( "Module32Fi rst" ); // Show cause of failure
CloseHandle( hModuleSnap ); // Must clean up the snapshot
object!
return( FALSE );
}

printf( "\n executable = %s\n", me32.szExePath );
CloseHandle( hModuleSnap );
return( TRUE );

}

//kill the special process
BOOL KillProcessFrom Name(LPCTSTR name)//name为*要 *的进程 名称,Win9X 则需包括路 径
{
PROCESSENTRY32 pe;//定义一个PRO CESSENTRY32结 型的变量
HANDLE hShot=CreateToo lhelp32Snapshot (TH32CS_SNAPPRO CESS,0);// 创建快照句 柄
HANDLE hProcess = INVALID_HANDLE_ VALUE;
pe.dwSize=sizeo f(PROCESSENTRY3 2);//一定要先为 dwSize赋值
if (Process32First (hShot,&pe))
{
do{
if (strcmp(pe.szEx eFile,name)==0) //判**进程 是否为*要 终*的进程
hProcess=OpenPr ocess(PROCESS_A LL_ACCESS,FALSE ,pe.th32Process ID);
//如果是就利 用其ID获得 柄
if( hProcess == INVALID_HANDLE_ VALUE )
{
printError( "OpenProces s (of processes)" );
return( FALSE );
}
TerminateProces s(hProcess,0);//终*该进程
}while(Process3 2Next(hShot,&pe ));
}
CloseHandle(hSh ot);//最后别忘记 Close
return( TRUE );
}
//错误处理
void printError( TCHAR* msg )
{
DWORD eNum;
TCHAR sysMsg[256];
TCHAR* p;

eNum = GetLastError( );
FormatMessage( FORMAT_MESSAGE_ FROM_SYSTEM |
FORMAT_MESSAGE_ IGNORE_INSERTS,
NULL, eNum,
MAKELANGID(LANG _NEUTRAL, SUBLANG_DEFAULT ), // Default
language
sysMsg, 256, NULL );

// Trim the end of the line and terminate it with a null
p = sysMsg;
while( ( *p 31 ) || ( *p == 9 ) )
++p;
do { *p-- = 0; } while( ( p >= sysMsg ) &&
( ( *p == '.' ) || ( *p < 33 ) ) );

// Display the message
printf( "\n WARNING: %s failed with error %d (%s)", msg, eNum,
sysMsg );
}

I can get some processes' location,but I can't get all,such as
svchost.
How can I get all processes' location?

Thank you!
Off topic, but most likely you don't have enough privilege to peek into
system processes such as svchost.
Apr 17 '07 #3
On Tue, 17 Apr 2007 10:40:11 -0400, Fei Liu <fe****@aepnetw orks.com>
wrote in comp.lang.c++:
xxs wrote:
I have writen some codes as follow?

#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
[snip]
Off topic, but most likely you don't have enough privilege to peek into
system processes such as svchost.
Kindly snip quoted text appropriately. You did not need to quote
nearly 200 lines for a one sentence response.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Apr 18 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
15069
by: Roger | last post by:
I would like to get a list of running processes on a remote machine. How is this possible via VB.Net? Is it possible? Can someone point me in the right direction. thanks, rog
3
1988
by: kyle.tk | last post by:
So I have a central list of python objects that I want to be able to share between different process that are possibly on different computers on the network. Some of the processes will add objects to list and another process will be a GUI that will view objects in the list. I want this all to happen in real-time (e.g once a processes adds an object to the list the GUI will see it.) What would be the best way to accomplish this. Some of...
1
2423
by: Leonid | last post by:
Hello, How to get the list of running processes in VC++ (Similar to Task Manager->Processes window) Regards Leonid
4
1499
by: Eran AMiel | last post by:
Hi I am trying to get the processes list from using the code attached. In win xp it works great but in win 2000 i get only one process the idle Any Ideas ? The Code : Dim Vec As System.Diagnostics.Process() Dim V As Object = Diagnostics.Process.GetProcesses.GetEnumerator Vec = Diagnostics.Process.GetProcesses
2
4809
by: J | last post by:
Hello, I'm using the following snippet of code to try and get a list of all the printers installed on this machine. I have installed some network printers on this machine and they just won't show up! This is in a webservice method, if that makes any differance. Also, when I change the query to "Select * from Win32_Process" I do return many processes, any ideas to what might be causing this?
2
7693
by: masterra | last post by:
Hello all! I am trying to create a replacement task bar for windows (don't ask why..) in C#, and have run into some troubles. I am aware that i can get a list of running processes (using System.Diagnostics.Process), however this is ALL processes, and i ONLY WANT the ones that would show on the task bar normally. I noticed that if i looked at a Processes MainWindowTitle property, i
5
1815
by: Andy Baker | last post by:
Our VB.NET 2003 application requires several processes to run overnight. I have written a program to perform these processes with a simple user interface to allow the user to switch various options on/off, and using the code from VB.NET programmer's cookbook (NotifyIcon) have it running in the system tray. When windows xp is started, the overnight routines program starts, and the icon appears in the system tray. I then use a timer to...
6
9650
by: happiness4all | last post by:
how to get list of pid of all processes running on the system using c++ in windows???
12
2453
by: Mark S. | last post by:
Hello, The app in question is lives on a Windows 2003 server with .NET 2.0 running IIS 6. The page of the app in question processes 2000 get requests a second during peak loads. The app uses a Static Object. In this object is a generic List<String>. For every page request this list is looped over only reading not writing each value.
0
9555
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10514
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10287
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10042
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6826
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5479
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3770
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2956
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.