编写代码获得当前用户名(GetUserName)、系统目录(GetSystemDirectory)、Windows所在目录(GetWindowsDirectory)、环境变量PATH的值等信息(ExpandEnvironmentStrings),获得操作系统的版本信息(GetVersionEx),不要使用GetVersionEx
时间: 2023-06-19 17:34:40 浏览: 71
好的,可以使用GetVersionEx函数的替代函数VerifyVersionInfo来获得操作系统的版本信息。下面是代码示例:
```c++
#include <iostream>
#include <Windows.h>
int main()
{
// 获取当前用户名
char username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
GetUserNameA(username, &username_len);
std::cout << "Current user: " << username << std::endl;
// 获取系统目录
char sys_dir[MAX_PATH];
GetSystemDirectoryA(sys_dir, MAX_PATH);
std::cout << "System directory: " << sys_dir << std::endl;
// 获取Windows所在目录
char win_dir[MAX_PATH];
GetWindowsDirectoryA(win_dir, MAX_PATH);
std::cout << "Windows directory: " << win_dir << std::endl;
// 获取环境变量PATH的值
char path[MAX_PATH];
DWORD path_len = ExpandEnvironmentStringsA("%PATH%", path, MAX_PATH);
if (path_len > MAX_PATH || path_len == 0) {
std::cerr << "Failed to get PATH environment variable." << std::endl;
return 1;
}
std::cout << "PATH: " << path << std::endl;
// 获取操作系统版本信息
OSVERSIONINFOEXA osvi = { sizeof(osvi) };
DWORDLONG dwlConditionMask = 0;
VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
VER_SET_CONDITION(dwlConditionMask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
osvi.dwMajorVersion = 10;
osvi.dwMinorVersion = 0;
osvi.wServicePackMajor = 0;
if (VerifyVersionInfoA(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask)) {
std::cout << "Operating system version: Windows 10 or newer" << std::endl;
}
else {
std::cout << "Operating system version: Older than Windows 10" << std::endl;
}
return 0;
}
```
阅读全文