最近在做客户端往阿里云上迁移,也就是在客户端上要求实现如下功能,判断操作系统是32位还是64位,获取内存大小,获取CPU信息(格式:型号;核数),检测阿里云Virtio驱动是否存在。
下面附上各个功能模块的C++代码:
1. 判断操作系统是32位还是64位
BOOL IsSystem64() { typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); LPFN_ISWOW64PROCESS fnIsWow64Process; BOOL bIsWow64 = FALSE; fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(_T("kernel32")), "IsWow64Process"); if (NULL != fnIsWow64Process) { fnIsWow64Process(GetCurrentProcess(), &bIsWow64); } return bIsWow64; }
2. 获取内存大小(单位:MB)
要注意的地方是获取内存大小应该使用的API是GlobalMemoryStatusEx,因为对于超过4G内存的机器,GlobalMemoryStatus API就会返回错误的结果。详见MSDN官方说明文档。
而且非常重要的一点:代码中MemoryInfo.dwLengh = sizeof(MemoryInfo)这句初始化不能少,最开始没有写发现获取到的内存大小不对,后来查阅了官方文档的示例程序才发现。
DWORDLONG GetMemoryInfo() { MEMORYSTATUSEX MemoryInfo; MemoryInfo.dwLength = sizeof(MemoryInfo); GlobalMemoryStatusEx(&MemoryInfo); DWORDLONG dwSize = MemoryInfo.ullTotalPhys / (1024 * 1024); return dwSize; }
3. 获取CPU信息
CPU型号的信息在注册表中就可以查看到,核心数可以通过GetSystemInfo 这个API获取到
代码如下:
DWORD QueryRegKey(string KeyName,string ValueName,void *pValue,int Length) { CRegKey *pRegKey = new CRegKey(); ULONG nChars = 0; DWORD ret = 0; if(pRegKey->Open(HKEY_LOCAL_MACHINE,KeyName.c_str())!= ERROR_SUCCESS) { delete pRegKey; return EXIT_FAILURE; } nChars = Length; ret = pRegKey->QueryStringValue(ValueName.c_str(),(char *)pValue,&nChars); pRegKey->Close(); delete pRegKey; return ret; } string GetCpuInfo() { DWORD dwNum; char cpuInfo[128]; memset(cpuInfo, 0, sizeof(cpuInfo)); QueryRegKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", "ProcessorNameString", cpuInfo, sizeof(cpuInfo)); SYSTEM_INFO si; memset(&si, 0, sizeof(SYSTEM_INFO)); GetSystemInfo(&si); dwNum = si.dwNumberOfProcessors; sprintf(cpuInfo, "%s; %d %s", cpuInfo, dwNum, "cores"); return cpuInfo; }
4. 检测驱动:
主要是检测相应的注册表键值有没有被创建,drivers目录下有没有相应的sys文件。
然后传入相应的参数,判断balloon,netkvm,vioser,viostor,pvpanic这5个驱动是否正确安装
代码如下:
bool FindDriverReg(string target) { bool flag = true; CRegKey *pRegKey = new CRegKey(); string path = "SYSTEM\\CurrentControlSet\\Services\\"; path = path + target; if (pRegKey->Open(HKEY_LOCAL_MACHINE, path.c_str()) != ERROR_SUCCESS) flag = false; delete pRegKey; return flag; } bool FindDriverFile(string fileName) { bool flag = true; string path = "C:\\Windows\\System32\\drivers\\"; path = path + fileName; WIN32_FIND_DATA pData; HANDLE hFile = FindFirstFile(path.c_str(), &pData); if (hFile == INVALID_HANDLE_VALUE) { flag = false; } FindClose(hFile); return flag; }