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

QueryPerformanceCounter

関数
高分解能パフォーマンスカウンタの現在値を取得する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL QueryPerformanceCounter(
    LONGLONG* lpPerformanceCount
);

パラメーター

名前方向
lpPerformanceCountLONGLONG*out

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL QueryPerformanceCounter(
    LONGLONG* lpPerformanceCount
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool QueryPerformanceCounter(
    out long lpPerformanceCount   // LONGLONG* out
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function QueryPerformanceCounter(
    <Out> ByRef lpPerformanceCount As Long   ' LONGLONG* out
) As Boolean
End Function
' lpPerformanceCount : LONGLONG* out
Declare PtrSafe Function QueryPerformanceCounter Lib "kernel32" ( _
    ByRef lpPerformanceCount As LongLong) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

QueryPerformanceCounter = ctypes.windll.kernel32.QueryPerformanceCounter
QueryPerformanceCounter.restype = wintypes.BOOL
QueryPerformanceCounter.argtypes = [
    ctypes.POINTER(ctypes.c_longlong),  # lpPerformanceCount : LONGLONG* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procQueryPerformanceCounter = kernel32.NewProc("QueryPerformanceCounter")
)

// lpPerformanceCount (LONGLONG* out)
r1, _, err := procQueryPerformanceCounter.Call(
	uintptr(lpPerformanceCount),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function QueryPerformanceCounter(
  lpPerformanceCount: Pointer   // LONGLONG* out
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'QueryPerformanceCounter';
result := DllCall("KERNEL32\QueryPerformanceCounter"
    , "Ptr", lpPerformanceCount   ; LONGLONG* out
    , "Int")   ; return: BOOL
●QueryPerformanceCounter(lpPerformanceCount) = DLL("KERNEL32.dll", "bool QueryPerformanceCounter(void*)")
# 呼び出し: QueryPerformanceCounter(lpPerformanceCount)
# lpPerformanceCount : LONGLONG* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。