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

PdhGetFormattedCounterValue

関数
カウンターの値を指定形式に整形して取得する。
DLLpdh.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

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

DWORD PdhGetFormattedCounterValue(
    PDH_HCOUNTER hCounter,
    PDH_FMT dwFormat,
    DWORD* lpdwType,   // optional
    PDH_FMT_COUNTERVALUE* pValue
);

パラメーター

名前方向説明
hCounterPDH_HCOUNTERin表示可能な値を計算する対象のカウンターのハンドル。このハンドルは PdhAddCounter 関数が返します。
dwFormatPDH_FMTin

書式化される値のデータ型を決定します。次のいずれかの値を指定します。

意味
PDH_FMT_DOUBLE
データを倍精度浮動小数点の実数として返します。
PDH_FMT_LARGE
データを 64 ビット整数として返します。
PDH_FMT_LONG
データを long 整数として返します。

ビットごとの論理和演算子 (|) を使用して、データ型を次のいずれかのスケーリング係数と組み合わせることができます。

意味
PDH_FMT_NOSCALE
カウンターの既定のスケーリング係数を適用しません。
PDH_FMT_NOCAP100
100 を超えるカウンター値 (たとえばマルチプロセッサーコンピューターでプロセッサー負荷を測定するカウンター値など) が 100 にリセットされなくなります。既定の動作では、カウンター値は 100 を上限として制限されます。
PDH_FMT_1000
実際の値を 1,000 倍します。
lpdwTypeDWORD*outoptionalカウンターの種類を受け取ります。カウンターの種類の一覧については、Windows Server 2003 Deployment Kit の「Counter Types」セクションを参照してください。このパラメーターは省略可能です。
pValuePDH_FMT_COUNTERVALUE*outカウンター値を受け取る PDH_FMT_COUNTERVALUE 構造体。

戻り値の型: DWORD

公式ドキュメント

指定したカウンターについて、表示可能な値を計算します。

戻り値

関数が成功した場合は、ERROR_SUCCESS を返します。

関数が失敗した場合、戻り値は システムエラーコード または PDH エラーコード です。考えられる値は次のとおりです。

戻りコード 説明
PDH_INVALID_ARGUMENT
パラメーターが無効であるか、書式が正しくありません。
PDH_INVALID_DATA
指定したカウンターに有効なデータまたは成功を示すステータスコードが含まれていません。
PDH_INVALID_HANDLE
カウンターハンドルが無効です。

解説(Remarks)

呼び出しの処理中にデータが変更されるのを防ぐため、カウンターのデータは PdhGetFormattedCounterValue の呼び出し中ロック (保護) されます。データの読み取り (この関数の呼び出しが成功すること) により、カウンターのデータ変更フラグがクリアされます。

レートカウンターなど一部のカウンターでは、表示可能な値を計算するために 2 つのカウンター値が必要です。この場合、PdhGetFormattedCounterValue を呼び出す前に PdhCollectQueryData を 2 回呼び出す必要があります。詳細については、Collecting Performance Data を参照してください。

指定したカウンターインスタンスが存在しない場合、このメソッドは PDH_INVALID_DATA を返し、PDH_FMT_COUNTERVALUE 構造体の CStatus メンバーを PDH_CSTATUS_NO_INSTANCE に設定します。

Windows Server 2003 より前: インスタンスが見つからない場合、単一の値のみを必要とするカウンターに対して書式化の呼び出しが失敗することがあります。クエリと書式化の呼び出しを再度試みてください。2 回目も書式化の呼び出しが失敗する場合、そのインスタンスは見つかりません。あるいは、PdhEnumObjects 関数を refresh オプションを TRUE に設定して呼び出し、カウンターデータをクエリおよび書式化する前にカウンターインスタンスを更新することもできます。

例については、 Browsing Performance Counters または Reading Performance Data from a Log File を参照してください。

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

各言語での呼び出し定義

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

DWORD PdhGetFormattedCounterValue(
    PDH_HCOUNTER hCounter,
    PDH_FMT dwFormat,
    DWORD* lpdwType,   // optional
    PDH_FMT_COUNTERVALUE* pValue
);
[DllImport("pdh.dll", ExactSpelling = true)]
static extern uint PdhGetFormattedCounterValue(
    IntPtr hCounter,   // PDH_HCOUNTER
    uint dwFormat,   // PDH_FMT
    IntPtr lpdwType,   // DWORD* optional, out
    IntPtr pValue   // PDH_FMT_COUNTERVALUE* out
);
<DllImport("pdh.dll", ExactSpelling:=True)>
Public Shared Function PdhGetFormattedCounterValue(
    hCounter As IntPtr,   ' PDH_HCOUNTER
    dwFormat As UInteger,   ' PDH_FMT
    lpdwType As IntPtr,   ' DWORD* optional, out
    pValue As IntPtr   ' PDH_FMT_COUNTERVALUE* out
) As UInteger
End Function
' hCounter : PDH_HCOUNTER
' dwFormat : PDH_FMT
' lpdwType : DWORD* optional, out
' pValue : PDH_FMT_COUNTERVALUE* out
Declare PtrSafe Function PdhGetFormattedCounterValue Lib "pdh" ( _
    ByVal hCounter As LongPtr, _
    ByVal dwFormat As Long, _
    ByVal lpdwType As LongPtr, _
    ByVal pValue As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

PdhGetFormattedCounterValue = ctypes.windll.pdh.PdhGetFormattedCounterValue
PdhGetFormattedCounterValue.restype = wintypes.DWORD
PdhGetFormattedCounterValue.argtypes = [
    wintypes.HANDLE,  # hCounter : PDH_HCOUNTER
    wintypes.DWORD,  # dwFormat : PDH_FMT
    ctypes.POINTER(wintypes.DWORD),  # lpdwType : DWORD* optional, out
    ctypes.c_void_p,  # pValue : PDH_FMT_COUNTERVALUE* out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('pdh.dll')
PdhGetFormattedCounterValue = Fiddle::Function.new(
  lib['PdhGetFormattedCounterValue'],
  [
    Fiddle::TYPE_VOIDP,  # hCounter : PDH_HCOUNTER
    -Fiddle::TYPE_INT,  # dwFormat : PDH_FMT
    Fiddle::TYPE_VOIDP,  # lpdwType : DWORD* optional, out
    Fiddle::TYPE_VOIDP,  # pValue : PDH_FMT_COUNTERVALUE* out
  ],
  -Fiddle::TYPE_INT)
#[link(name = "pdh")]
extern "system" {
    fn PdhGetFormattedCounterValue(
        hCounter: *mut core::ffi::c_void,  // PDH_HCOUNTER
        dwFormat: u32,  // PDH_FMT
        lpdwType: *mut u32,  // DWORD* optional, out
        pValue: *mut PDH_FMT_COUNTERVALUE  // PDH_FMT_COUNTERVALUE* out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("pdh.dll")]
public static extern uint PdhGetFormattedCounterValue(IntPtr hCounter, uint dwFormat, IntPtr lpdwType, IntPtr pValue);
"@
$api = Add-Type -MemberDefinition $sig -Name 'pdh_PdhGetFormattedCounterValue' -Namespace Win32 -PassThru
# $api::PdhGetFormattedCounterValue(hCounter, dwFormat, lpdwType, pValue)
#uselib "pdh.dll"
#func global PdhGetFormattedCounterValue "PdhGetFormattedCounterValue" sptr, sptr, sptr, sptr
; PdhGetFormattedCounterValue hCounter, dwFormat, varptr(lpdwType), varptr(pValue)   ; 戻り値は stat
; hCounter : PDH_HCOUNTER -> "sptr"
; dwFormat : PDH_FMT -> "sptr"
; lpdwType : DWORD* optional, out -> "sptr"
; pValue : PDH_FMT_COUNTERVALUE* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "pdh.dll"
#cfunc global PdhGetFormattedCounterValue "PdhGetFormattedCounterValue" sptr, int, var, var
; res = PdhGetFormattedCounterValue(hCounter, dwFormat, lpdwType, pValue)
; hCounter : PDH_HCOUNTER -> "sptr"
; dwFormat : PDH_FMT -> "int"
; lpdwType : DWORD* optional, out -> "var"
; pValue : PDH_FMT_COUNTERVALUE* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD PdhGetFormattedCounterValue(PDH_HCOUNTER hCounter, PDH_FMT dwFormat, DWORD* lpdwType, PDH_FMT_COUNTERVALUE* pValue)
#uselib "pdh.dll"
#cfunc global PdhGetFormattedCounterValue "PdhGetFormattedCounterValue" intptr, int, var, var
; res = PdhGetFormattedCounterValue(hCounter, dwFormat, lpdwType, pValue)
; hCounter : PDH_HCOUNTER -> "intptr"
; dwFormat : PDH_FMT -> "int"
; lpdwType : DWORD* optional, out -> "var"
; pValue : PDH_FMT_COUNTERVALUE* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	pdh = windows.NewLazySystemDLL("pdh.dll")
	procPdhGetFormattedCounterValue = pdh.NewProc("PdhGetFormattedCounterValue")
)

// hCounter (PDH_HCOUNTER), dwFormat (PDH_FMT), lpdwType (DWORD* optional, out), pValue (PDH_FMT_COUNTERVALUE* out)
r1, _, err := procPdhGetFormattedCounterValue.Call(
	uintptr(hCounter),
	uintptr(dwFormat),
	uintptr(lpdwType),
	uintptr(pValue),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function PdhGetFormattedCounterValue(
  hCounter: THandle;   // PDH_HCOUNTER
  dwFormat: DWORD;   // PDH_FMT
  lpdwType: Pointer;   // DWORD* optional, out
  pValue: Pointer   // PDH_FMT_COUNTERVALUE* out
): DWORD; stdcall;
  external 'pdh.dll' name 'PdhGetFormattedCounterValue';
result := DllCall("pdh\PdhGetFormattedCounterValue"
    , "Ptr", hCounter   ; PDH_HCOUNTER
    , "UInt", dwFormat   ; PDH_FMT
    , "Ptr", lpdwType   ; DWORD* optional, out
    , "Ptr", pValue   ; PDH_FMT_COUNTERVALUE* out
    , "UInt")   ; return: DWORD
●PdhGetFormattedCounterValue(hCounter, dwFormat, lpdwType, pValue) = DLL("pdh.dll", "dword PdhGetFormattedCounterValue(void*, dword, void*, void*)")
# 呼び出し: PdhGetFormattedCounterValue(hCounter, dwFormat, lpdwType, pValue)
# hCounter : PDH_HCOUNTER -> "void*"
# dwFormat : PDH_FMT -> "dword"
# lpdwType : DWORD* optional, out -> "void*"
# pValue : PDH_FMT_COUNTERVALUE* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

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

typedef PdhGetFormattedCounterValueNative = Uint32 Function(Pointer<Void>, Uint32, Pointer<Uint32>, Pointer<Void>);
typedef PdhGetFormattedCounterValueDart = int Function(Pointer<Void>, int, Pointer<Uint32>, Pointer<Void>);
final PdhGetFormattedCounterValue = DynamicLibrary.open('pdh.dll')
    .lookupFunction<PdhGetFormattedCounterValueNative, PdhGetFormattedCounterValueDart>('PdhGetFormattedCounterValue');
// hCounter : PDH_HCOUNTER -> Pointer<Void>
// dwFormat : PDH_FMT -> Uint32
// lpdwType : DWORD* optional, out -> Pointer<Uint32>
// pValue : PDH_FMT_COUNTERVALUE* out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function PdhGetFormattedCounterValue(
  hCounter: THandle;   // PDH_HCOUNTER
  dwFormat: DWORD;   // PDH_FMT
  lpdwType: Pointer;   // DWORD* optional, out
  pValue: Pointer   // PDH_FMT_COUNTERVALUE* out
): DWORD; stdcall;
  external 'pdh.dll' name 'PdhGetFormattedCounterValue';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "PdhGetFormattedCounterValue"
  c_PdhGetFormattedCounterValue :: Ptr () -> Word32 -> Ptr Word32 -> Ptr () -> IO Word32
-- hCounter : PDH_HCOUNTER -> Ptr ()
-- dwFormat : PDH_FMT -> Word32
-- lpdwType : DWORD* optional, out -> Ptr Word32
-- pValue : PDH_FMT_COUNTERVALUE* out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let pdhgetformattedcountervalue =
  foreign "PdhGetFormattedCounterValue"
    ((ptr void) @-> uint32_t @-> (ptr uint32_t) @-> (ptr void) @-> returning uint32_t)
(* hCounter : PDH_HCOUNTER -> (ptr void) *)
(* dwFormat : PDH_FMT -> uint32_t *)
(* lpdwType : DWORD* optional, out -> (ptr uint32_t) *)
(* pValue : PDH_FMT_COUNTERVALUE* out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library pdh (t "pdh.dll"))
(cffi:use-foreign-library pdh)

(cffi:defcfun ("PdhGetFormattedCounterValue" pdh-get-formatted-counter-value :convention :stdcall) :uint32
  (h-counter :pointer)   ; PDH_HCOUNTER
  (dw-format :uint32)   ; PDH_FMT
  (lpdw-type :pointer)   ; DWORD* optional, out
  (p-value :pointer))   ; PDH_FMT_COUNTERVALUE* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $PdhGetFormattedCounterValue = Win32::API::More->new('pdh',
    'DWORD PdhGetFormattedCounterValue(HANDLE hCounter, DWORD dwFormat, LPVOID lpdwType, LPVOID pValue)');
# my $ret = $PdhGetFormattedCounterValue->Call($hCounter, $dwFormat, $lpdwType, $pValue);
# hCounter : PDH_HCOUNTER -> HANDLE
# dwFormat : PDH_FMT -> DWORD
# lpdwType : DWORD* optional, out -> LPVOID
# pValue : PDH_FMT_COUNTERVALUE* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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