アプリケーションで Windows をターゲットにする

Windows 8.1 以降では、GetVersion 関数と GetVersionEx 関数は非推奨となりました。 Windows 10の時点で、VerifyVersionInfo 関数も非推奨になっています。 非推奨の関数を呼び出すこともできますが、アプリケーションが特にWindows 8.1以降を対象としていない場合、関数はWindows 8バージョン (6.2) を返します。

Note

GetVersionGetVersionExVerifyVersionInfo、および バージョン ヘルパー関数 は、デスクトップ アプリ専用です。 ユニバーサル Windows アプリでは、テレメトリと診断ログに AnalyticsInfo.VersionInfo プロパティを使用できます。

アプリがWindows 8.1以降をターゲットにするには、アプリの実行可能ファイルのアプリ (実行可能) マニフェストを含める必要があります。 次に、マニフェストの<互換性>セクションで、アプリがサポートしていることを宣言する Windows バージョンごとに supportedOS> 要素を追加<する必要があります。

次の例は、Windows Vista からWindows 11までのすべてのバージョンの Windows をサポートするアプリのアプリ マニフェスト ファイルを示しています。

<!-- example.exe.manifest -->
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
    <assemblyIdentity
        type="win32"
        name="Contoso.ExampleApplication.ExampleBinary"
        version="1.2.3.4"
        processorArchitecture="x86"
    />
    <description>Contoso Example Application</description>
    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application>
            <!-- Windows 10 and Windows 11 -->
            <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
            <!-- Windows 8.1 -->
            <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
            <!-- Windows 8 -->
            <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
            <!-- Windows 7 -->
            <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
            <!-- Windows Vista -->
            <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/> 
        </application>
    </compatibility>
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
            <requestedPrivileges>
                <!--
                  UAC settings:
                  - app should run at same integrity level as calling process
                  - app does not need to manipulate windows belonging to
                    higher-integrity-level processes
                  -->
                <requestedExecutionLevel
                    level="asInvoker"
                    uiAccess="false"
                />   
            </requestedPrivileges>
        </security>
    </trustInfo>
</assembly>

アプリ マニフェストで Windows 8.1 以降のサポートを宣言しても、以前のオペレーティング システムでアプリを実行しても効果はありません。

上記のアプリ マニフェストには trustInfo> セクションも含まれています<。このセクションでは、ユーザー アカウント制御 (UAC) に関してシステムで処理する方法を指定します。 trustInfo の追加は必須ではありませんが、アプリで特定の UAC 関連の動作が必要ない場合でも、強くお勧めします。 特に、 trustInfo をまったく追加しない場合、アプリの 32 ビット x86 バージョンは UAC ファイルの仮想化の対象となります。これにより、Windows システム フォルダーなどの管理者特権フォルダーへの書き込みが失敗した場合は成功しますが、ユーザー固有の "VirtualStore" フォルダーにリダイレクトされます。