1 // Written by Zoltan Csizmadia, zoltan_csizmadia@yahoo.com
2 // For companies(Austin,TX): If you would like to get my resume, send an email.
4 // The source is free, but if you want to use it, mention my name and e-mail address
6 //////////////////////////////////////////////////////////////////////////////////////
10 #ifndef SYSTEMINFO_H_INCLUDED
11 #define SYSTEMINFO_H_INCLUDED
14 #error You need Windows NT to use this source code. Define WINNT!
19 #endif // _MSC_VER > 1000
21 #pragma warning( disable : 4786 )
22 #pragma warning( disable : 4200 )
31 ///////////////////////////////////////////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////////////////
37 typedef struct _UNICODE_STRING
44 ///////////////////////////////////////////////////////////////////////////////
48 ///////////////////////////////////////////////////////////////////////////////
56 ///////////////////////////////////////////////////////////////////////
57 // String conversion functions
59 // From wide char string to string
60 static void LPCWSTR2string( LPCWSTR strW
, string
& str
);
61 // From unicode string to string
62 static void Unicode2string( UNICODE_STRING
* strU
, string
& str
);
64 ///////////////////////////////////////////////////////////////////////
65 // File name conversion functions
67 static BOOL
GetDeviceFileName( LPCTSTR
, string
& );
68 static BOOL
GetFsFileName( LPCTSTR
, string
& );
70 ///////////////////////////////////////////////////////////////////////
71 // Information functions
73 static DWORD
GetNTMajorVersion();
76 ///////////////////////////////////////////////////////////////////////////////
80 ///////////////////////////////////////////////////////////////////////////////
85 typedef DWORD (WINAPI
*PNtQueryObject
)( HANDLE
, DWORD
, VOID
*, DWORD
, VOID
* );
86 typedef DWORD (WINAPI
*PNtQuerySystemInformation
)( DWORD
, VOID
*, DWORD
, ULONG
* );
87 typedef DWORD (WINAPI
*PNtQueryInformationThread
)(HANDLE
, ULONG
, PVOID
, DWORD
, DWORD
* );
88 typedef DWORD (WINAPI
*PNtQueryInformationFile
)(HANDLE
, PVOID
, PVOID
, DWORD
, DWORD
);
89 typedef DWORD (WINAPI
*PNtQueryInformationProcess
)(HANDLE
, DWORD
, PVOID
, DWORD
, PVOID
);
92 static PNtQuerySystemInformation NtQuerySystemInformation
;
93 static PNtQueryObject NtQueryObject
;
94 static PNtQueryInformationThread NtQueryInformationThread
;
95 static PNtQueryInformationFile NtQueryInformationFile
;
96 static PNtQueryInformationProcess NtQueryInformationProcess
;
98 static BOOL NtDllStatus
;
100 static DWORD dwNTMajorVersion
;
106 ///////////////////////////////////////////////////////////////////////////////
108 // SystemProcessInformation
110 ///////////////////////////////////////////////////////////////////////////////
112 class SystemProcessInformation
: public INtDll
115 typedef LARGE_INTEGER QWORD
;
117 typedef struct _PROCESS_BASIC_INFORMATION
{
119 PVOID PebBaseAddress
;
122 DWORD UniqueProcessId
;
123 DWORD InheritedFromUniqueProcessId
;
124 } PROCESS_BASIC_INFORMATION
;
126 typedef struct _VM_COUNTERS
128 DWORD PeakVirtualSize
;
130 DWORD PageFaultCount
;
131 DWORD PeakWorkingSetSize
;
132 DWORD WorkingSetSize
;
133 DWORD QuotaPeakPagedPoolUsage
;
134 DWORD QuotaPagedPoolUsage
;
135 DWORD QuotaPeakNonPagedPoolUsage
;
136 DWORD QuotaNonPagedPoolUsage
;
138 DWORD PeakPagefileUsage
;
141 typedef struct _SYSTEM_THREAD
151 DWORD dContextSwitches
;
152 DWORD dThreadState
; // 2=running, 5=waiting
161 typedef struct _SYSTEM_PROCESS_INFORMATION
174 UNICODE_STRING usName
;
176 DWORD dUniqueProcessId
;
177 DWORD dInheritedFromUniqueProcessId
;
181 VM_COUNTERS VmCounters
;
183 SYSTEM_THREAD Threads
[1];
184 } SYSTEM_PROCESS_INFORMATION
;
186 enum { BufferSize
= 0x10000 };
189 SystemProcessInformation( BOOL bRefresh
= FALSE
);
190 virtual ~SystemProcessInformation();
195 map
< DWORD
, SYSTEM_PROCESS_INFORMATION
*> m_ProcessInfos
;
196 SYSTEM_PROCESS_INFORMATION
* m_pCurrentProcessInfo
;
202 ///////////////////////////////////////////////////////////////////////////////
204 // SystemThreadInformation
206 ///////////////////////////////////////////////////////////////////////////////
208 class SystemThreadInformation
: public INtDll
211 typedef struct _THREAD_INFORMATION
216 } THREAD_INFORMATION
;
219 typedef struct _BASIC_THREAD_INFORMATION
{
227 } BASIC_THREAD_INFORMATION
;
230 SystemThreadInformation( DWORD pID
= (DWORD
)-1, BOOL bRefresh
= FALSE
);
235 list
< THREAD_INFORMATION
> m_ThreadInfos
;
240 ///////////////////////////////////////////////////////////////////////////////
242 // SystemHandleInformation
244 ///////////////////////////////////////////////////////////////////////////////
246 class SystemHandleInformation
: public INtDll
253 OB_TYPE_SYMBOLIC_LINK
,
265 OB_TYPE_WINDOW_STATION
,
270 OB_TYPE_WAITABLE_PORT
,
275 //OB_TYPE_CONTROLLER,
278 OB_TYPE_IO_COMPLETION
,
283 typedef struct _SYSTEM_HANDLE
292 typedef struct _SYSTEM_HANDLE_INFORMATION
295 SYSTEM_HANDLE Handles
[1];
296 } SYSTEM_HANDLE_INFORMATION
;
299 typedef struct _GetFileNameThreadParam
304 } GetFileNameThreadParam
;
307 SystemHandleInformation( DWORD pID
= (DWORD
)-1, BOOL bRefresh
= FALSE
, LPCTSTR lpTypeFilter
= NULL
);
308 ~SystemHandleInformation();
310 BOOL
SetFilter( LPCTSTR lpTypeFilter
, BOOL bRefresh
= TRUE
);
311 const string
& GetFilter();
316 //Information functions
317 static BOOL
GetType( HANDLE
, WORD
&, DWORD processId
= GetCurrentProcessId() );
318 static BOOL
GetTypeToken( HANDLE
, string
&, DWORD processId
= GetCurrentProcessId() );
319 static BOOL
GetTypeFromTypeToken( LPCTSTR typeToken
, WORD
& type
);
320 static BOOL
GetNameByType( HANDLE
, WORD
, string
& str
, DWORD processId
= GetCurrentProcessId());
321 static BOOL
GetName( HANDLE
, string
&, DWORD processId
= GetCurrentProcessId() );
323 //Thread related functions
324 static BOOL
GetThreadId( HANDLE
, DWORD
&, DWORD processId
= GetCurrentProcessId() );
326 //Process related functions
327 static BOOL
GetProcessId( HANDLE
, DWORD
&, DWORD processId
= GetCurrentProcessId() );
328 static BOOL
GetProcessPath( HANDLE h
, string
& strPath
, DWORD processId
= GetCurrentProcessId());
330 //File related functions
331 static BOOL
GetFileName( HANDLE
, string
&, DWORD processId
= GetCurrentProcessId() );
334 //For remote handle support
335 static HANDLE
OpenProcess( DWORD processId
);
336 static HANDLE
DuplicateHandle( HANDLE hProcess
, HANDLE hRemote
);
339 static void GetFileNameThread( PVOID
/* GetFileNameThreadParam* */ );
340 BOOL
IsSupportedHandle( SYSTEM_HANDLE
& handle
);
343 list
< SYSTEM_HANDLE
> m_HandleInfos
;
347 string m_strTypeFilter
;
350 ///////////////////////////////////////////////////////////////////////////////
352 // SystemModuleInformation
354 ///////////////////////////////////////////////////////////////////////////////
356 class SystemModuleInformation
359 typedef struct _MODULE_INFO
362 TCHAR FullPath
[_MAX_PATH
];
367 typedef DWORD (WINAPI
*PEnumProcessModules
)(
368 HANDLE hProcess
, // handle to process
369 HMODULE
*lphModule
, // array of module handles
370 DWORD cb
, // size of array
371 LPDWORD lpcbNeeded
// number of bytes required
374 typedef DWORD (WINAPI
*PGetModuleFileNameEx
)(
375 HANDLE hProcess
, // handle to process
376 HMODULE hModule
, // handle to module
377 LPTSTR lpFilename
, // path buffer
378 DWORD nSize
// maximum characters to retrieve
382 SystemModuleInformation( DWORD pID
= (DWORD
)-1, BOOL bRefresh
= FALSE
);
387 void GetModuleListForProcess( DWORD processID
);
391 list
< MODULE_INFO
> m_ModuleInfos
;
394 PEnumProcessModules m_EnumProcessModules
;
395 PGetModuleFileNameEx m_GetModuleFileNameEx
;
398 ///////////////////////////////////////////////////////////////////////////////
400 // SystemWindowInformation
402 ///////////////////////////////////////////////////////////////////////////////
404 class SystemWindowInformation
407 enum { MaxCaptionSize
= 1024 };
409 typedef struct _WINDOW_INFO
412 TCHAR Caption
[MaxCaptionSize
];
417 SystemWindowInformation( DWORD pID
= (DWORD
)-1, BOOL bRefresh
= FALSE
);
422 static BOOL CALLBACK
EnumerateWindows( HWND hwnd
, LPARAM lParam
);
426 list
< WINDOW_INFO
> m_WindowInfos
;