~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to results/plugins/org.eclipse.cdt.core.win32/library/listtasks/listtasks.cpp

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
7
 
 *
8
 
 * Contributors:
9
 
 *     QNX Software Systems - initial API and implementation
10
 
 *******************************************************************************/
11
 
// ProcList.cpp : Defines the entry point for the console application.
12
 
//
13
 
 
14
 
#include "stdafx.h"
15
 
 
16
 
#include "listtasks.h"
17
 
#include <tlhelp32.h>
18
 
#include <vdmdbg.h>
19
 
#include <iostream>
20
 
#include <iomanip>
21
 
 
22
 
using namespace std;
23
 
 
24
 
typedef struct
25
 
{
26
 
  DWORD          dwPID ;
27
 
  PROCENUMPROC   lpProc ;
28
 
  DWORD          lParam ;
29
 
  BOOL           bEnd ;
30
 
} EnumInfoStruct ;
31
 
 
32
 
BOOL WINAPI Enum16( DWORD dwThreadId, WORD hMod16, WORD hTask16,
33
 
  PSZ pszModName, PSZ pszFileName, LPARAM lpUserDefined ) ;
34
 
 
35
 
BOOL CALLBACK OutProcInfo( DWORD pid, WORD, LPSTR procName, LPARAM ) ;
36
 
 
37
 
int main(int argc, char* argv[])
38
 
{
39
 
        EnumProcs(OutProcInfo, 0);
40
 
        return 0;
41
 
}
42
 
 
43
 
 
44
 
 
45
 
/*********************
46
 
EnumProc.cpp
47
 
*********************/ 
48
 
 
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 ) ;
54
 
// 
55
 
// lpProc -- Address of callback routine.
56
 
// 
57
 
// lParam -- A user-defined LPARAM value to be passed to
58
 
//           the callback routine.
59
 
BOOL WINAPI EnumProcs( PROCENUMPROC lpProc, LPARAM lParam )
60
 
{
61
 
  OSVERSIONINFO  osver ;
62
 
  HINSTANCE      hInstLib ;
63
 
  HINSTANCE      hInstLib2 ;
64
 
  HANDLE         hSnapShot ;
65
 
  PROCESSENTRY32 procentry ;
66
 
  BOOL           bFlag ;
67
 
  LPDWORD        lpdwPIDs ;
68
 
  DWORD          dwSize, dwSize2, dwIndex ;
69
 
  HMODULE        hMod ;
70
 
  HANDLE         hProcess ;
71
 
  char           szFileName[ MAX_PATH ] ;
72
 
  EnumInfoStruct sInfo ;
73
 
 
74
 
  // ToolHelp Function Pointers.
75
 
  HANDLE (WINAPI *lpfCreateToolhelp32Snapshot)(DWORD,DWORD) ;
76
 
  BOOL (WINAPI *lpfProcess32First)(HANDLE,LPPROCESSENTRY32) ;
77
 
  BOOL (WINAPI *lpfProcess32Next)(HANDLE,LPPROCESSENTRY32) ;
78
 
 
79
 
  // PSAPI Function Pointers.
80
 
  BOOL (WINAPI *lpfEnumProcesses)( DWORD *, DWORD cb, DWORD * );
81
 
  BOOL (WINAPI *lpfEnumProcessModules)( HANDLE, HMODULE *,
82
 
     DWORD, LPDWORD );
83
 
  DWORD (WINAPI *lpfGetModuleFileNameEx)( HANDLE, HMODULE,
84
 
     LPTSTR, DWORD );
85
 
 
86
 
  // VDMDBG Function Pointers.
87
 
  INT (WINAPI *lpfVDMEnumTaskWOWEx)( DWORD,
88
 
     TASKENUMPROCEX  fp, LPARAM );
89
 
 
90
 
 
91
 
  // Check to see if were running under Windows95 or
92
 
  // Windows NT.
93
 
  osver.dwOSVersionInfoSize = sizeof( osver ) ;
94
 
  if( !GetVersionEx( &osver ) )
95
 
  {
96
 
     return FALSE ;
97
 
  }
98
 
 
99
 
  // If Windows NT:
100
 
  if( osver.dwPlatformId == VER_PLATFORM_WIN32_NT )
101
 
  {
102
 
 
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 )
109
 
        return FALSE ;
110
 
 
111
 
     hInstLib2 = LoadLibraryA( "VDMDBG.DLL" ) ;
112
 
     if( hInstLib2 == NULL )
113
 
        return FALSE ;
114
 
 
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)
130
 
        {
131
 
           FreeLibrary( hInstLib ) ;
132
 
           FreeLibrary( hInstLib2 ) ;
133
 
           return FALSE ;
134
 
        }
135
 
 
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 ) ;
149
 
     lpdwPIDs = NULL ;
150
 
     do
151
 
     {
152
 
        if( lpdwPIDs )
153
 
        {
154
 
           HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
155
 
           dwSize2 *= 2 ;
156
 
        }
157
 
        lpdwPIDs = (LPDWORD)HeapAlloc( GetProcessHeap(), 0, dwSize2 );
158
 
        if( lpdwPIDs == NULL )
159
 
        {
160
 
           FreeLibrary( hInstLib ) ;
161
 
           FreeLibrary( hInstLib2 ) ;
162
 
           return FALSE ;
163
 
        }
164
 
        if( !lpfEnumProcesses( lpdwPIDs, dwSize2, &dwSize ) )
165
 
        {
166
 
           HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
167
 
           FreeLibrary( hInstLib ) ;
168
 
           FreeLibrary( hInstLib2 ) ;
169
 
           return FALSE ;
170
 
        }
171
 
     }while( dwSize == dwSize2 ) ;
172
 
 
173
 
     // How many ProcID's did we get?
174
 
     dwSize /= sizeof( DWORD ) ;
175
 
 
176
 
     // Loop through each ProcID.
177
 
     for( dwIndex = 0 ; dwIndex < dwSize ; dwIndex++ )
178
 
     {
179
 
        szFileName[0] = 0 ;
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 )
186
 
        {
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 ) )
193
 
           {
194
 
              // Get Full pathname:
195
 
              if( !lpfGetModuleFileNameEx( hProcess, hMod,
196
 
                 szFileName, sizeof( szFileName ) ) )
197
 
              {
198
 
                 szFileName[0] = 0 ;
199
 
                }
200
 
           }
201
 
           CloseHandle( hProcess ) ;
202
 
        }
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))
206
 
           break ;
207
 
 
208
 
        // Did we just bump into an NTVDM?
209
 
        if( _stricmp( szFileName+(strlen(szFileName)-9),
210
 
           "NTVDM.EXE")==0)
211
 
        {
212
 
           // Fill in some info for the 16-bit enum proc.
213
 
           sInfo.dwPID = lpdwPIDs[dwIndex] ;
214
 
           sInfo.lpProc = lpProc ;
215
 
           sInfo.lParam = lParam ;
216
 
           sInfo.bEnd = FALSE ;
217
 
           // Enum the 16-bit stuff.
218
 
           lpfVDMEnumTaskWOWEx( lpdwPIDs[dwIndex],
219
 
              (TASKENUMPROCEX) Enum16,
220
 
              (LPARAM) &sInfo);
221
 
 
222
 
           // Did our main enum func say quit?
223
 
           if(sInfo.bEnd)
224
 
              break ;
225
 
        }
226
 
     }
227
 
 
228
 
     HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
229
 
     FreeLibrary( hInstLib2 ) ;
230
 
 
231
 
  // If Windows 95:
232
 
  }else if( osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
233
 
  {
234
 
 
235
 
 
236
 
     hInstLib = LoadLibraryA( "Kernel32.DLL" ) ;
237
 
     if( hInstLib == NULL )
238
 
        return FALSE ;
239
 
 
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" ) ;
250
 
     lpfProcess32First=
251
 
        (BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
252
 
        GetProcAddress( hInstLib, "Process32First" ) ;
253
 
     lpfProcess32Next=
254
 
        (BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
255
 
        GetProcAddress( hInstLib, "Process32Next" ) ;
256
 
     if( lpfProcess32Next == NULL ||
257
 
        lpfProcess32First == NULL ||
258
 
        lpfCreateToolhelp32Snapshot == NULL )
259
 
     {
260
 
        FreeLibrary( hInstLib ) ;
261
 
        return FALSE ;
262
 
     }
263
 
 
264
 
     // Get a handle to a Toolhelp snapshot of the systems
265
 
     // processes.
266
 
     hSnapShot = lpfCreateToolhelp32Snapshot(
267
 
        TH32CS_SNAPPROCESS, 0 ) ;
268
 
     if( hSnapShot == INVALID_HANDLE_VALUE )
269
 
     {
270
 
        FreeLibrary( hInstLib ) ;
271
 
        return FALSE ;
272
 
     }
273
 
 
274
 
     // Get the first process' information.
275
 
     procentry.dwSize = sizeof(PROCESSENTRY32) ;
276
 
     bFlag = lpfProcess32First( hSnapShot, &procentry ) ;
277
 
 
278
 
     // While there are processes, keep looping.
279
 
     while( bFlag )
280
 
     {
281
 
        // Call the enum func with the filename and ProcID.
282
 
        if(lpProc( procentry.th32ProcessID, 0,
283
 
           procentry.szExeFile, lParam ))
284
 
        {
285
 
           procentry.dwSize = sizeof(PROCESSENTRY32) ;
286
 
           bFlag = lpfProcess32Next( hSnapShot, &procentry );
287
 
        }else
288
 
           bFlag = FALSE ;
289
 
     }
290
 
 
291
 
 
292
 
  }else
293
 
     return FALSE ;
294
 
 
295
 
  // Free the library.
296
 
  FreeLibrary( hInstLib ) ;
297
 
 
298
 
  return TRUE ;
299
 
}
300
 
 
301
 
BOOL WINAPI Enum16( DWORD dwThreadId, WORD hMod16, WORD hTask16,
302
 
  PSZ pszModName, PSZ pszFileName, LPARAM lpUserDefined )
303
 
{
304
 
  BOOL bRet ;
305
 
 
306
 
  EnumInfoStruct *psInfo = (EnumInfoStruct *)lpUserDefined ;
307
 
 
308
 
  bRet = psInfo->lpProc( psInfo->dwPID, hTask16, pszFileName,
309
 
     psInfo->lParam ) ;
310
 
 
311
 
  if(!bRet)
312
 
  {
313
 
     psInfo->bEnd = TRUE ;
314
 
  }
315
 
 
316
 
  return !bRet;
317
 
318
 
 
319
 
BOOL CALLBACK OutProcInfo( DWORD pid, WORD, LPSTR procName, LPARAM ) 
320
 
{
321
 
        cout << setw(10) <<  pid << '\t' << procName << '\n';
322
 
        return TRUE;
323
 
}