目录
1. 操作系统版本
2. 获取CPU信息
3. 获取内存信息
4. 获取硬盘信息
5.获取网络接口信息
6.获取计算机名称、用户名
在C++中,你可以使用Windows API函数来获取Windows系统的各种信息。以下是一些常见的API函数和示例代码,用于获取Windows系统信息:
1. 操作系统版本
#include <windows.h>
#include <iostream>
int main() {OSVERSIONINFOEX osvi;ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);if (GetVersionEx((OSVERSIONINFO*)&osvi)) {std::cout << "Operating System Version: " << osvi.dwMajorVersion << "." << osvi.dwMinorVersion << std::endl;}
return 0;
}
2. 获取CPU信息
你可以使用GetSystemInfo函数来获取CPU架构信息。
#include <windows.h>
#include <iostream>
int main() {SYSTEM_INFO sysInfo;GetSystemInfo(&sysInfo);
std::cout << "Processor Architecture: ";switch (sysInfo.wProcessorArchitecture) {case PROCESSOR_ARCHITECTURE_AMD64:std::cout << "AMD64" << std::endl;break;case PROCESSOR_ARCHITECTURE_IA64:std::cout << "IA64" << std::endl;break;case PROCESSOR_ARCHITECTURE_INTEL:std::cout << "x86" << std::endl;break;// 其他架构... default:std::cout << "Unknown" << std::endl;break;}
return 0;
}
3. 获取内存信息
你可以使用GlobalMemoryStatusEx函数来获取内存信息。
#include <windows.h>
#include <iostream> int main() { MEMORYSTATUSEX statex; statex.dwLength = sizeof (statex); GlobalMemoryStatusEx (&statex); std::cout << "Total physical memory: " << statex.ullTotalPhys / (1024 * 1024) << " MB" << std::endl; std::cout << "Available physical memory: " << statex.ullAvailPhys / (1024 * 1024) << " MB" << std::endl; return 0;
}
4. 获取硬盘信息
你可以使用GetDiskFreeSpaceEx函数来获取硬盘的可用空间。
#include <windows.h>
#include <iostream>
int main() {ULARGE_INTEGER freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes;if (GetDiskFreeSpaceEx(L"C:\\", &freeBytesAvailable, &totalNumberOfBytes, &totalNumberOfFreeBytes)) {std::cout << "Total number of bytes: "<< totalNumberOfBytes.QuadPart / (1024 * 1024 * 1024) << " GB" << std::endl;std::cout << "Free bytes available: "<< freeBytesAvailable.QuadPart / (1024 * 1024 * 1024) << " GB" << std::endl;}else {std::cerr << "Failed to get disk space information." << std::endl;}
return 0;
}
5.获取网络接口信息
使用GetAdaptersInfo函数来获取网络接口的信息。
#include <winsock2.h>
#include <iphlpapi.h>
#include <iostream>
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
int main() {PIP_ADAPTER_INFO pAdapterInfo;ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);pAdapterInfo = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) {std::cerr << "Error allocating memory needed to call GetAdaptersInfo" << std::endl;return 1;}
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {free(pAdapterInfo);pAdapterInfo = (IP_ADAPTER_INFO*)malloc(ulOutBufLen);if (pAdapterInfo == NULL) {std::cerr << "Error allocating memory needed to call GetAdaptersInfo" << std::endl;return 1;}}
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == NO_ERROR) {PIP_ADAPTER_INFO pAdapter = pAdapterInfo;while (pAdapter) {std::wcout << L"Adapter Name: " << pAdapter->AdapterName << std::endl;std::wcout << L"Description: " << pAdapter->Description << std::endl;std::cout << "IP Address: ";for (PIP_ADDR_STRING pIpAddressList = &pAdapter->IpAddressList; pIpAddressList; pIpAddressList = pIpAddressList->Next) {std::cout << pIpAddressList->IpAddress.String << " ";}std::cout << std::endl;
// Move to the next adapter pAdapter = pAdapter->Next;}}else {std::cerr << "GetAdaptersInfo failed with error: " << GetLastError() << std::endl;}
if (pAdapterInfo) {free(pAdapterInfo);}
return 0;
}
6.获取计算机名称、用户名
在C++中,你可以使用Windows API函数GetComputerName来获取Windows计算机的名称。
#include <windows.h>
#include <stdio.h>
void GetSysInfo() {
wchar_t szComputerName[MAXBYTE] = { 0 };wchar_t szUserName[MAXBYTE] = { 0 };unsigned long nSize = MAXBYTE;OSVERSIONINFO OsVer;OsVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);GetVersionEx(&OsVer);
if (OsVer.dwPlatformId == VER_PLATFORM_WIN32_NT) {if (OsVer.dwMajorVersion == 5 && OsVer.dwMinorVersion == 1) {wprintf(L"window xp %s\r\n", OsVer.szCSDVersion);}else if (OsVer.dwMajorVersion == 5 && OsVer.dwMinorVersion == 0) {wprintf(L"windows 2k \r\n");}else if (OsVer.dwMajorVersion == 6 && OsVer.dwMinorVersion >= 1) {printf("windows win7 \r\n");}}else {wprintf(L"ohter system \r\n");}GetComputerName(szComputerName, &nSize);wprintf(L"computer name is %s \r\n", szComputerName);
nSize = MAXBYTE;GetUserName(szUserName, &nSize);wprintf(L"user name is %s \r\n", szUserName);
}
int main()
{GetSysInfo();system("pause");return 0;
}
运行以上代码可能会出现错误
error C4996: 'GetVersionExW': 被声明为已否决
需要关闭SDL
在Visual Studio(VS)中,SDL(Security Development Lifecycle)检查是一个编译时选项,用于增强代码的安全性。SDL是微软开发的一种迭代的安全开发过程,旨在确保在开发早期就考虑并实现安全性和隐私保护。通过结合SDL和Visual Studio的编译时检查,开发人员可以在编译阶段就发现和修复潜在的安全漏洞。
在Visual Studio中启用SDL检查后,编译器会执行一系列额外的安全检查,以确保代码符合安全性和隐私保护的最佳实践。这些检查可能包括检查缓冲区溢出、验证输入数据、防止跨站脚本攻击(XSS)等。如果编译器在代码中发现了潜在的安全问题,它会生成警告或错误,以便开发人员能够及时发现并修复这些问题。
通过启用SDL检查,开发人员可以在整个开发过程中实现更高的安全性和隐私保护水平。这有助于减少在软件发布后发现安全漏洞的风险,并提高用户对软件的信任和满意度。