1.PSAPI法
2.ToolHelpApi
3.WTSOpenServer法
4.使用Native API我们使用NtQuerySystemInformation
#include <windows.h>
#include <iostream>
#include <tchar.h>
using namespace std;
#include "MyNtdll.h"//定义了函数指针和各种结构体
#define DEF_BUF_SIZE 0x1000
PFNNtQuerySystemInformation NtQuerySystemInformation ;
BOOL Initial ()
{
// 检测当前进程中是否存在ntdll.dll
HMODULE hMod = GetModuleHandle(_T("ntdll.dll"));
if ( hMod == NULL )
{
// 如果不存在,就使用LoadLibrary来加载
hMod = LoadLibrary (_T("ntdll.dll")) ;
if ( hMod == NULL )
return FALSE ;
}
// 取得函数地址
NtQuerySystemInformation =(PFNNtQuerySystemInformation)GetProcAddress(hMod,"NtQuerySystemInformation") ;
return TRUE ;
}
VOID DisplayProcessInformation ( LPBYTE lpBuf )
{
cout << "///" << endl ;
cout << "// Process Information //" << endl ;
cout << "///" << endl ;
PSYSTEM_PROCESSES pSysProcess = (PSYSTEM_PROCESSES)lpBuf ;
while ( TRUE )
{
cout << "ProcessName:" << '\t' ;
if ( pSysProcess->ProcessName.Buffer != NULL )
printf ("%30S", pSysProcess->ProcessName.Buffer ) ;
cout << endl ;
cout << "InheritedFromProcessId:\t\t" << pSysProcess->InheritedFromProcessId << endl ;
cout << "ProcessId:\t\t\t" << pSysProcess->ProcessId << endl ;
cout << "HandleCount:\t\t\t" << pSysProcess->HandleCount << endl ;
cout << "ThreadCount:\t\t\t" << pSysProcess->ThreadCount << endl ;
cout << "-------------------------------------------------------------------------" << endl ;
if ( pSysProcess->NextEntryDelta == 0 )
break ;
pSysProcess = (PSYSTEM_PROCESSES)( (DWORD)pSysProcess + pSysProcess->NextEntryDelta ) ;
}
}
VOID QuerySystemInformation (SYSTEM_INFORMATION_CLASS SystemInformationClass)
{
NTSTATUS status ;
UINT nSize = DEF_BUF_SIZE ;
LPBYTE lpBuf = NULL ;
// 由于事先并不知道需要多少空间来存储进程信息
// 因而采用循环测试法,
while ( TRUE )
{
// 动态分配空间,用来存储进程信息
if ( ( lpBuf = new BYTE [ nSize ] ) == NULL )
{
cout << "Allocate memory failed!" << endl ;
return ;
}
// 枚举进程信息
status = NtQuerySystemInformation(SystemInformationClass,lpBuf,nSize,0) ;
if ( !NT_SUCCESS(status))
{
// 检测是否返回缓冲区不够大
if ( status == STATUS_INFO_LENGTH_MISMATCH )
{
nSize += DEF_BUF_SIZE ;
delete lpBuf ;
continue ;
}
else
{
cout << "NtQuerySystemInformation ErrorCode=0x" << hex << status << endl ;
return ;
}
}
else
break ;
}
// 输出进程信息
DisplayProcessInformation (lpBuf) ;
delete lpBuf ;
}
int main()
{
if (Initial() == FALSE)
{
_tprintf(_T("Initial failed!"));
return 0;
}
QuerySystemInformation(SystemProcessesAndThreadsInformation );
return 0;
}