467,118 Members | 999 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,118 developers. It's quick & easy.

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 ListProcessModules( DWORD dwPID );
BOOL KillProcessFromName(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 = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( hProcessSnap == INVALID_HANDLE_VALUE )
{
printError( "CreateToolhelp32Snapshot (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_ACCESS, FALSE,
pe32.th32ProcessID );
if( hProcess == NULL )
printError( "OpenProcess" );
else
{
dwPriorityClass = GetPriorityClass( hProcess );
if( !dwPriorityClass )
printError( "GetPriorityClass" );
CloseHandle( hProcess );
}
//进程的相关信息
printf( "\n process ID = 0x%08X", pe32.th32ProcessID );//id号
// List the modules and threads associated with this process
ListProcessModules( pe32.th32ProcessID );
// ListProcessThreads( pe32.th32ProcessID );

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

CloseHandle( hProcessSnap );
return( TRUE );
}
//模块信息
BOOL ListProcessModules( DWORD dwPID )
{
HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
MODULEENTRY32 me32;
hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID );
if(hModuleSnap == INVALID_HANDLE_VALUE)
{
printError( "CreateToolhelp32Snapshot (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( "Module32First" ); // 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 KillProcessFromName(LPCTSTR name)//name为*要终*的进程的名称,Win9X则 包括路径
{
PROCESSENTRY32 pe;//定义一个PROCESSENTRY32结类型的变量
HANDLE hShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);// 创建快照句柄
HANDLE hProcess = INVALID_HANDLE_VALUE;
pe.dwSize=sizeof(PROCESSENTRY32);//一定要先为dwSize赋值
if (Process32First(hShot,&pe))
{
do{
if (strcmp(pe.szExeFile,name)==0) //判**进程是否为*要终*的进程
hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,pe.t h32ProcessID);
//如果是就利用其ID获得句柄
if( hProcess == INVALID_HANDLE_VALUE )
{
printError( "OpenProcess (of processes)" );
return( FALSE );
}
TerminateProcess(hProcess,0);//终*该进程
}while(Process32Next(hShot,&pe));
}
CloseHandle(hShot);//最后别忘记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
  • viewed: 3114
Share:
3 Replies
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.public.*
groups, such as microsoft.public.win32.programmer.

--
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 ListProcessModules( DWORD dwPID );
BOOL KillProcessFromName(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 = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( hProcessSnap == INVALID_HANDLE_VALUE )
{
printError( "CreateToolhelp32Snapshot (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_ACCESS, FALSE,
pe32.th32ProcessID );
if( hProcess == NULL )
printError( "OpenProcess" );
else
{
dwPriorityClass = GetPriorityClass( hProcess );
if( !dwPriorityClass )
printError( "GetPriorityClass" );
CloseHandle( hProcess );
}
//进程的相关信息
printf( "\n process ID = 0x%08X", pe32.th32ProcessID );//id号
// List the modules and threads associated with this process
ListProcessModules( pe32.th32ProcessID );
// ListProcessThreads( pe32.th32ProcessID );

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

CloseHandle( hProcessSnap );
return( TRUE );
}
//模块信息
BOOL ListProcessModules( DWORD dwPID )
{
HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
MODULEENTRY32 me32;
hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID );
if(hModuleSnap == INVALID_HANDLE_VALUE)
{
printError( "CreateToolhelp32Snapshot (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( "Module32First" ); // 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 KillProcessFromName(LPCTSTR name)//name为*要终*的进程的名称,Win9X则 包括路径
{
PROCESSENTRY32 pe;//定义一个PROCESSENTRY32结类型的变量
HANDLE hShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);// 创建快照句柄
HANDLE hProcess = INVALID_HANDLE_VALUE;
pe.dwSize=sizeof(PROCESSENTRY32);//一定要先为dwSize赋值
if (Process32First(hShot,&pe))
{
do{
if (strcmp(pe.szExeFile,name)==0) //判**进程是否为*要终*的进程
hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,pe.t h32ProcessID);
//如果是就利用其ID获得句柄
if( hProcess == INVALID_HANDLE_VALUE )
{
printError( "OpenProcess (of processes)" );
return( FALSE );
}
TerminateProcess(hProcess,0);//终*该进程
}while(Process32Next(hShot,&pe));
}
CloseHandle(hShot);//最后别忘记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****@aepnetworks.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.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Apr 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by kyle.tk | last post: by
1 post views Thread by Leonid | last post: by
4 posts views Thread by Eran AMiel | last post: by
2 posts views Thread by J | last post: by
2 posts views Thread by masterra@gmail.com | last post: by
5 posts views Thread by Andy Baker | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.