1
/*******************************************************************************
2
* Copyright (c) 2002 - 2005 QNX Software Systems and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* QNX Software Systems - initial API and implementation
10
*******************************************************************************/
11
// ProcList.cpp : Defines the entry point for the console application.
16
#include "listtasks.h"
32
BOOL WINAPI Enum16( DWORD dwThreadId, WORD hMod16, WORD hTask16,
33
PSZ pszModName, PSZ pszFileName, LPARAM lpUserDefined ) ;
35
BOOL CALLBACK OutProcInfo( DWORD pid, WORD, LPSTR procName, LPARAM ) ;
37
int main(int argc, char* argv[])
39
EnumProcs(OutProcInfo, 0);
45
/*********************
47
*********************/
49
// The EnumProcs function takes a pointer to a callback function
50
// that will be called once per process in the system providing
51
// process EXE filename and process ID.
52
// Callback function definition:
53
// BOOL CALLBACK Proc( DWORD dw, LPCSTR lpstr, LPARAM lParam ) ;
55
// lpProc -- Address of callback routine.
57
// lParam -- A user-defined LPARAM value to be passed to
58
// the callback routine.
59
BOOL WINAPI EnumProcs( PROCENUMPROC lpProc, LPARAM lParam )
65
PROCESSENTRY32 procentry ;
68
DWORD dwSize, dwSize2, dwIndex ;
71
char szFileName[ MAX_PATH ] ;
72
EnumInfoStruct sInfo ;
74
// ToolHelp Function Pointers.
75
HANDLE (WINAPI *lpfCreateToolhelp32Snapshot)(DWORD,DWORD) ;
76
BOOL (WINAPI *lpfProcess32First)(HANDLE,LPPROCESSENTRY32) ;
77
BOOL (WINAPI *lpfProcess32Next)(HANDLE,LPPROCESSENTRY32) ;
79
// PSAPI Function Pointers.
80
BOOL (WINAPI *lpfEnumProcesses)( DWORD *, DWORD cb, DWORD * );
81
BOOL (WINAPI *lpfEnumProcessModules)( HANDLE, HMODULE *,
83
DWORD (WINAPI *lpfGetModuleFileNameEx)( HANDLE, HMODULE,
86
// VDMDBG Function Pointers.
87
INT (WINAPI *lpfVDMEnumTaskWOWEx)( DWORD,
88
TASKENUMPROCEX fp, LPARAM );
91
// Check to see if were running under Windows95 or
93
osver.dwOSVersionInfoSize = sizeof( osver ) ;
94
if( !GetVersionEx( &osver ) )
100
if( osver.dwPlatformId == VER_PLATFORM_WIN32_NT )
103
// Load library and get the procedures explicitly. We do
104
// this so that we don't have to worry about modules using
105
// this code failing to load under Windows 95, because
106
// it can't resolve references to the PSAPI.DLL.
107
hInstLib = LoadLibraryA( "PSAPI.DLL" ) ;
108
if( hInstLib == NULL )
111
hInstLib2 = LoadLibraryA( "VDMDBG.DLL" ) ;
112
if( hInstLib2 == NULL )
115
// Get procedure addresses.
116
lpfEnumProcesses = (BOOL(WINAPI *)(DWORD *,DWORD,DWORD*))
117
GetProcAddress( hInstLib, "EnumProcesses" ) ;
118
lpfEnumProcessModules = (BOOL(WINAPI *)(HANDLE, HMODULE *,
119
DWORD, LPDWORD)) GetProcAddress( hInstLib,
120
"EnumProcessModules" ) ;
121
lpfGetModuleFileNameEx =(DWORD (WINAPI *)(HANDLE, HMODULE,
122
LPTSTR, DWORD )) GetProcAddress( hInstLib,
123
"GetModuleFileNameExA" ) ;
124
lpfVDMEnumTaskWOWEx =(INT(WINAPI *)( DWORD, TASKENUMPROCEX,
125
LPARAM))GetProcAddress( hInstLib2, "VDMEnumTaskWOWEx" );
126
if( lpfEnumProcesses == NULL ||
127
lpfEnumProcessModules == NULL ||
128
lpfGetModuleFileNameEx == NULL ||
129
lpfVDMEnumTaskWOWEx == NULL)
131
FreeLibrary( hInstLib ) ;
132
FreeLibrary( hInstLib2 ) ;
136
// Call the PSAPI function EnumProcesses to get all of the
137
// ProcID's currently in the system.
138
// NOTE: In the documentation, the third parameter of
139
// EnumProcesses is named cbNeeded, which implies that you
140
// can call the function once to find out how much space to
141
// allocate for a buffer and again to fill the buffer.
142
// This is not the case. The cbNeeded parameter returns
143
// the number of PIDs returned, so if your buffer size is
144
// zero cbNeeded returns zero.
145
// NOTE: The "HeapAlloc" loop here ensures that we
146
// actually allocate a buffer large enough for all the
147
// PIDs in the system.
148
dwSize2 = 256 * sizeof( DWORD ) ;
154
HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
157
lpdwPIDs = (LPDWORD)HeapAlloc( GetProcessHeap(), 0, dwSize2 );
158
if( lpdwPIDs == NULL )
160
FreeLibrary( hInstLib ) ;
161
FreeLibrary( hInstLib2 ) ;
164
if( !lpfEnumProcesses( lpdwPIDs, dwSize2, &dwSize ) )
166
HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
167
FreeLibrary( hInstLib ) ;
168
FreeLibrary( hInstLib2 ) ;
171
}while( dwSize == dwSize2 ) ;
173
// How many ProcID's did we get?
174
dwSize /= sizeof( DWORD ) ;
176
// Loop through each ProcID.
177
for( dwIndex = 0 ; dwIndex < dwSize ; dwIndex++ )
180
// Open the process (if we can... security does not
181
// permit every process in the system).
182
hProcess = OpenProcess(
183
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
184
FALSE, lpdwPIDs[ dwIndex ] ) ;
185
if( hProcess != NULL )
187
// Here we call EnumProcessModules to get only the
188
// first module in the process this is important,
189
// because this will be the .EXE module for which we
190
// will retrieve the full path name in a second.
191
if( lpfEnumProcessModules( hProcess, &hMod,
192
sizeof( hMod ), &dwSize2 ) )
194
// Get Full pathname:
195
if( !lpfGetModuleFileNameEx( hProcess, hMod,
196
szFileName, sizeof( szFileName ) ) )
201
CloseHandle( hProcess ) ;
203
// Regardless of OpenProcess success or failure, we
204
// still call the enum func with the ProcID.
205
if(!lpProc( lpdwPIDs[dwIndex], 0, szFileName, lParam))
208
// Did we just bump into an NTVDM?
209
if( _stricmp( szFileName+(strlen(szFileName)-9),
212
// Fill in some info for the 16-bit enum proc.
213
sInfo.dwPID = lpdwPIDs[dwIndex] ;
214
sInfo.lpProc = lpProc ;
215
sInfo.lParam = lParam ;
217
// Enum the 16-bit stuff.
218
lpfVDMEnumTaskWOWEx( lpdwPIDs[dwIndex],
219
(TASKENUMPROCEX) Enum16,
222
// Did our main enum func say quit?
228
HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
229
FreeLibrary( hInstLib2 ) ;
232
}else if( osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
236
hInstLib = LoadLibraryA( "Kernel32.DLL" ) ;
237
if( hInstLib == NULL )
240
// Get procedure addresses.
241
// We are linking to these functions of Kernel32
242
// explicitly, because otherwise a module using
243
// this code would fail to load under Windows NT,
244
// which does not have the Toolhelp32
245
// functions in the Kernel 32.
246
lpfCreateToolhelp32Snapshot=
247
(HANDLE(WINAPI *)(DWORD,DWORD))
248
GetProcAddress( hInstLib,
249
"CreateToolhelp32Snapshot" ) ;
251
(BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
252
GetProcAddress( hInstLib, "Process32First" ) ;
254
(BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
255
GetProcAddress( hInstLib, "Process32Next" ) ;
256
if( lpfProcess32Next == NULL ||
257
lpfProcess32First == NULL ||
258
lpfCreateToolhelp32Snapshot == NULL )
260
FreeLibrary( hInstLib ) ;
264
// Get a handle to a Toolhelp snapshot of the systems
266
hSnapShot = lpfCreateToolhelp32Snapshot(
267
TH32CS_SNAPPROCESS, 0 ) ;
268
if( hSnapShot == INVALID_HANDLE_VALUE )
270
FreeLibrary( hInstLib ) ;
274
// Get the first process' information.
275
procentry.dwSize = sizeof(PROCESSENTRY32) ;
276
bFlag = lpfProcess32First( hSnapShot, &procentry ) ;
278
// While there are processes, keep looping.
281
// Call the enum func with the filename and ProcID.
282
if(lpProc( procentry.th32ProcessID, 0,
283
procentry.szExeFile, lParam ))
285
procentry.dwSize = sizeof(PROCESSENTRY32) ;
286
bFlag = lpfProcess32Next( hSnapShot, &procentry );
296
FreeLibrary( hInstLib ) ;
301
BOOL WINAPI Enum16( DWORD dwThreadId, WORD hMod16, WORD hTask16,
302
PSZ pszModName, PSZ pszFileName, LPARAM lpUserDefined )
306
EnumInfoStruct *psInfo = (EnumInfoStruct *)lpUserDefined ;
308
bRet = psInfo->lpProc( psInfo->dwPID, hTask16, pszFileName,
313
psInfo->bEnd = TRUE ;
319
BOOL CALLBACK OutProcInfo( DWORD pid, WORD, LPSTR procName, LPARAM )
321
cout << setw(10) << pid << '\t' << procName << '\n';