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

GetPhysicallyInstalledSystemMemory

関数
物理的に搭載された総メモリ量をキロバイト単位で取得する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

// KERNEL32.dll
#include <windows.h>

BOOL GetPhysicallyInstalledSystemMemory(
    ULONGLONG* TotalMemoryInKilobytes
);

パラメーター

名前方向説明
TotalMemoryInKilobytesULONGLONG*out物理的に搭載されている RAM の容量(キロバイト単位)を受け取る変数へのポインター。

戻り値の型: BOOL

公式ドキュメント

コンピューターに物理的に搭載されている RAM の容量を取得します。

戻り値

関数が成功した場合は TRUE を返し、TotalMemoryInKilobytes パラメーターに 0 以外の値を設定します。

関数が失敗した場合は FALSE を返し、TotalMemoryInKilobytes パラメーターは変更しません。拡張エラー情報を取得するには、GetLastError 関数を使用します。代表的なエラーを次の表に示します。

戻り値 説明
ERROR_INVALID_PARAMETER
TotalMemoryInKilobytes パラメーターが NULL です。
ERROR_INVALID_DATA
System Management BIOS (SMBIOS) のデータが不正な形式です。

解説(Remarks)

GetPhysicallyInstalledSystemMemory 関数は、物理的に搭載されている RAM の容量をコンピューターの SMBIOS ファームウェアテーブルから取得します。これは GlobalMemoryStatusEx 関数が報告する容量とは異なる場合があります。GlobalMemoryStatusEx 関数は、オペレーティングシステムが使用可能な物理メモリの容量を MEMORYSTATUSEX 構造体の ullTotalPhys メンバーに設定します。BIOS や一部のドライバーがメモリマップドデバイス用の I/O 領域としてメモリを予約し、そのメモリをオペレーティングシステムやアプリケーションから利用できなくする場合があるため、オペレーティングシステムが利用可能なメモリの容量は、コンピューターに物理的に搭載されているメモリの容量よりも少なくなることがあります。

GetPhysicallyInstalledSystemMemory 関数が取得する物理メモリの容量は、GlobalMemoryStatusEx 関数が報告する容量以上でなければなりません。これより少ない場合、SMBIOS データは不正な形式であり、関数は ERROR_INVALID_DATA で失敗します。不正な形式の SMBIOS データは、ユーザーのコンピューターに問題があることを示している可能性があります。

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

各言語での呼び出し定義

// KERNEL32.dll
#include <windows.h>

BOOL GetPhysicallyInstalledSystemMemory(
    ULONGLONG* TotalMemoryInKilobytes
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool GetPhysicallyInstalledSystemMemory(
    out ulong TotalMemoryInKilobytes   // ULONGLONG* out
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetPhysicallyInstalledSystemMemory(
    <Out> ByRef TotalMemoryInKilobytes As ULong   ' ULONGLONG* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' TotalMemoryInKilobytes : ULONGLONG* out
Declare PtrSafe Function GetPhysicallyInstalledSystemMemory Lib "kernel32" ( _
    ByRef TotalMemoryInKilobytes As LongLong) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetPhysicallyInstalledSystemMemory = ctypes.windll.kernel32.GetPhysicallyInstalledSystemMemory
GetPhysicallyInstalledSystemMemory.restype = wintypes.BOOL
GetPhysicallyInstalledSystemMemory.argtypes = [
    ctypes.POINTER(ctypes.c_ulonglong),  # TotalMemoryInKilobytes : ULONGLONG* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetPhysicallyInstalledSystemMemory = kernel32.NewProc("GetPhysicallyInstalledSystemMemory")
)

// TotalMemoryInKilobytes (ULONGLONG* out)
r1, _, err := procGetPhysicallyInstalledSystemMemory.Call(
	uintptr(TotalMemoryInKilobytes),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function GetPhysicallyInstalledSystemMemory(
  TotalMemoryInKilobytes: Pointer   // ULONGLONG* out
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'GetPhysicallyInstalledSystemMemory';
result := DllCall("KERNEL32\GetPhysicallyInstalledSystemMemory"
    , "Ptr", TotalMemoryInKilobytes   ; ULONGLONG* out
    , "Int")   ; return: BOOL
●GetPhysicallyInstalledSystemMemory(TotalMemoryInKilobytes) = DLL("KERNEL32.dll", "bool GetPhysicallyInstalledSystemMemory(void*)")
# 呼び出し: GetPhysicallyInstalledSystemMemory(TotalMemoryInKilobytes)
# TotalMemoryInKilobytes : ULONGLONG* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn GetPhysicallyInstalledSystemMemory(
    TotalMemoryInKilobytes: [*c]u64 // ULONGLONG* out
) callconv(std.os.windows.WINAPI) i32;
proc GetPhysicallyInstalledSystemMemory(
    TotalMemoryInKilobytes: ptr uint64  # ULONGLONG* out
): int32 {.importc: "GetPhysicallyInstalledSystemMemory", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
int GetPhysicallyInstalledSystemMemory(
    ulong* TotalMemoryInKilobytes   // ULONGLONG* out
);
ccall((:GetPhysicallyInstalledSystemMemory, "KERNEL32.dll"), stdcall, Int32,
      (Ptr{UInt64},),
      TotalMemoryInKilobytes)
# TotalMemoryInKilobytes : ULONGLONG* out -> Ptr{UInt64}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetPhysicallyInstalledSystemMemory(
    uint64_t* TotalMemoryInKilobytes);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GetPhysicallyInstalledSystemMemory(TotalMemoryInKilobytes)
-- TotalMemoryInKilobytes : ULONGLONG* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const GetPhysicallyInstalledSystemMemory = lib.func('__stdcall', 'GetPhysicallyInstalledSystemMemory', 'int32_t', ['uint64_t *']);
// GetPhysicallyInstalledSystemMemory(TotalMemoryInKilobytes)
// TotalMemoryInKilobytes : ULONGLONG* out -> 'uint64_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  GetPhysicallyInstalledSystemMemory: { parameters: ["pointer"], result: "i32" },
});
// lib.symbols.GetPhysicallyInstalledSystemMemory(TotalMemoryInKilobytes)
// TotalMemoryInKilobytes : ULONGLONG* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetPhysicallyInstalledSystemMemory(
    uint64_t* TotalMemoryInKilobytes);
C, "KERNEL32.dll");
// $ffi->GetPhysicallyInstalledSystemMemory(TotalMemoryInKilobytes);
// TotalMemoryInKilobytes : ULONGLONG* 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);
    boolean GetPhysicallyInstalledSystemMemory(
        LongByReference TotalMemoryInKilobytes   // ULONGLONG* out
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun GetPhysicallyInstalledSystemMemory = GetPhysicallyInstalledSystemMemory(
    TotalMemoryInKilobytes : UInt64*   # ULONGLONG* out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

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

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

let getphysicallyinstalledsystemmemory =
  foreign "GetPhysicallyInstalledSystemMemory"
    ((ptr uint64_t) @-> returning int32_t)
(* TotalMemoryInKilobytes : ULONGLONG* out -> (ptr uint64_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("GetPhysicallyInstalledSystemMemory" get-physically-installed-system-memory :convention :stdcall) :int32
  (total-memory-in-kilobytes :pointer))   ; ULONGLONG* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetPhysicallyInstalledSystemMemory = Win32::API::More->new('KERNEL32',
    'BOOL GetPhysicallyInstalledSystemMemory(LPVOID TotalMemoryInKilobytes)');
# my $ret = $GetPhysicallyInstalledSystemMemory->Call($TotalMemoryInKilobytes);
# TotalMemoryInKilobytes : ULONGLONG* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目