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

CreateMemoryResourceNotification

関数
メモリリソース状態の通知オブジェクトを作成する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

HANDLE CreateMemoryResourceNotification(
    MEMORY_RESOURCE_NOTIFICATION_TYPE NotificationType
);

パラメーター

名前方向説明
NotificationTypeMEMORY_RESOURCE_NOTIFICATION_TYPEin

オブジェクトがシグナル状態になるメモリ条件。このパラメーターには、MEMORY_RESOURCE_NOTIFICATION_TYPE 列挙体の次のいずれかの値を指定できます。

意味
LowMemoryResourceNotification
0
利用可能な物理メモリが不足しつつあります。
HighMemoryResourceNotification
1
利用可能な物理メモリが十分にあります。

戻り値の型: HANDLE

公式ドキュメント

メモリリソース通知オブジェクトを作成します。

戻り値

関数が成功した場合、戻り値はメモリリソース通知オブジェクトへのハンドルです。

関数が失敗した場合、戻り値は NULL です。拡張エラー情報を取得するには、GetLastError を呼び出します。

解説(Remarks)

アプリケーションは、メモリリソース通知イベントを使用してメモリ使用量を適切に調整できます。利用可能なメモリが少ない場合、アプリケーションはワーキングセットを削減できます。利用可能なメモリが多い場合、アプリケーションはより多くのメモリを割り当てることができます。

呼び出し元プロセスの任意のスレッドは、QueryMemoryResourceNotification 関数または 待機関数 の呼び出しで、メモリリソース通知ハンドルを指定できます。指定したメモリ条件が存在するとき、オブジェクトの状態はシグナル状態になります。これはシステム全体のイベントであるため、オブジェクトがシグナル状態になるとすべてのアプリケーションが通知を受け取ります。なお、LowMemoryResourceNotification オブジェクトも HighMemoryResourceNotification オブジェクトもシグナル状態にならないメモリ可用性の範囲が存在します。この場合、アプリケーションはメモリ使用量を一定に保つよう努める必要があります。

ハンドルを閉じるには、CloseHandle 関数を使用します。プロセスが終了すると、システムは自動的にハンドルを閉じます。メモリリソース通知オブジェクトは、その最後のハンドルが閉じられたときに破棄されます。

この関数を使用するアプリケーションをコンパイルするには、_WIN32_WINNT マクロを 0x0501 以降として定義します。詳細については、Using the Windows Headers を参照してください。

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

各言語での呼び出し定義

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

HANDLE CreateMemoryResourceNotification(
    MEMORY_RESOURCE_NOTIFICATION_TYPE NotificationType
);
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr CreateMemoryResourceNotification(
    int NotificationType   // MEMORY_RESOURCE_NOTIFICATION_TYPE
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CreateMemoryResourceNotification(
    NotificationType As Integer   ' MEMORY_RESOURCE_NOTIFICATION_TYPE
) As IntPtr
End Function
' NotificationType : MEMORY_RESOURCE_NOTIFICATION_TYPE
Declare PtrSafe Function CreateMemoryResourceNotification Lib "kernel32" ( _
    ByVal NotificationType As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CreateMemoryResourceNotification = ctypes.windll.kernel32.CreateMemoryResourceNotification
CreateMemoryResourceNotification.restype = ctypes.c_void_p
CreateMemoryResourceNotification.argtypes = [
    ctypes.c_int,  # NotificationType : MEMORY_RESOURCE_NOTIFICATION_TYPE
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
CreateMemoryResourceNotification = Fiddle::Function.new(
  lib['CreateMemoryResourceNotification'],
  [
    Fiddle::TYPE_INT,  # NotificationType : MEMORY_RESOURCE_NOTIFICATION_TYPE
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "kernel32")]
extern "system" {
    fn CreateMemoryResourceNotification(
        NotificationType: i32  // MEMORY_RESOURCE_NOTIFICATION_TYPE
    ) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern IntPtr CreateMemoryResourceNotification(int NotificationType);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CreateMemoryResourceNotification' -Namespace Win32 -PassThru
# $api::CreateMemoryResourceNotification(NotificationType)
#uselib "KERNEL32.dll"
#func global CreateMemoryResourceNotification "CreateMemoryResourceNotification" sptr
; CreateMemoryResourceNotification NotificationType   ; 戻り値は stat
; NotificationType : MEMORY_RESOURCE_NOTIFICATION_TYPE -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global CreateMemoryResourceNotification "CreateMemoryResourceNotification" int
; res = CreateMemoryResourceNotification(NotificationType)
; NotificationType : MEMORY_RESOURCE_NOTIFICATION_TYPE -> "int"
; HANDLE CreateMemoryResourceNotification(MEMORY_RESOURCE_NOTIFICATION_TYPE NotificationType)
#uselib "KERNEL32.dll"
#cfunc global CreateMemoryResourceNotification "CreateMemoryResourceNotification" int
; res = CreateMemoryResourceNotification(NotificationType)
; NotificationType : MEMORY_RESOURCE_NOTIFICATION_TYPE -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procCreateMemoryResourceNotification = kernel32.NewProc("CreateMemoryResourceNotification")
)

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

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

typedef CreateMemoryResourceNotificationNative = Pointer<Void> Function(Int32);
typedef CreateMemoryResourceNotificationDart = Pointer<Void> Function(int);
final CreateMemoryResourceNotification = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<CreateMemoryResourceNotificationNative, CreateMemoryResourceNotificationDart>('CreateMemoryResourceNotification');
// NotificationType : MEMORY_RESOURCE_NOTIFICATION_TYPE -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CreateMemoryResourceNotification(
  NotificationType: Integer   // MEMORY_RESOURCE_NOTIFICATION_TYPE
): THandle; stdcall;
  external 'KERNEL32.dll' name 'CreateMemoryResourceNotification';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CreateMemoryResourceNotification"
  c_CreateMemoryResourceNotification :: Int32 -> IO (Ptr ())
-- NotificationType : MEMORY_RESOURCE_NOTIFICATION_TYPE -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

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

(cffi:defcfun ("CreateMemoryResourceNotification" create-memory-resource-notification :convention :stdcall) :pointer
  (notification-type :int32))   ; MEMORY_RESOURCE_NOTIFICATION_TYPE
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CreateMemoryResourceNotification = Win32::API::More->new('KERNEL32',
    'HANDLE CreateMemoryResourceNotification(int NotificationType)');
# my $ret = $CreateMemoryResourceNotification->Call($NotificationType);
# NotificationType : MEMORY_RESOURCE_NOTIFICATION_TYPE -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型