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

PdhExpandCounterPathA

関数
ワイルドカードパスをパス一覧に展開する(ANSI版)。
DLLpdh.dll文字セットANSI (-A)呼出規約winapi対応OSWindows XP 以降

シグネチャ

// pdh.dll  (ANSI / -A)
#include <windows.h>

DWORD PdhExpandCounterPathA(
    LPCSTR szWildCardPath,
    LPSTR mszExpandedPathList,   // optional
    DWORD* pcchPathListLength
);

パラメーター

名前方向説明
szWildCardPathLPCSTRin展開対象のカウンターパスを含む null 終端文字列です。この関数は、パスで指定されたコンピューターを対象に一致を検索します。パスにコンピューターが指定されていない場合は、ローカルコンピューターを検索します。カウンターパスの最大長は PDH_MAX_COUNTER_PATH です。
mszExpandedPathListLPSTRoutoptionalszWildCardPath のワイルドカード指定に一致する、展開されたカウンターパスの一覧を受け取る、呼び出し側が割り当てたバッファーです。この一覧内の各カウンターパスは null 文字で終端されます。一覧全体は 2 つの NULL 文字で終端されます。pcchPathListLength が 0 の場合は NULL を設定してください。
pcchPathListLengthDWORD*inout

mszExpandedPathList バッファーのサイズ(TCHAR 単位)です。入力時に 0 の場合、この関数は PDH_MORE_DATA を返し、このパラメーターに必要なバッファーサイズを設定します。バッファーが必要なサイズより大きい場合、このパラメーターには実際に使用されたバッファーサイズが設定されます。入力時に指定したサイズが 0 より大きく、かつ必要なサイズより小さい場合、返されたサイズに基づいてバッファーを再割り当てしないでください。

注意 Windows XP では、必要なサイズに 1 を加算する必要があります。

戻り値の型: DWORD

公式ドキュメント

指定されたコンピューター(指定がない場合はローカルコンピューター)を調べ、カウンターパス内のワイルドカード文字列に一致するカウンターおよびカウンターのインスタンスを検索します。(ANSI)

戻り値

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

関数が失敗した場合の戻り値は、 システムエラーコード または PDH エラーコード です。

戻り値 説明
PDH_MORE_DATA
mszExpandedPathList バッファーが小さすぎて、パスの一覧を格納できません。入力時に pcchPathListLength が 0 の場合、この戻り値が想定されます。入力時に指定したサイズが 0 より大きく、かつ必要なサイズより小さい場合、返されたサイズに基づいてバッファーを再割り当てしないでください。
PDH_INVALID_ARGUMENT
パラメーターが無効です。たとえば、一部のリリースでは、入力時に指定したサイズが 0 より大きく、かつ必要なサイズより小さい場合に、このエラーを受け取ることがあります。
PDH_MEMORY_ALLOCATION_FAILURE
この関数をサポートするためのメモリを割り当てることができません。

解説(Remarks)

この関数は 2 回呼び出してください。1 回目は必要なバッファーサイズを取得するため(mszExpandedPathListNULLpcchPathListLength を 0 に設定)、2 回目はデータを取得するためです。

カウンターパスの一般的な形式は次のとおりです。

\computer\object(parent/instance#index)\counter

カウンターパスの parent、instance、index、counter の各構成要素には、有効な名前またはワイルドカード文字のいずれかを指定できます。computer、parent、instance、index の各構成要素は、すべてのカウンターで必要というわけではありません。

使用すべきカウンターパスは、カウンター自体によって決まります。たとえば、LogicalDisk オブジェクトにはインスタンスインデックスがあるため、#index またはワイルドカードを指定する必要があります。したがって、次の形式を使用できます。

\LogicalDisk(/#*)*

これに対して、Process オブジェクトはインスタンスインデックスを必要としません。したがって、次の形式を使用できます。

\Process(*)\ID Process

指定可能な形式の一覧を次に示します。

parent 名にワイルドカード文字が指定された場合、指定された instance および counter フィールドに一致する、指定オブジェクトのすべてのインスタンスが返されます。

instance 名にワイルドカード文字が指定された場合、指定されたインデックスに対応するすべてのインスタンス名がワイルドカード文字に一致すれば、指定オブジェクトおよび親オブジェクトのすべてのインスタンスが返されます。

counter 名にワイルドカード文字が指定された場合、指定オブジェクトのすべてのカウンターが返されます。

カウンターパス文字列の部分一致(たとえば "pro*")はサポートされていません。

次の例は、この関数の使用方法を示しています。


#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <pdh.h>
#include <pdhmsg.h>

#pragma comment(lib, "pdh.lib")

CONST PWSTR WILDCARD_PATH = L"\\Processor(*)\\*";

void wmain(void)
{
    PDH_STATUS Status;
    PWSTR EndOfPaths;
    PWSTR Paths = NULL;
    DWORD BufferSize = 0;

    Status = PdhExpandCounterPath(WILDCARD_PATH, Paths, &BufferSize);

    while (Status == PDH_MORE_DATA) 
    {
        Paths = (PWSTR)malloc(BufferSize * sizeof(WCHAR));
        Status = PdhExpandCounterPath(WILDCARD_PATH, Paths, &BufferSize);
    }

    if (Status != ERROR_SUCCESS) 
    {
        wprintf(L"\nPdhExpandCounterPath failed with status 0x%x", Status);
        goto Cleanup;
    }

    if (Paths == NULL) 
    {
        wprintf(L"\nThe counter path %s cannot be expanded.", WILDCARD_PATH);
        goto Cleanup;
    }

    EndOfPaths = Paths + BufferSize;

    // On Vista and later operating systems, the buffer is terminated with two 
    // null-terminator characters; however, on earlier systems, the buffer is
    // not terminated with two null-terminator characters. This covers both cases.
    for (PWSTR p = Paths; ((p != EndOfPaths) && (*p != L'\0')); p += wcslen(p) + 1) 
    {
        wprintf(L"\n%s", p);
    }

Cleanup:
    if (Paths) 
    {
        free(Paths);
    }
}
メモ

pdh.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして PdhExpandCounterPath を定義します。エンコーディングに依存しないエイリアスの使用を、エンコーディングに依存しないわけではないコードと混在させると、不一致が生じ、コンパイルエラーやランタイムエラーを引き起こす可能性があります。詳細については、関数プロトタイプの規則 を参照してください。

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

各言語での呼び出し定義

// pdh.dll  (ANSI / -A)
#include <windows.h>

DWORD PdhExpandCounterPathA(
    LPCSTR szWildCardPath,
    LPSTR mszExpandedPathList,   // optional
    DWORD* pcchPathListLength
);
[DllImport("pdh.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern uint PdhExpandCounterPathA(
    [MarshalAs(UnmanagedType.LPStr)] string szWildCardPath,   // LPCSTR
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder mszExpandedPathList,   // LPSTR optional, out
    ref uint pcchPathListLength   // DWORD* in/out
);
<DllImport("pdh.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function PdhExpandCounterPathA(
    <MarshalAs(UnmanagedType.LPStr)> szWildCardPath As String,   ' LPCSTR
    <MarshalAs(UnmanagedType.LPStr)> mszExpandedPathList As System.Text.StringBuilder,   ' LPSTR optional, out
    ByRef pcchPathListLength As UInteger   ' DWORD* in/out
) As UInteger
End Function
' szWildCardPath : LPCSTR
' mszExpandedPathList : LPSTR optional, out
' pcchPathListLength : DWORD* in/out
Declare PtrSafe Function PdhExpandCounterPathA Lib "pdh" ( _
    ByVal szWildCardPath As String, _
    ByVal mszExpandedPathList As String, _
    ByRef pcchPathListLength As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

PdhExpandCounterPathA = ctypes.windll.pdh.PdhExpandCounterPathA
PdhExpandCounterPathA.restype = wintypes.DWORD
PdhExpandCounterPathA.argtypes = [
    wintypes.LPCSTR,  # szWildCardPath : LPCSTR
    wintypes.LPSTR,  # mszExpandedPathList : LPSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcchPathListLength : DWORD* in/out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('pdh.dll')
PdhExpandCounterPathA = Fiddle::Function.new(
  lib['PdhExpandCounterPathA'],
  [
    Fiddle::TYPE_VOIDP,  # szWildCardPath : LPCSTR
    Fiddle::TYPE_VOIDP,  # mszExpandedPathList : LPSTR optional, out
    Fiddle::TYPE_VOIDP,  # pcchPathListLength : DWORD* in/out
  ],
  -Fiddle::TYPE_INT)
#[link(name = "pdh")]
extern "system" {
    fn PdhExpandCounterPathA(
        szWildCardPath: *const u8,  // LPCSTR
        mszExpandedPathList: *mut u8,  // LPSTR optional, out
        pcchPathListLength: *mut u32  // DWORD* in/out
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("pdh.dll", CharSet = CharSet.Ansi)]
public static extern uint PdhExpandCounterPathA([MarshalAs(UnmanagedType.LPStr)] string szWildCardPath, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder mszExpandedPathList, ref uint pcchPathListLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'pdh_PdhExpandCounterPathA' -Namespace Win32 -PassThru
# $api::PdhExpandCounterPathA(szWildCardPath, mszExpandedPathList, pcchPathListLength)
#uselib "pdh.dll"
#func global PdhExpandCounterPathA "PdhExpandCounterPathA" sptr, sptr, sptr
; PdhExpandCounterPathA szWildCardPath, varptr(mszExpandedPathList), varptr(pcchPathListLength)   ; 戻り値は stat
; szWildCardPath : LPCSTR -> "sptr"
; mszExpandedPathList : LPSTR optional, out -> "sptr"
; pcchPathListLength : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "pdh.dll"
#cfunc global PdhExpandCounterPathA "PdhExpandCounterPathA" str, var, var
; res = PdhExpandCounterPathA(szWildCardPath, mszExpandedPathList, pcchPathListLength)
; szWildCardPath : LPCSTR -> "str"
; mszExpandedPathList : LPSTR optional, out -> "var"
; pcchPathListLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD PdhExpandCounterPathA(LPCSTR szWildCardPath, LPSTR mszExpandedPathList, DWORD* pcchPathListLength)
#uselib "pdh.dll"
#cfunc global PdhExpandCounterPathA "PdhExpandCounterPathA" str, var, var
; res = PdhExpandCounterPathA(szWildCardPath, mszExpandedPathList, pcchPathListLength)
; szWildCardPath : LPCSTR -> "str"
; mszExpandedPathList : LPSTR optional, out -> "var"
; pcchPathListLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	pdh = windows.NewLazySystemDLL("pdh.dll")
	procPdhExpandCounterPathA = pdh.NewProc("PdhExpandCounterPathA")
)

// szWildCardPath (LPCSTR), mszExpandedPathList (LPSTR optional, out), pcchPathListLength (DWORD* in/out)
r1, _, err := procPdhExpandCounterPathA.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(szWildCardPath))),
	uintptr(mszExpandedPathList),
	uintptr(pcchPathListLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function PdhExpandCounterPathA(
  szWildCardPath: PAnsiChar;   // LPCSTR
  mszExpandedPathList: PAnsiChar;   // LPSTR optional, out
  pcchPathListLength: Pointer   // DWORD* in/out
): DWORD; stdcall;
  external 'pdh.dll' name 'PdhExpandCounterPathA';
result := DllCall("pdh\PdhExpandCounterPathA"
    , "AStr", szWildCardPath   ; LPCSTR
    , "Ptr", mszExpandedPathList   ; LPSTR optional, out
    , "Ptr", pcchPathListLength   ; DWORD* in/out
    , "UInt")   ; return: DWORD
●PdhExpandCounterPathA(szWildCardPath, mszExpandedPathList, pcchPathListLength) = DLL("pdh.dll", "dword PdhExpandCounterPathA(char*, char*, void*)")
# 呼び出し: PdhExpandCounterPathA(szWildCardPath, mszExpandedPathList, pcchPathListLength)
# szWildCardPath : LPCSTR -> "char*"
# mszExpandedPathList : LPSTR optional, out -> "char*"
# pcchPathListLength : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

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

typedef PdhExpandCounterPathANative = Uint32 Function(Pointer<Utf8>, Pointer<Utf8>, Pointer<Uint32>);
typedef PdhExpandCounterPathADart = int Function(Pointer<Utf8>, Pointer<Utf8>, Pointer<Uint32>);
final PdhExpandCounterPathA = DynamicLibrary.open('pdh.dll')
    .lookupFunction<PdhExpandCounterPathANative, PdhExpandCounterPathADart>('PdhExpandCounterPathA');
// szWildCardPath : LPCSTR -> Pointer<Utf8>
// mszExpandedPathList : LPSTR optional, out -> Pointer<Utf8>
// pcchPathListLength : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function PdhExpandCounterPathA(
  szWildCardPath: PAnsiChar;   // LPCSTR
  mszExpandedPathList: PAnsiChar;   // LPSTR optional, out
  pcchPathListLength: Pointer   // DWORD* in/out
): DWORD; stdcall;
  external 'pdh.dll' name 'PdhExpandCounterPathA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "PdhExpandCounterPathA"
  c_PdhExpandCounterPathA :: CString -> CString -> Ptr Word32 -> IO Word32
-- szWildCardPath : LPCSTR -> CString
-- mszExpandedPathList : LPSTR optional, out -> CString
-- pcchPathListLength : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let pdhexpandcounterpatha =
  foreign "PdhExpandCounterPathA"
    (string @-> string @-> (ptr uint32_t) @-> returning uint32_t)
(* szWildCardPath : LPCSTR -> string *)
(* mszExpandedPathList : LPSTR optional, out -> string *)
(* pcchPathListLength : DWORD* in/out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library pdh (t "pdh.dll"))
(cffi:use-foreign-library pdh)

(cffi:defcfun ("PdhExpandCounterPathA" pdh-expand-counter-path-a :convention :stdcall) :uint32
  (sz-wild-card-path :string)   ; LPCSTR
  (msz-expanded-path-list :pointer)   ; LPSTR optional, out
  (pcch-path-list-length :pointer))   ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $PdhExpandCounterPathA = Win32::API::More->new('pdh',
    'DWORD PdhExpandCounterPathA(LPCSTR szWildCardPath, LPSTR mszExpandedPathList, LPVOID pcchPathListLength)');
# my $ret = $PdhExpandCounterPathA->Call($szWildCardPath, $mszExpandedPathList, $pcchPathListLength);
# szWildCardPath : LPCSTR -> LPCSTR
# mszExpandedPathList : LPSTR optional, out -> LPSTR
# pcchPathListLength : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い
公式の関連項目