ホーム › System.Performance › PdhLookupPerfIndexByNameW
PdhLookupPerfIndexByNameW
関数パフォーマンス名から対応するインデックス番号を取得する。
シグネチャ
// pdh.dll (Unicode / -W)
#include <windows.h>
DWORD PdhLookupPerfIndexByNameW(
LPCWSTR szMachineName, // optional
LPCWSTR szNameBuffer,
DWORD* pdwIndex
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| szMachineName | LPCWSTR | inoptional | 指定されたカウンターが存在するコンピューターの名前を指定する Null 終端文字列です。コンピューター名は DNS 名または IP アドレスで指定できます。NULL の場合、関数はローカルコンピューターを使用します。 |
| szNameBuffer | LPCWSTR | in | カウンター名を含む Null 終端文字列です。 |
| pdwIndex | DWORD* | out | カウンターのインデックスです。 |
戻り値の型: DWORD
公式ドキュメント
指定されたカウンター名に対応するカウンターのインデックスを返します。(Unicode)
戻り値
関数が成功した場合は、ERROR_SUCCESS を返します。
関数が失敗した場合、戻り値は システムエラーコードまたは PDH エラーコードです。次の値が考えられます。
| 戻り値 | 説明 |
|---|---|
| パラメーターが無効であるか、形式が正しくありません。 |
解説(Remarks)
メモ
pdh.h ヘッダーは PdhLookupPerfIndexByName をエイリアスとして定義しており、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択します。エンコーディング中立のエイリアスと、エンコーディング中立でないコードを混在させて使用すると、不一致が生じ、コンパイルエラーや実行時エラーの原因となる場合があります。詳細については、関数プロトタイプの規約を参照してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// pdh.dll (Unicode / -W)
#include <windows.h>
DWORD PdhLookupPerfIndexByNameW(
LPCWSTR szMachineName, // optional
LPCWSTR szNameBuffer,
DWORD* pdwIndex
);[DllImport("pdh.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint PdhLookupPerfIndexByNameW(
[MarshalAs(UnmanagedType.LPWStr)] string szMachineName, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] string szNameBuffer, // LPCWSTR
out uint pdwIndex // DWORD* out
);<DllImport("pdh.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function PdhLookupPerfIndexByNameW(
<MarshalAs(UnmanagedType.LPWStr)> szMachineName As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> szNameBuffer As String, ' LPCWSTR
<Out> ByRef pdwIndex As UInteger ' DWORD* out
) As UInteger
End Function' szMachineName : LPCWSTR optional
' szNameBuffer : LPCWSTR
' pdwIndex : DWORD* out
Declare PtrSafe Function PdhLookupPerfIndexByNameW Lib "pdh" ( _
ByVal szMachineName As LongPtr, _
ByVal szNameBuffer As LongPtr, _
ByRef pdwIndex 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
PdhLookupPerfIndexByNameW = ctypes.windll.pdh.PdhLookupPerfIndexByNameW
PdhLookupPerfIndexByNameW.restype = wintypes.DWORD
PdhLookupPerfIndexByNameW.argtypes = [
wintypes.LPCWSTR, # szMachineName : LPCWSTR optional
wintypes.LPCWSTR, # szNameBuffer : LPCWSTR
ctypes.POINTER(wintypes.DWORD), # pdwIndex : DWORD* out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('pdh.dll')
PdhLookupPerfIndexByNameW = Fiddle::Function.new(
lib['PdhLookupPerfIndexByNameW'],
[
Fiddle::TYPE_VOIDP, # szMachineName : LPCWSTR optional
Fiddle::TYPE_VOIDP, # szNameBuffer : LPCWSTR
Fiddle::TYPE_VOIDP, # pdwIndex : DWORD* out
],
-Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "pdh")]
extern "system" {
fn PdhLookupPerfIndexByNameW(
szMachineName: *const u16, // LPCWSTR optional
szNameBuffer: *const u16, // LPCWSTR
pdwIndex: *mut u32 // DWORD* out
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("pdh.dll", CharSet = CharSet.Unicode)]
public static extern uint PdhLookupPerfIndexByNameW([MarshalAs(UnmanagedType.LPWStr)] string szMachineName, [MarshalAs(UnmanagedType.LPWStr)] string szNameBuffer, out uint pdwIndex);
"@
$api = Add-Type -MemberDefinition $sig -Name 'pdh_PdhLookupPerfIndexByNameW' -Namespace Win32 -PassThru
# $api::PdhLookupPerfIndexByNameW(szMachineName, szNameBuffer, pdwIndex)#uselib "pdh.dll"
#func global PdhLookupPerfIndexByNameW "PdhLookupPerfIndexByNameW" wptr, wptr, wptr
; PdhLookupPerfIndexByNameW szMachineName, szNameBuffer, varptr(pdwIndex) ; 戻り値は stat
; szMachineName : LPCWSTR optional -> "wptr"
; szNameBuffer : LPCWSTR -> "wptr"
; pdwIndex : DWORD* out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "pdh.dll" #cfunc global PdhLookupPerfIndexByNameW "PdhLookupPerfIndexByNameW" wstr, wstr, var ; res = PdhLookupPerfIndexByNameW(szMachineName, szNameBuffer, pdwIndex) ; szMachineName : LPCWSTR optional -> "wstr" ; szNameBuffer : LPCWSTR -> "wstr" ; pdwIndex : DWORD* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "pdh.dll" #cfunc global PdhLookupPerfIndexByNameW "PdhLookupPerfIndexByNameW" wstr, wstr, sptr ; res = PdhLookupPerfIndexByNameW(szMachineName, szNameBuffer, varptr(pdwIndex)) ; szMachineName : LPCWSTR optional -> "wstr" ; szNameBuffer : LPCWSTR -> "wstr" ; pdwIndex : DWORD* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; DWORD PdhLookupPerfIndexByNameW(LPCWSTR szMachineName, LPCWSTR szNameBuffer, DWORD* pdwIndex) #uselib "pdh.dll" #cfunc global PdhLookupPerfIndexByNameW "PdhLookupPerfIndexByNameW" wstr, wstr, var ; res = PdhLookupPerfIndexByNameW(szMachineName, szNameBuffer, pdwIndex) ; szMachineName : LPCWSTR optional -> "wstr" ; szNameBuffer : LPCWSTR -> "wstr" ; pdwIndex : DWORD* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; DWORD PdhLookupPerfIndexByNameW(LPCWSTR szMachineName, LPCWSTR szNameBuffer, DWORD* pdwIndex) #uselib "pdh.dll" #cfunc global PdhLookupPerfIndexByNameW "PdhLookupPerfIndexByNameW" wstr, wstr, intptr ; res = PdhLookupPerfIndexByNameW(szMachineName, szNameBuffer, varptr(pdwIndex)) ; szMachineName : LPCWSTR optional -> "wstr" ; szNameBuffer : LPCWSTR -> "wstr" ; pdwIndex : DWORD* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
pdh = windows.NewLazySystemDLL("pdh.dll")
procPdhLookupPerfIndexByNameW = pdh.NewProc("PdhLookupPerfIndexByNameW")
)
// szMachineName (LPCWSTR optional), szNameBuffer (LPCWSTR), pdwIndex (DWORD* out)
r1, _, err := procPdhLookupPerfIndexByNameW.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(szMachineName))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(szNameBuffer))),
uintptr(pdwIndex),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // DWORDfunction PdhLookupPerfIndexByNameW(
szMachineName: PWideChar; // LPCWSTR optional
szNameBuffer: PWideChar; // LPCWSTR
pdwIndex: Pointer // DWORD* out
): DWORD; stdcall;
external 'pdh.dll' name 'PdhLookupPerfIndexByNameW';result := DllCall("pdh\PdhLookupPerfIndexByNameW"
, "WStr", szMachineName ; LPCWSTR optional
, "WStr", szNameBuffer ; LPCWSTR
, "Ptr", pdwIndex ; DWORD* out
, "UInt") ; return: DWORD●PdhLookupPerfIndexByNameW(szMachineName, szNameBuffer, pdwIndex) = DLL("pdh.dll", "dword PdhLookupPerfIndexByNameW(char*, char*, void*)")
# 呼び出し: PdhLookupPerfIndexByNameW(szMachineName, szNameBuffer, pdwIndex)
# szMachineName : LPCWSTR optional -> "char*"
# szNameBuffer : LPCWSTR -> "char*"
# pdwIndex : DWORD* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "pdh" fn PdhLookupPerfIndexByNameW(
szMachineName: [*c]const u16, // LPCWSTR optional
szNameBuffer: [*c]const u16, // LPCWSTR
pdwIndex: [*c]u32 // DWORD* out
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc PdhLookupPerfIndexByNameW(
szMachineName: WideCString, # LPCWSTR optional
szNameBuffer: WideCString, # LPCWSTR
pdwIndex: ptr uint32 # DWORD* out
): uint32 {.importc: "PdhLookupPerfIndexByNameW", stdcall, dynlib: "pdh.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "pdh");
extern(Windows)
uint PdhLookupPerfIndexByNameW(
const(wchar)* szMachineName, // LPCWSTR optional
const(wchar)* szNameBuffer, // LPCWSTR
uint* pdwIndex // DWORD* out
);ccall((:PdhLookupPerfIndexByNameW, "pdh.dll"), stdcall, UInt32,
(Cwstring, Cwstring, Ptr{UInt32}),
szMachineName, szNameBuffer, pdwIndex)
# szMachineName : LPCWSTR optional -> Cwstring
# szNameBuffer : LPCWSTR -> Cwstring
# pdwIndex : DWORD* out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
uint32_t PdhLookupPerfIndexByNameW(
const uint16_t* szMachineName,
const uint16_t* szNameBuffer,
uint32_t* pdwIndex);
]]
local pdh = ffi.load("pdh")
-- pdh.PdhLookupPerfIndexByNameW(szMachineName, szNameBuffer, pdwIndex)
-- szMachineName : LPCWSTR optional
-- szNameBuffer : LPCWSTR
-- pdwIndex : DWORD* 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 PdhLookupPerfIndexByNameW = lib.func('__stdcall', 'PdhLookupPerfIndexByNameW', 'uint32_t', ['str16', 'str16', 'uint32_t *']);
// PdhLookupPerfIndexByNameW(szMachineName, szNameBuffer, pdwIndex)
// szMachineName : LPCWSTR optional -> 'str16'
// szNameBuffer : LPCWSTR -> 'str16'
// pdwIndex : DWORD* out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("pdh.dll", {
PdhLookupPerfIndexByNameW: { parameters: ["buffer", "buffer", "pointer"], result: "u32" },
});
// lib.symbols.PdhLookupPerfIndexByNameW(szMachineName, szNameBuffer, pdwIndex)
// szMachineName : LPCWSTR optional -> "buffer"
// szNameBuffer : LPCWSTR -> "buffer"
// pdwIndex : DWORD* out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t PdhLookupPerfIndexByNameW(
const uint16_t* szMachineName,
const uint16_t* szNameBuffer,
uint32_t* pdwIndex);
C, "pdh.dll");
// $ffi->PdhLookupPerfIndexByNameW(szMachineName, szNameBuffer, pdwIndex);
// szMachineName : LPCWSTR optional
// szNameBuffer : LPCWSTR
// pdwIndex : DWORD* 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 PdhLookupPerfIndexByNameW(
WString szMachineName, // LPCWSTR optional
WString szNameBuffer, // LPCWSTR
IntByReference pdwIndex // DWORD* out
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("pdh")]
lib Libpdh
fun PdhLookupPerfIndexByNameW = PdhLookupPerfIndexByNameW(
szMachineName : UInt16*, # LPCWSTR optional
szNameBuffer : UInt16*, # LPCWSTR
pdwIndex : UInt32* # DWORD* out
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef PdhLookupPerfIndexByNameWNative = Uint32 Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Uint32>);
typedef PdhLookupPerfIndexByNameWDart = int Function(Pointer<Utf16>, Pointer<Utf16>, Pointer<Uint32>);
final PdhLookupPerfIndexByNameW = DynamicLibrary.open('pdh.dll')
.lookupFunction<PdhLookupPerfIndexByNameWNative, PdhLookupPerfIndexByNameWDart>('PdhLookupPerfIndexByNameW');
// szMachineName : LPCWSTR optional -> Pointer<Utf16>
// szNameBuffer : LPCWSTR -> Pointer<Utf16>
// pdwIndex : DWORD* out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function PdhLookupPerfIndexByNameW(
szMachineName: PWideChar; // LPCWSTR optional
szNameBuffer: PWideChar; // LPCWSTR
pdwIndex: Pointer // DWORD* out
): DWORD; stdcall;
external 'pdh.dll' name 'PdhLookupPerfIndexByNameW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "PdhLookupPerfIndexByNameW"
c_PdhLookupPerfIndexByNameW :: CWString -> CWString -> Ptr Word32 -> IO Word32
-- szMachineName : LPCWSTR optional -> CWString
-- szNameBuffer : LPCWSTR -> CWString
-- pdwIndex : DWORD* out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let pdhlookupperfindexbynamew =
foreign "PdhLookupPerfIndexByNameW"
((ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint32_t) @-> returning uint32_t)
(* szMachineName : LPCWSTR optional -> (ptr uint16_t) *)
(* szNameBuffer : LPCWSTR -> (ptr uint16_t) *)
(* pdwIndex : DWORD* 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 ("PdhLookupPerfIndexByNameW" pdh-lookup-perf-index-by-name-w :convention :stdcall) :uint32
(sz-machine-name (:string :encoding :utf-16le)) ; LPCWSTR optional
(sz-name-buffer (:string :encoding :utf-16le)) ; LPCWSTR
(pdw-index :pointer)) ; DWORD* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $PdhLookupPerfIndexByNameW = Win32::API::More->new('pdh',
'DWORD PdhLookupPerfIndexByNameW(LPCWSTR szMachineName, LPCWSTR szNameBuffer, LPVOID pdwIndex)');
# my $ret = $PdhLookupPerfIndexByNameW->Call($szMachineName, $szNameBuffer, $pdwIndex);
# szMachineName : LPCWSTR optional -> LPCWSTR
# szNameBuffer : LPCWSTR -> LPCWSTR
# pdwIndex : DWORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f PdhLookupPerfIndexByNameA (ANSI版) — 名前からパフォーマンスインデックスを取得する(ANSI版)。
公式の関連項目
- f PdhLookupPerfNameByIndexW — インデックス番号から対応するパフォーマンス名を取得する。