Win32 API 日本語リファレンス
ホームSystem.SystemInformation › GetVersionExW

GetVersionExW

関数
OSのバージョン情報を取得する(Unicode版)。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

// KERNEL32.dll  (Unicode / -W)
#include <windows.h>

BOOL GetVersionExW(
    OSVERSIONINFOW* lpVersionInformation
);

パラメーター

名前方向説明
lpVersionInformationOSVERSIONINFOW*inout

オペレーティングシステム情報を受け取る OSVERSIONINFOW または OSVERSIONINFOEXW 構造体です。

GetVersionEx 関数を呼び出す前に、この関数に渡すデータ構造を示すために、構造体の dwOSVersionInfoSize メンバーを適切に設定してください。

戻り値の型: BOOL

公式ドキュメント

Windows 8.1 のリリースに伴い、GetVersionEx API が返すオペレーティングシステムのバージョン値の動作が変更されました。GetVersionEx 関数が返す値は、アプリケーションのマニフェストの記述方法に依存するようになりました。(Unicode)

戻り値

関数が成功した場合、戻り値は 0 以外の値です。

関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、 GetLastError を呼び出します。OSVERSIONINFOW または OSVERSIONINFOEXW 構造体の dwOSVersionInfoSize メンバーに無効な値を指定した場合、関数は失敗します。

解説(Remarks)

現在のオペレーティングシステムを特定することは、通常、特定のオペレーティングシステム機能が存在するかどうかを判断する最善の方法ではありません。これは、オペレーティングシステムに再頒布可能な DLL を通じて新機能が追加されている場合があるためです。オペレーティングシステムのプラットフォームやバージョン番号を判断するために GetVersionEx を使用するのではなく、その機能自体の有無をテストしてください。詳細については、 Operating System Version を参照してください。

GetSystemMetrics 関数は、現在のオペレーティングシステムに関する追加情報を提供します。

製品 設定
Windows XP Media Center Edition SM_MEDIACENTER
Windows XP Starter Edition SM_STARTER
Windows XP Tablet PC Edition SM_TABLETPC
Windows Server 2003 R2 SM_SERVERR2

特定のオペレーティングシステムやオペレーティングシステム機能を確認するには、IsOS 関数を使用します。GetProductInfo 関数は製品の種類を取得します。

リモートコンピューター上のオペレーティングシステムの情報を取得するには、NetWkstaGetInfo 関数、Win32_OperatingSystem WMI クラス、または IADsComputer インターフェイスの OperatingSystem プロパティを使用します。

現在のシステムバージョンを必要なバージョンと比較するには、自分で比較を行うために GetVersionEx を使用するのではなく、 VerifyVersionInfo 関数を使用してください。

互換モードが有効な場合、GetVersionEx 関数はオペレーティングシステムが自身を識別する内容を報告しますが、これはインストールされているオペレーティングシステムとは異なる場合があります。たとえば、互換モードが有効な場合、GetVersionExapplication compatibility 用に選択されたオペレーティングシステムを報告します。

GetVersionEx 関数を使用してアプリケーションが特定のバージョンのオペレーティングシステム上で実行されているかどうかを判断する場合は、目的のバージョン番号以上のバージョン番号を確認してください。これにより、それ以降のバージョンのオペレーティングシステムでもテストが成功するようになります。たとえば、アプリケーションが Windows XP 以降を必要とする場合は、次のテストを使用します。

#include <windows.h>
#include <stdio.h>

void main()
{
    OSVERSIONINFO osvi;
    BOOL bIsWindowsXPorLater;

    ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

    GetVersionEx(&osvi);

    bIsWindowsXPorLater = 
       ( (osvi.dwMajorVersion > 5) ||
       ( (osvi.dwMajorVersion == 5) && (osvi.dwMinorVersion >= 1) ));

    if(bIsWindowsXPorLater)
        printf("The system meets the requirements.\n");
    else printf("The system does not meet the requirements.\n");
}

現在のオペレーティングシステムを特定する例については、 Getting the System Version を参照してください。

メモ

sysinfoapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして GetVersionEx を定義しています。エンコーディング中立のエイリアスと、エンコーディング中立でないコードを混在して使用すると、不一致が生じ、コンパイルエラーまたは実行時エラーを引き起こす可能性があります。詳細については、Conventions for Function Prototypes を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// KERNEL32.dll  (Unicode / -W)
#include <windows.h>

BOOL GetVersionExW(
    OSVERSIONINFOW* lpVersionInformation
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool GetVersionExW(
    IntPtr lpVersionInformation   // OSVERSIONINFOW* in/out
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetVersionExW(
    lpVersionInformation As IntPtr   ' OSVERSIONINFOW* in/out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' lpVersionInformation : OSVERSIONINFOW* in/out
Declare PtrSafe Function GetVersionExW Lib "kernel32" ( _
    ByVal lpVersionInformation As LongPtr) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetVersionExW = ctypes.windll.kernel32.GetVersionExW
GetVersionExW.restype = wintypes.BOOL
GetVersionExW.argtypes = [
    ctypes.c_void_p,  # lpVersionInformation : OSVERSIONINFOW* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
GetVersionExW = Fiddle::Function.new(
  lib['GetVersionExW'],
  [
    Fiddle::TYPE_VOIDP,  # lpVersionInformation : OSVERSIONINFOW* in/out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn GetVersionExW(
        lpVersionInformation: *mut OSVERSIONINFOW  // OSVERSIONINFOW* in/out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool GetVersionExW(IntPtr lpVersionInformation);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetVersionExW' -Namespace Win32 -PassThru
# $api::GetVersionExW(lpVersionInformation)
#uselib "KERNEL32.dll"
#func global GetVersionExW "GetVersionExW" wptr
; GetVersionExW varptr(lpVersionInformation)   ; 戻り値は stat
; lpVersionInformation : OSVERSIONINFOW* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global GetVersionExW "GetVersionExW" var
; res = GetVersionExW(lpVersionInformation)
; lpVersionInformation : OSVERSIONINFOW* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL GetVersionExW(OSVERSIONINFOW* lpVersionInformation)
#uselib "KERNEL32.dll"
#cfunc global GetVersionExW "GetVersionExW" var
; res = GetVersionExW(lpVersionInformation)
; lpVersionInformation : OSVERSIONINFOW* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetVersionExW = kernel32.NewProc("GetVersionExW")
)

// lpVersionInformation (OSVERSIONINFOW* in/out)
r1, _, err := procGetVersionExW.Call(
	uintptr(lpVersionInformation),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function GetVersionExW(
  lpVersionInformation: Pointer   // OSVERSIONINFOW* in/out
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'GetVersionExW';
result := DllCall("KERNEL32\GetVersionExW"
    , "Ptr", lpVersionInformation   ; OSVERSIONINFOW* in/out
    , "Int")   ; return: BOOL
●GetVersionExW(lpVersionInformation) = DLL("KERNEL32.dll", "bool GetVersionExW(void*)")
# 呼び出し: GetVersionExW(lpVersionInformation)
# lpVersionInformation : OSVERSIONINFOW* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "kernel32" fn GetVersionExW(
    lpVersionInformation: [*c]OSVERSIONINFOW // OSVERSIONINFOW* in/out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc GetVersionExW(
    lpVersionInformation: ptr OSVERSIONINFOW  # OSVERSIONINFOW* in/out
): int32 {.importc: "GetVersionExW", stdcall, dynlib: "KERNEL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "kernel32");
extern(Windows)
int GetVersionExW(
    OSVERSIONINFOW* lpVersionInformation   // OSVERSIONINFOW* in/out
);
ccall((:GetVersionExW, "KERNEL32.dll"), stdcall, Int32,
      (Ptr{OSVERSIONINFOW},),
      lpVersionInformation)
# lpVersionInformation : OSVERSIONINFOW* in/out -> Ptr{OSVERSIONINFOW}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetVersionExW(
    void* lpVersionInformation);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GetVersionExW(lpVersionInformation)
-- lpVersionInformation : OSVERSIONINFOW* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const GetVersionExW = lib.func('__stdcall', 'GetVersionExW', 'int32_t', ['void *']);
// GetVersionExW(lpVersionInformation)
// lpVersionInformation : OSVERSIONINFOW* in/out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  GetVersionExW: { parameters: ["pointer"], result: "i32" },
});
// lib.symbols.GetVersionExW(lpVersionInformation)
// lpVersionInformation : OSVERSIONINFOW* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetVersionExW(
    void* lpVersionInformation);
C, "KERNEL32.dll");
// $ffi->GetVersionExW(lpVersionInformation);
// lpVersionInformation : OSVERSIONINFOW* in/out
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Kernel32 extends StdCallLibrary {
    Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
    boolean GetVersionExW(
        Pointer lpVersionInformation   // OSVERSIONINFOW* in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("kernel32")]
lib LibKERNEL32
  fun GetVersionExW = GetVersionExW(
    lpVersionInformation : OSVERSIONINFOW*   # OSVERSIONINFOW* in/out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetVersionExWNative = Int32 Function(Pointer<Void>);
typedef GetVersionExWDart = int Function(Pointer<Void>);
final GetVersionExW = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<GetVersionExWNative, GetVersionExWDart>('GetVersionExW');
// lpVersionInformation : OSVERSIONINFOW* in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetVersionExW(
  lpVersionInformation: Pointer   // OSVERSIONINFOW* in/out
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'GetVersionExW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetVersionExW"
  c_GetVersionExW :: Ptr () -> IO CInt
-- lpVersionInformation : OSVERSIONINFOW* in/out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getversionexw =
  foreign "GetVersionExW"
    ((ptr void) @-> returning int32_t)
(* lpVersionInformation : OSVERSIONINFOW* in/out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("GetVersionExW" get-version-ex-w :convention :stdcall) :int32
  (lp-version-information :pointer))   ; OSVERSIONINFOW* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetVersionExW = Win32::API::More->new('KERNEL32',
    'BOOL GetVersionExW(LPVOID lpVersionInformation)');
# my $ret = $GetVersionExW->Call($lpVersionInformation);
# lpVersionInformation : OSVERSIONINFOW* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
類似 API
公式の関連項目
使用する型