在某些情况下需要对一台电脑的信息进行搜集,大概来说通过 进程 服务 安装的软件 和系统类型就够了。
这些天把这些常用的信息搜集做成了一个类如下。
头文件
#include <vector> #include <tchar.h> #include <stdio.h> #include <windows.h> #include <Winsvc.h> #include <tlhelp32.h> struct ProbeStruct { TCHAR szTxt[MAX_PATH]; }; class CProbe { public: void GetProbe(); std::vector<ProbeStruct> MyProbeVector; protected: void GetWindows(); void GetProcess(); void GetService(); void GetRegedit(); void AddItem(TCHAR szBuffer[]); ProbeStruct ps; }; /* 简单的调用方法如下 void main() { CProbe myprobe; myprobe.GetProbe(); for (int i=0;i<myprobe.MyProbeVector.size();i++) { OutputDebugString(myprobe.MyProbeVector[i].szTxt); } }*/
实现的cpp文件如下
#include "CMyProbe.h" void CProbe::GetProbe() { MyProbeVector.clear(); GetWindows(); GetProcess(); GetService(); GetRegedit(); } void CProbe::AddItem(TCHAR szBuffer[]) { OutputDebugString(szBuffer); _stprintf(ps.szTxt,szBuffer); MyProbeVector.push_back(ps); } void CProbe::GetWindows() { OSVERSIONINFO osvi; memset(&osvi,0,sizeof(osvi)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); if(!GetVersionEx(&osvi)) { return; } switch (osvi.dwPlatformId) { case VER_PLATFORM_WIN32_NT: if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 ) AddItem(_T("windows2003")); if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 ) AddItem(_T("windowsxp")); if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) AddItem(_T("windows2000")); if ( osvi.dwMajorVersion <= 4 ) AddItem(_T("windowsnt")); if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0 ) AddItem(_T("windows2008")); if (osvi.dwMajorVersion >=6 && osvi.dwMinorVersion == 1) AddItem(_T("windows7")); break; } } void CProbe::GetProcess() { AddItem(_T("Process List")); HANDLE handle=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); if (handle==INVALID_HANDLE_VALUE ) { OutputDebugStringA("CreateToolhelp32Snapshot INVALID_HANDLE_VALUE"); } PROCESSENTRY32* info=new PROCESSENTRY32; info->dwSize=sizeof(PROCESSENTRY32); int i=0; if(Process32First(handle,info)) { if(GetLastError()==ERROR_NO_MORE_FILES ) { OutputDebugStringA("No More Process"); } else { i++; AddItem(info->szExeFile); while(Process32Next(handle,info)!=FALSE) { AddItem(info->szExeFile); i++; } } } CloseHandle(handle); } void CProbe::GetService() { AddItem(_T("Service List")); LPQUERY_SERVICE_CONFIG ServicesInfo = NULL; LPENUM_SERVICE_STATUS lpServices = NULL; DWORD nSize = 0; DWORD n; DWORD nResumeHandle = 0; DWORD dwServiceType = SERVICE_WIN32; SC_HANDLE schSCManager = NULL; if((schSCManager=OpenSCManager(NULL,NULL,SC_MANAGER_ENUMERATE_SERVICE))==NULL) { OutputDebugStringA("OpenSCManager Error/n"); return; } lpServices = (LPENUM_SERVICE_STATUS) LocalAlloc(LPTR, 64 * 1024); // Allocate Ram EnumServicesStatus(schSCManager, dwServiceType, SERVICE_STATE_ALL, (LPENUM_SERVICE_STATUS)lpServices, 64 * 1024, &nSize, &n, &nResumeHandle); for ( DWORD i = 0; i < n; i++) { AddItem(lpServices[i].lpServiceName); } } void CProbe::GetRegedit() { AddItem(_T("Regedit List")); HKEY hKey; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("software"),0,KEY_ALL_ACCESS,&hKey)!=ERROR_SUCCESS)//打开 { return; } TCHAR szName[MAX_PATH]; DWORD i=0,NameCnt,NameMaxLen; DWORD KeyLen,KeyCnt,KeyMaxLen,MaxDateLen; if(RegQueryInfoKey(hKey,NULL,NULL,NULL,&KeyCnt,&KeyMaxLen,NULL,&NameCnt,&NameMaxLen,&MaxDateLen,NULL,NULL)!=ERROR_SUCCESS) { return; } for(i=0;i<KeyCnt;i++) { KeyLen=KeyMaxLen*2+1; RegEnumKeyEx(hKey,i,szName,&KeyLen,NULL,NULL,NULL,NULL); AddItem(szName); } RegCloseKey(hKey); }