ホーム › System.SystemInformation › GlobalMemoryStatusEx
GlobalMemoryStatusEx
関数物理メモリや仮想メモリの使用状況を取得する。
シグネチャ
// KERNEL32.dll
#include <windows.h>
BOOL GlobalMemoryStatusEx(
MEMORYSTATUSEX* lpBuffer
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpBuffer | MEMORYSTATUSEX* | inout | 現在のメモリ可用性に関する情報を受け取る MEMORYSTATUSEX 構造体へのポインターです。 |
戻り値の型: BOOL
公式ドキュメント
システムによる物理メモリおよび仮想メモリの現在の使用状況に関する情報を取得します。(GlobalMemoryStatusEx)
戻り値
関数が成功すると、戻り値は 0 以外になります。
関数が失敗すると、戻り値は 0 になります。拡張エラー情報を取得するには、 GetLastError を呼び出します。
解説(Remarks)
GlobalMemoryStatusEx 関数を使用すると、他のアプリケーションに重大な影響を与えることなく、アプリケーションがどれだけのメモリを割り当てられるかを判断できます。
GlobalMemoryStatusEx 関数が返す情報は揮発性です。この関数を続けて 2 回呼び出しても、同じ情報が返される保証はありません。
lpBuffer が指す MEMORYSTATUSEX 構造体の ullAvailPhys メンバーには、すべての NUMA ノードのメモリが含まれます。
例
次のコードは、 GlobalMemoryStatusEx 関数の簡単な使用例を示しています。
// Sample output:
// There is 51 percent of memory in use.
// There are 2029968 total KB of physical memory.
// There are 987388 free KB of physical memory.
// There are 3884620 total KB of paging file.
// There are 2799776 free KB of paging file.
// There are 2097024 total KB of virtual memory.
// There are 2084876 free KB of virtual memory.
// There are 0 free KB of extended memory.
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
// Use to convert bytes to KB
#define DIV 1024
// Specify the width of the field in which to print the numbers.
// The asterisk in the format specifier "%*I64d" takes an integer
// argument and uses it to pad and right justify the number.
#define WIDTH 7
void _tmain()
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
_tprintf (TEXT("There is %*ld percent of memory in use.\n"),
WIDTH, statex.dwMemoryLoad);
_tprintf (TEXT("There are %*I64d total KB of physical memory.\n"),
WIDTH, statex.ullTotalPhys/DIV);
_tprintf (TEXT("There are %*I64d free KB of physical memory.\n"),
WIDTH, statex.ullAvailPhys/DIV);
_tprintf (TEXT("There are %*I64d total KB of paging file.\n"),
WIDTH, statex.ullTotalPageFile/DIV);
_tprintf (TEXT("There are %*I64d free KB of paging file.\n"),
WIDTH, statex.ullAvailPageFile/DIV);
_tprintf (TEXT("There are %*I64d total KB of virtual memory.\n"),
WIDTH, statex.ullTotalVirtual/DIV);
_tprintf (TEXT("There are %*I64d free KB of virtual memory.\n"),
WIDTH, statex.ullAvailVirtual/DIV);
// Show the amount of extended memory available.
_tprintf (TEXT("There are %*I64d free KB of extended memory.\n"),
WIDTH, statex.ullAvailExtendedVirtual/DIV);
}
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll
#include <windows.h>
BOOL GlobalMemoryStatusEx(
MEMORYSTATUSEX* lpBuffer
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool GlobalMemoryStatusEx(
IntPtr lpBuffer // MEMORYSTATUSEX* in/out
);<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GlobalMemoryStatusEx(
lpBuffer As IntPtr ' MEMORYSTATUSEX* in/out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' lpBuffer : MEMORYSTATUSEX* in/out
Declare PtrSafe Function GlobalMemoryStatusEx Lib "kernel32" ( _
ByVal lpBuffer As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
GlobalMemoryStatusEx = ctypes.windll.kernel32.GlobalMemoryStatusEx
GlobalMemoryStatusEx.restype = wintypes.BOOL
GlobalMemoryStatusEx.argtypes = [
ctypes.c_void_p, # lpBuffer : MEMORYSTATUSEX* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
GlobalMemoryStatusEx = Fiddle::Function.new(
lib['GlobalMemoryStatusEx'],
[
Fiddle::TYPE_VOIDP, # lpBuffer : MEMORYSTATUSEX* in/out
],
Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn GlobalMemoryStatusEx(
lpBuffer: *mut MEMORYSTATUSEX // MEMORYSTATUSEX* in/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 GlobalMemoryStatusEx(IntPtr lpBuffer);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GlobalMemoryStatusEx' -Namespace Win32 -PassThru
# $api::GlobalMemoryStatusEx(lpBuffer)#uselib "KERNEL32.dll"
#func global GlobalMemoryStatusEx "GlobalMemoryStatusEx" sptr
; GlobalMemoryStatusEx varptr(lpBuffer) ; 戻り値は stat
; lpBuffer : MEMORYSTATUSEX* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "KERNEL32.dll" #cfunc global GlobalMemoryStatusEx "GlobalMemoryStatusEx" var ; res = GlobalMemoryStatusEx(lpBuffer) ; lpBuffer : MEMORYSTATUSEX* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global GlobalMemoryStatusEx "GlobalMemoryStatusEx" sptr ; res = GlobalMemoryStatusEx(varptr(lpBuffer)) ; lpBuffer : MEMORYSTATUSEX* in/out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL GlobalMemoryStatusEx(MEMORYSTATUSEX* lpBuffer) #uselib "KERNEL32.dll" #cfunc global GlobalMemoryStatusEx "GlobalMemoryStatusEx" var ; res = GlobalMemoryStatusEx(lpBuffer) ; lpBuffer : MEMORYSTATUSEX* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL GlobalMemoryStatusEx(MEMORYSTATUSEX* lpBuffer) #uselib "KERNEL32.dll" #cfunc global GlobalMemoryStatusEx "GlobalMemoryStatusEx" intptr ; res = GlobalMemoryStatusEx(varptr(lpBuffer)) ; lpBuffer : MEMORYSTATUSEX* in/out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procGlobalMemoryStatusEx = kernel32.NewProc("GlobalMemoryStatusEx")
)
// lpBuffer (MEMORYSTATUSEX* in/out)
r1, _, err := procGlobalMemoryStatusEx.Call(
uintptr(lpBuffer),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction GlobalMemoryStatusEx(
lpBuffer: Pointer // MEMORYSTATUSEX* in/out
): BOOL; stdcall;
external 'KERNEL32.dll' name 'GlobalMemoryStatusEx';result := DllCall("KERNEL32\GlobalMemoryStatusEx"
, "Ptr", lpBuffer ; MEMORYSTATUSEX* in/out
, "Int") ; return: BOOL●GlobalMemoryStatusEx(lpBuffer) = DLL("KERNEL32.dll", "bool GlobalMemoryStatusEx(void*)")
# 呼び出し: GlobalMemoryStatusEx(lpBuffer)
# lpBuffer : MEMORYSTATUSEX* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "kernel32" fn GlobalMemoryStatusEx(
lpBuffer: [*c]MEMORYSTATUSEX // MEMORYSTATUSEX* in/out
) callconv(std.os.windows.WINAPI) i32;proc GlobalMemoryStatusEx(
lpBuffer: ptr MEMORYSTATUSEX # MEMORYSTATUSEX* in/out
): int32 {.importc: "GlobalMemoryStatusEx", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
int GlobalMemoryStatusEx(
MEMORYSTATUSEX* lpBuffer // MEMORYSTATUSEX* in/out
);ccall((:GlobalMemoryStatusEx, "KERNEL32.dll"), stdcall, Int32,
(Ptr{MEMORYSTATUSEX},),
lpBuffer)
# lpBuffer : MEMORYSTATUSEX* in/out -> Ptr{MEMORYSTATUSEX}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t GlobalMemoryStatusEx(
void* lpBuffer);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GlobalMemoryStatusEx(lpBuffer)
-- lpBuffer : MEMORYSTATUSEX* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const GlobalMemoryStatusEx = lib.func('__stdcall', 'GlobalMemoryStatusEx', 'int32_t', ['void *']);
// GlobalMemoryStatusEx(lpBuffer)
// lpBuffer : MEMORYSTATUSEX* in/out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
GlobalMemoryStatusEx: { parameters: ["pointer"], result: "i32" },
});
// lib.symbols.GlobalMemoryStatusEx(lpBuffer)
// lpBuffer : MEMORYSTATUSEX* in/out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t GlobalMemoryStatusEx(
void* lpBuffer);
C, "KERNEL32.dll");
// $ffi->GlobalMemoryStatusEx(lpBuffer);
// lpBuffer : MEMORYSTATUSEX* 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);
boolean GlobalMemoryStatusEx(
Pointer lpBuffer // MEMORYSTATUSEX* in/out
);
}@[Link("kernel32")]
lib LibKERNEL32
fun GlobalMemoryStatusEx = GlobalMemoryStatusEx(
lpBuffer : MEMORYSTATUSEX* # MEMORYSTATUSEX* 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 GlobalMemoryStatusExNative = Int32 Function(Pointer<Void>);
typedef GlobalMemoryStatusExDart = int Function(Pointer<Void>);
final GlobalMemoryStatusEx = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<GlobalMemoryStatusExNative, GlobalMemoryStatusExDart>('GlobalMemoryStatusEx');
// lpBuffer : MEMORYSTATUSEX* in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function GlobalMemoryStatusEx(
lpBuffer: Pointer // MEMORYSTATUSEX* in/out
): BOOL; stdcall;
external 'KERNEL32.dll' name 'GlobalMemoryStatusEx';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "GlobalMemoryStatusEx"
c_GlobalMemoryStatusEx :: Ptr () -> IO CInt
-- lpBuffer : MEMORYSTATUSEX* in/out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let globalmemorystatusex =
foreign "GlobalMemoryStatusEx"
((ptr void) @-> returning int32_t)
(* lpBuffer : MEMORYSTATUSEX* 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 ("GlobalMemoryStatusEx" global-memory-status-ex :convention :stdcall) :int32
(lp-buffer :pointer)) ; MEMORYSTATUSEX* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $GlobalMemoryStatusEx = Win32::API::More->new('KERNEL32',
'BOOL GlobalMemoryStatusEx(LPVOID lpBuffer)');
# my $ret = $GlobalMemoryStatusEx->Call($lpBuffer);
# lpBuffer : MEMORYSTATUSEX* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
類似 API
- f GlobalMemoryStatus — システムの物理および仮想メモリ使用状況を取得する。
使用する型