c ++ - Korrekt måde at få Windows-version på

Indlæg af Hanne Mølgaard Plasc

Problem




    Dette spørgsmål har allerede et svar her:

    

            

  •             Registrering af Windows 10-version
                    
                        3 svar
                    

            

  •         

  •             C ++ Sådan registreres Windows 10
                    
                        4 svar
                    

            

  •     

Bedste reference


Hvis du har brug for en ægte Windows-version med build-nummer, der ikke er virtualiseret i forbindelse med din app.manifest -fil, kan du få en Windows-kernelversion fra ntoskrnl.exe: /questions/36543301/detecting-windows-10-version /questions/32115255/c-how-to-detect-windows-10


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;
  }