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

PdhExpandCounterPathW

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

シグネチャ

// pdh.dll  (Unicode / -W)
#include <windows.h>

DWORD PdhExpandCounterPathW(
    LPCWSTR szWildCardPath,
    LPWSTR mszExpandedPathList,   // optional
    DWORD* pcchPathListLength
);

パラメーター

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

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

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

戻り値の型: DWORD

公式ドキュメント

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

戻り値

関数が成功すると、ERROR_SUCCESS を返します。

関数が失敗すると、戻り値は システムエラーコード または PDH エラーコード になります。

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

解説(Remarks)

この関数は 2 回呼び出す必要があります。1 回目は必要なバッファーサイズを取得するため(mszExpandedPathListNULL に、pcchPathListLength を 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  (Unicode / -W)
#include <windows.h>

DWORD PdhExpandCounterPathW(
    LPCWSTR szWildCardPath,
    LPWSTR mszExpandedPathList,   // optional
    DWORD* pcchPathListLength
);
[DllImport("pdh.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint PdhExpandCounterPathW(
    [MarshalAs(UnmanagedType.LPWStr)] string szWildCardPath,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder mszExpandedPathList,   // LPWSTR optional, out
    ref uint pcchPathListLength   // DWORD* in/out
);
<DllImport("pdh.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function PdhExpandCounterPathW(
    <MarshalAs(UnmanagedType.LPWStr)> szWildCardPath As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> mszExpandedPathList As System.Text.StringBuilder,   ' LPWSTR optional, out
    ByRef pcchPathListLength As UInteger   ' DWORD* in/out
) As UInteger
End Function
' szWildCardPath : LPCWSTR
' mszExpandedPathList : LPWSTR optional, out
' pcchPathListLength : DWORD* in/out
Declare PtrSafe Function PdhExpandCounterPathW Lib "pdh" ( _
    ByVal szWildCardPath As LongPtr, _
    ByVal mszExpandedPathList As LongPtr, _
    ByRef pcchPathListLength As Long) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

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

lib = Fiddle.dlopen('pdh.dll')
PdhExpandCounterPathW = Fiddle::Function.new(
  lib['PdhExpandCounterPathW'],
  [
    Fiddle::TYPE_VOIDP,  # szWildCardPath : LPCWSTR
    Fiddle::TYPE_VOIDP,  # mszExpandedPathList : LPWSTR optional, out
    Fiddle::TYPE_VOIDP,  # pcchPathListLength : DWORD* in/out
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "pdh")]
extern "system" {
    fn PdhExpandCounterPathW(
        szWildCardPath: *const u16,  // LPCWSTR
        mszExpandedPathList: *mut u16,  // LPWSTR 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.Unicode)]
public static extern uint PdhExpandCounterPathW([MarshalAs(UnmanagedType.LPWStr)] string szWildCardPath, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder mszExpandedPathList, ref uint pcchPathListLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'pdh_PdhExpandCounterPathW' -Namespace Win32 -PassThru
# $api::PdhExpandCounterPathW(szWildCardPath, mszExpandedPathList, pcchPathListLength)
#uselib "pdh.dll"
#func global PdhExpandCounterPathW "PdhExpandCounterPathW" wptr, wptr, wptr
; PdhExpandCounterPathW szWildCardPath, varptr(mszExpandedPathList), varptr(pcchPathListLength)   ; 戻り値は stat
; szWildCardPath : LPCWSTR -> "wptr"
; mszExpandedPathList : LPWSTR optional, out -> "wptr"
; pcchPathListLength : DWORD* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "pdh.dll"
#cfunc global PdhExpandCounterPathW "PdhExpandCounterPathW" wstr, var, var
; res = PdhExpandCounterPathW(szWildCardPath, mszExpandedPathList, pcchPathListLength)
; szWildCardPath : LPCWSTR -> "wstr"
; mszExpandedPathList : LPWSTR optional, out -> "var"
; pcchPathListLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; DWORD PdhExpandCounterPathW(LPCWSTR szWildCardPath, LPWSTR mszExpandedPathList, DWORD* pcchPathListLength)
#uselib "pdh.dll"
#cfunc global PdhExpandCounterPathW "PdhExpandCounterPathW" wstr, var, var
; res = PdhExpandCounterPathW(szWildCardPath, mszExpandedPathList, pcchPathListLength)
; szWildCardPath : LPCWSTR -> "wstr"
; mszExpandedPathList : LPWSTR optional, out -> "var"
; pcchPathListLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	pdh = windows.NewLazySystemDLL("pdh.dll")
	procPdhExpandCounterPathW = pdh.NewProc("PdhExpandCounterPathW")
)

// szWildCardPath (LPCWSTR), mszExpandedPathList (LPWSTR optional, out), pcchPathListLength (DWORD* in/out)
r1, _, err := procPdhExpandCounterPathW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(szWildCardPath))),
	uintptr(mszExpandedPathList),
	uintptr(pcchPathListLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function PdhExpandCounterPathW(
  szWildCardPath: PWideChar;   // LPCWSTR
  mszExpandedPathList: PWideChar;   // LPWSTR optional, out
  pcchPathListLength: Pointer   // DWORD* in/out
): DWORD; stdcall;
  external 'pdh.dll' name 'PdhExpandCounterPathW';
result := DllCall("pdh\PdhExpandCounterPathW"
    , "WStr", szWildCardPath   ; LPCWSTR
    , "Ptr", mszExpandedPathList   ; LPWSTR optional, out
    , "Ptr", pcchPathListLength   ; DWORD* in/out
    , "UInt")   ; return: DWORD
●PdhExpandCounterPathW(szWildCardPath, mszExpandedPathList, pcchPathListLength) = DLL("pdh.dll", "dword PdhExpandCounterPathW(char*, char*, void*)")
# 呼び出し: PdhExpandCounterPathW(szWildCardPath, mszExpandedPathList, pcchPathListLength)
# szWildCardPath : LPCWSTR -> "char*"
# mszExpandedPathList : LPWSTR optional, out -> "char*"
# pcchPathListLength : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "pdh" fn PdhExpandCounterPathW(
    szWildCardPath: [*c]const u16, // LPCWSTR
    mszExpandedPathList: [*c]u16, // LPWSTR optional, out
    pcchPathListLength: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc PdhExpandCounterPathW(
    szWildCardPath: WideCString,  # LPCWSTR
    mszExpandedPathList: ptr uint16,  # LPWSTR optional, out
    pcchPathListLength: ptr uint32  # DWORD* in/out
): uint32 {.importc: "PdhExpandCounterPathW", stdcall, dynlib: "pdh.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "pdh");
extern(Windows)
uint PdhExpandCounterPathW(
    const(wchar)* szWildCardPath,   // LPCWSTR
    wchar* mszExpandedPathList,   // LPWSTR optional, out
    uint* pcchPathListLength   // DWORD* in/out
);
ccall((:PdhExpandCounterPathW, "pdh.dll"), stdcall, UInt32,
      (Cwstring, Ptr{UInt16}, Ptr{UInt32}),
      szWildCardPath, mszExpandedPathList, pcchPathListLength)
# szWildCardPath : LPCWSTR -> Cwstring
# mszExpandedPathList : LPWSTR optional, out -> Ptr{UInt16}
# pcchPathListLength : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
uint32_t PdhExpandCounterPathW(
    const uint16_t* szWildCardPath,
    uint16_t* mszExpandedPathList,
    uint32_t* pcchPathListLength);
]]
local pdh = ffi.load("pdh")
-- pdh.PdhExpandCounterPathW(szWildCardPath, mszExpandedPathList, pcchPathListLength)
-- szWildCardPath : LPCWSTR
-- mszExpandedPathList : LPWSTR optional, out
-- pcchPathListLength : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('pdh.dll');
const PdhExpandCounterPathW = lib.func('__stdcall', 'PdhExpandCounterPathW', 'uint32_t', ['str16', 'uint16_t *', 'uint32_t *']);
// PdhExpandCounterPathW(szWildCardPath, mszExpandedPathList, pcchPathListLength)
// szWildCardPath : LPCWSTR -> 'str16'
// mszExpandedPathList : LPWSTR optional, out -> 'uint16_t *'
// pcchPathListLength : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("pdh.dll", {
  PdhExpandCounterPathW: { parameters: ["buffer", "buffer", "pointer"], result: "u32" },
});
// lib.symbols.PdhExpandCounterPathW(szWildCardPath, mszExpandedPathList, pcchPathListLength)
// szWildCardPath : LPCWSTR -> "buffer"
// mszExpandedPathList : LPWSTR optional, out -> "buffer"
// pcchPathListLength : DWORD* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t PdhExpandCounterPathW(
    const uint16_t* szWildCardPath,
    uint16_t* mszExpandedPathList,
    uint32_t* pcchPathListLength);
C, "pdh.dll");
// $ffi->PdhExpandCounterPathW(szWildCardPath, mszExpandedPathList, pcchPathListLength);
// szWildCardPath : LPCWSTR
// mszExpandedPathList : LPWSTR 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.UNICODE_OPTIONS);
    int PdhExpandCounterPathW(
        WString szWildCardPath,   // LPCWSTR
        char[] mszExpandedPathList,   // LPWSTR optional, out
        IntByReference pcchPathListLength   // DWORD* in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("pdh")]
lib Libpdh
  fun PdhExpandCounterPathW = PdhExpandCounterPathW(
    szWildCardPath : UInt16*,   # LPCWSTR
    mszExpandedPathList : UInt16*,   # LPWSTR 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 PdhExpandCounterPathWNative = Uint32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Uint32>);
typedef PdhExpandCounterPathWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Uint32>);
final PdhExpandCounterPathW = DynamicLibrary.open('pdh.dll')
    .lookupFunction<PdhExpandCounterPathWNative, PdhExpandCounterPathWDart>('PdhExpandCounterPathW');
// szWildCardPath : LPCWSTR -> Pointer<Utf16>
// mszExpandedPathList : LPWSTR optional, out -> Pointer<Utf16>
// pcchPathListLength : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function PdhExpandCounterPathW(
  szWildCardPath: PWideChar;   // LPCWSTR
  mszExpandedPathList: PWideChar;   // LPWSTR optional, out
  pcchPathListLength: Pointer   // DWORD* in/out
): DWORD; stdcall;
  external 'pdh.dll' name 'PdhExpandCounterPathW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let pdhexpandcounterpathw =
  foreign "PdhExpandCounterPathW"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint32_t) @-> returning uint32_t)
(* szWildCardPath : LPCWSTR -> (ptr uint16_t) *)
(* mszExpandedPathList : LPWSTR optional, out -> (ptr uint16_t) *)
(* 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 ("PdhExpandCounterPathW" pdh-expand-counter-path-w :convention :stdcall) :uint32
  (sz-wild-card-path (:string :encoding :utf-16le))   ; LPCWSTR
  (msz-expanded-path-list :pointer)   ; LPWSTR 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 $PdhExpandCounterPathW = Win32::API::More->new('pdh',
    'DWORD PdhExpandCounterPathW(LPCWSTR szWildCardPath, LPWSTR mszExpandedPathList, LPVOID pcchPathListLength)');
# my $ret = $PdhExpandCounterPathW->Call($szWildCardPath, $mszExpandedPathList, $pcchPathListLength);
# szWildCardPath : LPCWSTR -> LPCWSTR
# mszExpandedPathList : LPWSTR optional, out -> LPWSTR
# pcchPathListLength : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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