获取Windows version的正确方法是什么(或多或少是现在已弃用的GetVersionEx
,但没有说谎。)
请不要告诉我我不应该使用版本检查,或者我应该使用VerifyVersionInfo
。它不是用于版本检查,而是用于生成报告。它也适用于Windows的未来版本(例如Windows 11或其他)
答案 0 :(得分:1)
如果您需要一个真实的Windows版本,其内部版本号未针对您的app.manifest
文件进行虚拟化,则可以从ntoskrnl.exe
获取Windows内核版本:
std::string GetNtDllVersion()
{
std::string versionString;
DWORD someHandle;
wchar_t systemFolderPath[ _MAX_PATH + 1 ];
UINT systemFolderPathSize = GetSystemDirectory( systemFolderPath, _MAX_PATH );
if ( systemFolderPathSize > 0 )
{
std::wstring ntDllPath( systemFolderPath, systemFolderPathSize );
ntDllPath += L"\\ntoskrnl.exe";
DWORD versionSize = GetFileVersionInfoSize( ntDllPath.c_str(), &someHandle );
if ( versionSize > 0 )
{
LPVOID dataPtr = malloc( versionSize );
if ( dataPtr != NULL )
{
if ( GetFileVersionInfo( ntDllPath.c_str(), 0, versionSize, dataPtr ))
{
UINT length;
LPVOID outputPtr;
if ( VerQueryValue( dataPtr, L"\\", &outputPtr, &length ))
{
VS_FIXEDFILEINFO* versionStructPtr = ( VS_FIXEDFILEINFO* )outputPtr;
if ( versionStructPtr->dwSignature == 0xFEEF04BD )
{
versionString = std::to_string( HIWORD( versionStructPtr->dwFileVersionMS ));
versionString += ".";
versionString += std::to_string( LOWORD( versionStructPtr->dwFileVersionMS ));
versionString += ".";
versionString += std::to_string( HIWORD( versionStructPtr->dwFileVersionLS ));
versionString += ".";
versionString += std::to_string( LOWORD( versionStructPtr->dwFileVersionLS ));
}
}
}
free( dataPtr );
}
}
}
return versionString;
}