得到当前运行操作系统版本号,用于判断系统版本,在安全开发中还是经常被使用到的一段代码片段。
float getSysVersion(){
float f_ret;
typedef void(__stdcall*NTPROC)(DWORD*, DWORD*, DWORD*);
DWORD dwMajor, dwMinor, dwBuildNumber;
NTPROC proc = (NTPROC)GetProcAddress(
LoadLibrary("ntdll.dll"),
"RtlGetNtVersionNumbers"
); proc(&dwMajor, &dwMinor, &dwBuildNumber);
// win 10
if (dwMajor == 10 && dwMinor == 0){
f_ret = dwMajor + dwMinor*0.1;
return f_ret;
}
// win 8.1
if (dwMajor == 6 && dwMinor == 3){
f_ret = dwMajor + dwMinor*0.1;
return f_ret;
}
// win 8.1 以下
SYSTEM_INFO info;
GetSystemInfo(&info);
OSVERSIONINFOEX os;
os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
#pragma warning(disable:4996)
if(GetVersionEx((OSVERSIONINFO *)&os)){
f_ret = os.dwMajorVersion + os.dwMinorVersion*0.1;
}
return f_ret;
}
效果图:
获取 ReleaseId
string get_systemReleaseId(){
string releaseId = "";
HKEY hKey_return = NULL;
char keyValue[256];
DWORD keySzType;
DWORD keySize;
if(ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\",0,KEY_READ,&hKey_return)){
cout << "RegOpenKeyEx failed." << endl;
goto end;
}
if(ERROR_SUCCESS != RegQueryValueEx(hKey_return,"ReleaseId",0,&keySzType,(LPBYTE)&keyValue,&keySize)){
cout << "RegQueryValueEx failed." << endl;
goto end;
}
releaseId = keyValue;
end:
return releaseId;
}