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

GetPerformanceInfo

関数
システム全体のパフォーマンス情報を取得する。
DLLPSAPI.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL GetPerformanceInfo(
    PERFORMANCE_INFORMATION* pPerformanceInformation,
    DWORD cb
);

パラメーター

名前方向説明
pPerformanceInformationPERFORMANCE_INFORMATION*inoutパフォーマンス情報を受け取る PERFORMANCE_INFORMATION 構造体へのポインターです。
cbDWORDin
  <a href="/windows/desktop/api/psapi/ns-psapi-performance_information">PERFORMANCE_INFORMATION</a> 構造体のサイズ(バイト単位)です。

戻り値の型: BOOL

公式ドキュメント

PERFORMANCE_INFORMATION 構造体に格納されたパフォーマンス値を取得します。

戻り値

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

解説(Remarks)

Windows 7 および Windows Server 2008 R2 以降、Psapi.h は PSAPI 関数のバージョン番号を定めています。PSAPI のバージョン番号は、関数を呼び出すために使用する名前と、プログラムが読み込む必要のあるライブラリに影響します。

PSAPI_VERSION が 2 以上の場合、この関数は Psapi.h において K32GetPerformanceInfo として定義され、Kernel32.lib および Kernel32.dll でエクスポートされます。PSAPI_VERSION が 1 の場合、この関数は Psapi.h において GetPerformanceInfo として定義され、K32GetPerformanceInfo を呼び出すラッパーとして Psapi.lib および Psapi.dll でエクスポートされます。

Windows 7 以降のバージョンに加えて、それ以前のバージョンの Windows でも実行する必要があるプログラムは、この関数を常に GetPerformanceInfo として呼び出してください。シンボルが正しく解決されるようにするには、TARGETLIBS マクロに Psapi.lib を追加し、–DPSAPI_VERSION=1 を指定してプログラムをコンパイルします。実行時の動的リンクを使用するには、Psapi.dll を読み込みます。

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

各言語での呼び出し定義

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

BOOL GetPerformanceInfo(
    PERFORMANCE_INFORMATION* pPerformanceInformation,
    DWORD cb
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("PSAPI.dll", SetLastError = true, ExactSpelling = true)]
static extern bool GetPerformanceInfo(
    IntPtr pPerformanceInformation,   // PERFORMANCE_INFORMATION* in/out
    uint cb   // DWORD
);
<DllImport("PSAPI.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetPerformanceInfo(
    pPerformanceInformation As IntPtr,   ' PERFORMANCE_INFORMATION* in/out
    cb As UInteger   ' DWORD
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' pPerformanceInformation : PERFORMANCE_INFORMATION* in/out
' cb : DWORD
Declare PtrSafe Function GetPerformanceInfo Lib "psapi" ( _
    ByVal pPerformanceInformation As LongPtr, _
    ByVal cb As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetPerformanceInfo = ctypes.windll.psapi.GetPerformanceInfo
GetPerformanceInfo.restype = wintypes.BOOL
GetPerformanceInfo.argtypes = [
    ctypes.c_void_p,  # pPerformanceInformation : PERFORMANCE_INFORMATION* in/out
    wintypes.DWORD,  # cb : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	psapi = windows.NewLazySystemDLL("PSAPI.dll")
	procGetPerformanceInfo = psapi.NewProc("GetPerformanceInfo")
)

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

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

typedef GetPerformanceInfoNative = Int32 Function(Pointer<Void>, Uint32);
typedef GetPerformanceInfoDart = int Function(Pointer<Void>, int);
final GetPerformanceInfo = DynamicLibrary.open('PSAPI.dll')
    .lookupFunction<GetPerformanceInfoNative, GetPerformanceInfoDart>('GetPerformanceInfo');
// pPerformanceInformation : PERFORMANCE_INFORMATION* in/out -> Pointer<Void>
// cb : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetPerformanceInfo(
  pPerformanceInformation: Pointer;   // PERFORMANCE_INFORMATION* in/out
  cb: DWORD   // DWORD
): BOOL; stdcall;
  external 'PSAPI.dll' name 'GetPerformanceInfo';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let getperformanceinfo =
  foreign "GetPerformanceInfo"
    ((ptr void) @-> uint32_t @-> returning int32_t)
(* pPerformanceInformation : PERFORMANCE_INFORMATION* in/out -> (ptr void) *)
(* cb : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library psapi (t "PSAPI.dll"))
(cffi:use-foreign-library psapi)

(cffi:defcfun ("GetPerformanceInfo" get-performance-info :convention :stdcall) :int32
  (p-performance-information :pointer)   ; PERFORMANCE_INFORMATION* in/out
  (cb :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetPerformanceInfo = Win32::API::More->new('PSAPI',
    'BOOL GetPerformanceInfo(LPVOID pPerformanceInformation, DWORD cb)');
# my $ret = $GetPerformanceInfo->Call($pPerformanceInformation, $cb);
# pPerformanceInformation : PERFORMANCE_INFORMATION* in/out -> LPVOID
# cb : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

使用する型