ホーム › System.Performance › QueryPerformanceCounter
QueryPerformanceCounter
関数高分解能パフォーマンスカウンタの現在値を取得する。
シグネチャ
// KERNEL32.dll
#include <windows.h>
BOOL QueryPerformanceCounter(
LONGLONG* lpPerformanceCount
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpPerformanceCount | LONGLONG* | out | 現在のパフォーマンスカウンター値 (カウント単位) を受け取る変数へのポインター。 |
戻り値の型: BOOL
公式ドキュメント
パフォーマンスカウンターの現在値を取得します。これは時間間隔の測定に使用できる高分解能 (<1µs) のタイムスタンプです。
戻り値
関数が成功した場合、戻り値は 0 以外です。
関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには GetLastError を呼び出します。Windows XP 以降が動作するシステムでは、有効なパラメーターが渡された場合この関数は常に成功するため、0 を返すことはありません。
解説(Remarks)
この関数とその使用方法の詳細については、Acquiring high-resolution time stamps を参照してください。
例
// Gets the current number of ticks from QueryPerformanceCounter. Throws an
// exception if the call to QueryPerformanceCounter fails.
static inline int64_t GetTicks()
{
LARGE_INTEGER ticks;
if (!QueryPerformanceCounter(&ticks))
{
winrt::throw_last_error();
}
return ticks.QuadPart;
}
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// 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 <MarshalAs(UnmanagedType.Bool)> 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 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global QueryPerformanceCounter "QueryPerformanceCounter" sptr ; res = QueryPerformanceCounter(varptr(lpPerformanceCount)) ; lpPerformanceCount : LONGLONG* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL QueryPerformanceCounter(LONGLONG* lpPerformanceCount) #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" intptr ; res = QueryPerformanceCounter(varptr(lpPerformanceCount)) ; lpPerformanceCount : LONGLONG* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは 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 // BOOLfunction 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)。const std = @import("std");
extern "kernel32" fn QueryPerformanceCounter(
lpPerformanceCount: [*c]i64 // LONGLONG* out
) callconv(std.os.windows.WINAPI) i32;proc QueryPerformanceCounter(
lpPerformanceCount: ptr int64 # LONGLONG* out
): int32 {.importc: "QueryPerformanceCounter", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
int QueryPerformanceCounter(
long* lpPerformanceCount // LONGLONG* out
);ccall((:QueryPerformanceCounter, "KERNEL32.dll"), stdcall, Int32,
(Ptr{Int64},),
lpPerformanceCount)
# lpPerformanceCount : LONGLONG* out -> Ptr{Int64}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t QueryPerformanceCounter(
int64_t* lpPerformanceCount);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.QueryPerformanceCounter(lpPerformanceCount)
-- lpPerformanceCount : LONGLONG* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const QueryPerformanceCounter = lib.func('__stdcall', 'QueryPerformanceCounter', 'int32_t', ['int64_t *']);
// QueryPerformanceCounter(lpPerformanceCount)
// lpPerformanceCount : LONGLONG* out -> 'int64_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
QueryPerformanceCounter: { parameters: ["pointer"], result: "i32" },
});
// lib.symbols.QueryPerformanceCounter(lpPerformanceCount)
// lpPerformanceCount : LONGLONG* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t QueryPerformanceCounter(
int64_t* lpPerformanceCount);
C, "KERNEL32.dll");
// $ffi->QueryPerformanceCounter(lpPerformanceCount);
// lpPerformanceCount : LONGLONG* 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 Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);
boolean QueryPerformanceCounter(
LongByReference lpPerformanceCount // LONGLONG* out
);
}@[Link("kernel32")]
lib LibKERNEL32
fun QueryPerformanceCounter = QueryPerformanceCounter(
lpPerformanceCount : Int64* # LONGLONG* out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef QueryPerformanceCounterNative = Int32 Function(Pointer<Int64>);
typedef QueryPerformanceCounterDart = int Function(Pointer<Int64>);
final QueryPerformanceCounter = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<QueryPerformanceCounterNative, QueryPerformanceCounterDart>('QueryPerformanceCounter');
// lpPerformanceCount : LONGLONG* out -> Pointer<Int64>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function QueryPerformanceCounter(
lpPerformanceCount: Pointer // LONGLONG* out
): BOOL; stdcall;
external 'KERNEL32.dll' name 'QueryPerformanceCounter';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "QueryPerformanceCounter"
c_QueryPerformanceCounter :: Ptr Int64 -> IO CInt
-- lpPerformanceCount : LONGLONG* out -> Ptr Int64
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let queryperformancecounter =
foreign "QueryPerformanceCounter"
((ptr int64_t) @-> returning int32_t)
(* lpPerformanceCount : LONGLONG* out -> (ptr int64_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("QueryPerformanceCounter" query-performance-counter :convention :stdcall) :int32
(lp-performance-count :pointer)) ; LONGLONG* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $QueryPerformanceCounter = Win32::API::More->new('KERNEL32',
'BOOL QueryPerformanceCounter(LPVOID lpPerformanceCount)');
# my $ret = $QueryPerformanceCounter->Call($lpPerformanceCount);
# lpPerformanceCount : LONGLONG* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
公式の関連項目
- f GetSystemTimePreciseAsFileTime — 高精度な現在のシステム時刻をFILETIME形式で取得する。
- f QueryPerformanceFrequency — 高分解能パフォーマンスカウンタの周波数を取得する。