SetWaitableTimerEx
関数シグネチャ
// KERNEL32.dll
#include <windows.h>
BOOL SetWaitableTimerEx(
HANDLE hTimer,
const LONGLONG* lpDueTime,
INT lPeriod,
PTIMERAPCROUTINE pfnCompletionRoutine, // optional
void* lpArgToCompletionRoutine, // optional
REASON_CONTEXT* WakeContext, // optional
DWORD TolerableDelay
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hTimer | HANDLE | in | タイマーオブジェクトへのハンドル。CreateWaitableTimer 関数または OpenWaitableTimer 関数がこのハンドルを返します。 このハンドルには TIMER_MODIFY_STATE アクセス権が必要です。詳細については、 Synchronization Object Security and Access Rights を参照してください。 |
| lpDueTime | LONGLONG* | in | タイマーの状態がシグナル状態に設定されるまでの時間を、100 ナノセカンド単位で指定します。 FILETIME 構造体で記述される形式を使用してください。正の値は絶対時刻を示します。システムは内部的に UTC ベースの時刻を使用するため、必ず UTC ベースの絶対時刻を使用してください。負の値は相対時刻を示します。実際のタイマー精度はハードウェアの能力に依存します。UTC ベースの時刻の詳細については、 System Time を参照してください。 Windows XP、Windows Server 2003、Windows Vista、Windows 7、Windows Server 2008、Windows Server 2008 R2: 相対時刻を指定した場合、タイマーには低電力状態で費やされた時間が含まれます。たとえば、コンピューターがスリープ状態の間もタイマーはカウントダウンを続けます。 Windows 8 以降、Windows Server 2012 以降: 相対時刻を指定した場合、タイマーには低電力状態で費やされた時間が含まれません。たとえば、コンピューターがスリープ状態の間はタイマーのカウントダウンが続きません。 |
| lPeriod | INT | in | タイマーの周期をミリ秒単位で指定します。lPeriod が 0 の場合、タイマーは 1 回だけシグナル状態になります。lPeriod が 0 より大きい場合、タイマーは周期的になります。周期的タイマーは、 CancelWaitableTimer 関数によってキャンセルされるか、SetWaitableTimerEx によってリセットされるまで、周期が経過するたびに自動的に再アクティブ化されます。lPeriod が 0 より小さい場合、関数は失敗します。 |
| pfnCompletionRoutine | PTIMERAPCROUTINE | inoptional | 省略可能な完了ルーチンへのポインター。完了ルーチンは、タイマーがシグナル状態になったときに実行される、PTIMERAPCROUTINE 型のアプリケーション定義の関数です。タイマーコールバック関数の詳細については、 TimerAPCProc を参照してください。APC とスレッドプールスレッドの詳細については、「解説」を参照してください。 |
| lpArgToCompletionRoutine | void* | inoptional | 完了ルーチンに渡される構造体へのポインター。 |
| WakeContext | REASON_CONTEXT* | inoptional | タイマーのコンテキスト情報を含む REASON_CONTEXT 構造体へのポインター。 |
| TolerableDelay | DWORD | in | 有効期限に対する許容遅延を、ミリ秒単位で指定します。 |
戻り値の型: BOOL
公式ドキュメント
指定された待機可能タイマーをアクティブ化し、タイマーのコンテキスト情報を提供します。指定時刻に達すると、タイマーはシグナル状態になり、タイマーを設定したスレッドが省略可能な完了ルーチンを呼び出します。
戻り値
関数が成功した場合、戻り値は 0 以外です。
関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、 GetLastError を呼び出します。
解説(Remarks)
SetWaitableTimerEx 関数は SetWaitableTimer 関数に似ていますが、SetWaitableTimerEx ではコンテキスト文字列とタイマーの有効期限に対する許容遅延を指定できる点が異なります。
この関数を使用するアプリケーションをコンパイルするには、_WIN32_WINNT を 0x0601 以降として定義します。詳細については、Using the Windows Headers を参照してください。
タイマーは初期状態では非アクティブです。タイマーをアクティブ化するには、 SetWaitableTimerEx を呼び出します。SetWaitableTimerEx を呼び出したときにタイマーが既にアクティブな場合、タイマーは停止され、その後再アクティブ化されます。この方法でタイマーを停止しても、タイマーの状態はシグナル状態に設定されないため、そのタイマーに対する待機操作でブロックされているスレッドはブロックされたままになります。ただし、保留中の完了ルーチンはすべてキャンセルされます。
指定された指定時刻に達すると、タイマーは非アクティブになり、保留中の APC がまだキューに登録されていない場合は、省略可能な APC がタイマーを設定したスレッドのキューに登録されます。タイマーの状態はシグナル状態に設定され、タイマーは指定された周期で再アクティブ化されます。そして、タイマーを設定したスレッドがアラート可能な待機状態に入ったときに完了ルーチンを呼び出します。詳細については、 QueueUserAPC を参照してください。APC は、スレッドプールスレッドに対しては他のシグナル機構ほどうまく機能しないことに注意してください。これは、システムがスレッドプールスレッドの有効期間を制御しているため、通知が配信される前にスレッドが終了される可能性があるためです。pfnCompletionRoutine パラメーターやその他の APC ベースのシグナル機構を使用する代わりに、CreateThreadpoolTimer で作成したタイマーなどの待機可能オブジェクトを使用してください。I/O の場合は、CreateThreadpoolIo で作成した I/O 完了オブジェクト、または SetThreadpoolWait 関数にイベントを渡すことができる hEvent ベースの OVERLAPPED 構造体を使用してください。
タイマーを設定したスレッドが終了し、関連付けられた完了ルーチンがある場合、タイマーはキャンセルされます。ただし、タイマーの状態は変更されません。完了ルーチンがない場合、スレッドの終了はタイマーに影響しません。
手動リセットタイマーがシグナル状態に設定されると、タイマーをリセットするために SetWaitableTimerEx が呼び出されるまで、その状態のままになります。その結果、周期的な手動リセットタイマーは、最初の指定時刻に達したときにシグナル状態に設定され、リセットされるまでシグナル状態のままになります。同期タイマーがシグナル状態に設定されると、スレッドがそのタイマーオブジェクトに対する待機操作を完了するまで、その状態のままになります。
システム時刻が調整された場合、保留中の絶対時刻タイマーの指定時刻も調整されます。
SetWaitableTimerEx を呼び出したスレッドが終了すると、タイマーはキャンセルされます。これにより、タイマーがシグナル状態に設定される前に停止され、保留中の APC がキャンセルされます。タイマーのシグナル状態は変更されません。
ウィンドウのイベントをスケジュールするためにタイマーを使用するには、SetTimer 関数を使用してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll
#include <windows.h>
BOOL SetWaitableTimerEx(
HANDLE hTimer,
const LONGLONG* lpDueTime,
INT lPeriod,
PTIMERAPCROUTINE pfnCompletionRoutine, // optional
void* lpArgToCompletionRoutine, // optional
REASON_CONTEXT* WakeContext, // optional
DWORD TolerableDelay
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool SetWaitableTimerEx(
IntPtr hTimer, // HANDLE
ref long lpDueTime, // LONGLONG*
int lPeriod, // INT
IntPtr pfnCompletionRoutine, // PTIMERAPCROUTINE optional
IntPtr lpArgToCompletionRoutine, // void* optional
IntPtr WakeContext, // REASON_CONTEXT* optional
uint TolerableDelay // DWORD
);<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetWaitableTimerEx(
hTimer As IntPtr, ' HANDLE
ByRef lpDueTime As Long, ' LONGLONG*
lPeriod As Integer, ' INT
pfnCompletionRoutine As IntPtr, ' PTIMERAPCROUTINE optional
lpArgToCompletionRoutine As IntPtr, ' void* optional
WakeContext As IntPtr, ' REASON_CONTEXT* optional
TolerableDelay As UInteger ' DWORD
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' hTimer : HANDLE
' lpDueTime : LONGLONG*
' lPeriod : INT
' pfnCompletionRoutine : PTIMERAPCROUTINE optional
' lpArgToCompletionRoutine : void* optional
' WakeContext : REASON_CONTEXT* optional
' TolerableDelay : DWORD
Declare PtrSafe Function SetWaitableTimerEx Lib "kernel32" ( _
ByVal hTimer As LongPtr, _
ByRef lpDueTime As LongLong, _
ByVal lPeriod As Long, _
ByVal pfnCompletionRoutine As LongPtr, _
ByVal lpArgToCompletionRoutine As LongPtr, _
ByVal WakeContext As LongPtr, _
ByVal TolerableDelay As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
SetWaitableTimerEx = ctypes.windll.kernel32.SetWaitableTimerEx
SetWaitableTimerEx.restype = wintypes.BOOL
SetWaitableTimerEx.argtypes = [
wintypes.HANDLE, # hTimer : HANDLE
ctypes.POINTER(ctypes.c_longlong), # lpDueTime : LONGLONG*
ctypes.c_int, # lPeriod : INT
ctypes.c_void_p, # pfnCompletionRoutine : PTIMERAPCROUTINE optional
ctypes.POINTER(None), # lpArgToCompletionRoutine : void* optional
ctypes.c_void_p, # WakeContext : REASON_CONTEXT* optional
wintypes.DWORD, # TolerableDelay : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
SetWaitableTimerEx = Fiddle::Function.new(
lib['SetWaitableTimerEx'],
[
Fiddle::TYPE_VOIDP, # hTimer : HANDLE
Fiddle::TYPE_VOIDP, # lpDueTime : LONGLONG*
Fiddle::TYPE_INT, # lPeriod : INT
Fiddle::TYPE_VOIDP, # pfnCompletionRoutine : PTIMERAPCROUTINE optional
Fiddle::TYPE_VOIDP, # lpArgToCompletionRoutine : void* optional
Fiddle::TYPE_VOIDP, # WakeContext : REASON_CONTEXT* optional
-Fiddle::TYPE_INT, # TolerableDelay : DWORD
],
Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn SetWaitableTimerEx(
hTimer: *mut core::ffi::c_void, // HANDLE
lpDueTime: *const i64, // LONGLONG*
lPeriod: i32, // INT
pfnCompletionRoutine: *const core::ffi::c_void, // PTIMERAPCROUTINE optional
lpArgToCompletionRoutine: *mut (), // void* optional
WakeContext: *mut REASON_CONTEXT, // REASON_CONTEXT* optional
TolerableDelay: u32 // DWORD
) -> 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 SetWaitableTimerEx(IntPtr hTimer, ref long lpDueTime, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr lpArgToCompletionRoutine, IntPtr WakeContext, uint TolerableDelay);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_SetWaitableTimerEx' -Namespace Win32 -PassThru
# $api::SetWaitableTimerEx(hTimer, lpDueTime, lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, WakeContext, TolerableDelay)#uselib "KERNEL32.dll"
#func global SetWaitableTimerEx "SetWaitableTimerEx" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; SetWaitableTimerEx hTimer, varptr(lpDueTime), lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, varptr(WakeContext), TolerableDelay ; 戻り値は stat
; hTimer : HANDLE -> "sptr"
; lpDueTime : LONGLONG* -> "sptr"
; lPeriod : INT -> "sptr"
; pfnCompletionRoutine : PTIMERAPCROUTINE optional -> "sptr"
; lpArgToCompletionRoutine : void* optional -> "sptr"
; WakeContext : REASON_CONTEXT* optional -> "sptr"
; TolerableDelay : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll" #cfunc global SetWaitableTimerEx "SetWaitableTimerEx" sptr, var, int, sptr, sptr, var, int ; res = SetWaitableTimerEx(hTimer, lpDueTime, lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, WakeContext, TolerableDelay) ; hTimer : HANDLE -> "sptr" ; lpDueTime : LONGLONG* -> "var" ; lPeriod : INT -> "int" ; pfnCompletionRoutine : PTIMERAPCROUTINE optional -> "sptr" ; lpArgToCompletionRoutine : void* optional -> "sptr" ; WakeContext : REASON_CONTEXT* optional -> "var" ; TolerableDelay : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global SetWaitableTimerEx "SetWaitableTimerEx" sptr, sptr, int, sptr, sptr, sptr, int ; res = SetWaitableTimerEx(hTimer, varptr(lpDueTime), lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, varptr(WakeContext), TolerableDelay) ; hTimer : HANDLE -> "sptr" ; lpDueTime : LONGLONG* -> "sptr" ; lPeriod : INT -> "int" ; pfnCompletionRoutine : PTIMERAPCROUTINE optional -> "sptr" ; lpArgToCompletionRoutine : void* optional -> "sptr" ; WakeContext : REASON_CONTEXT* optional -> "sptr" ; TolerableDelay : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; BOOL SetWaitableTimerEx(HANDLE hTimer, LONGLONG* lpDueTime, INT lPeriod, PTIMERAPCROUTINE pfnCompletionRoutine, void* lpArgToCompletionRoutine, REASON_CONTEXT* WakeContext, DWORD TolerableDelay) #uselib "KERNEL32.dll" #cfunc global SetWaitableTimerEx "SetWaitableTimerEx" intptr, var, int, intptr, intptr, var, int ; res = SetWaitableTimerEx(hTimer, lpDueTime, lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, WakeContext, TolerableDelay) ; hTimer : HANDLE -> "intptr" ; lpDueTime : LONGLONG* -> "var" ; lPeriod : INT -> "int" ; pfnCompletionRoutine : PTIMERAPCROUTINE optional -> "intptr" ; lpArgToCompletionRoutine : void* optional -> "intptr" ; WakeContext : REASON_CONTEXT* optional -> "var" ; TolerableDelay : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL SetWaitableTimerEx(HANDLE hTimer, LONGLONG* lpDueTime, INT lPeriod, PTIMERAPCROUTINE pfnCompletionRoutine, void* lpArgToCompletionRoutine, REASON_CONTEXT* WakeContext, DWORD TolerableDelay) #uselib "KERNEL32.dll" #cfunc global SetWaitableTimerEx "SetWaitableTimerEx" intptr, intptr, int, intptr, intptr, intptr, int ; res = SetWaitableTimerEx(hTimer, varptr(lpDueTime), lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, varptr(WakeContext), TolerableDelay) ; hTimer : HANDLE -> "intptr" ; lpDueTime : LONGLONG* -> "intptr" ; lPeriod : INT -> "int" ; pfnCompletionRoutine : PTIMERAPCROUTINE optional -> "intptr" ; lpArgToCompletionRoutine : void* optional -> "intptr" ; WakeContext : REASON_CONTEXT* optional -> "intptr" ; TolerableDelay : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procSetWaitableTimerEx = kernel32.NewProc("SetWaitableTimerEx")
)
// hTimer (HANDLE), lpDueTime (LONGLONG*), lPeriod (INT), pfnCompletionRoutine (PTIMERAPCROUTINE optional), lpArgToCompletionRoutine (void* optional), WakeContext (REASON_CONTEXT* optional), TolerableDelay (DWORD)
r1, _, err := procSetWaitableTimerEx.Call(
uintptr(hTimer),
uintptr(lpDueTime),
uintptr(lPeriod),
uintptr(pfnCompletionRoutine),
uintptr(lpArgToCompletionRoutine),
uintptr(WakeContext),
uintptr(TolerableDelay),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction SetWaitableTimerEx(
hTimer: THandle; // HANDLE
lpDueTime: Pointer; // LONGLONG*
lPeriod: Integer; // INT
pfnCompletionRoutine: Pointer; // PTIMERAPCROUTINE optional
lpArgToCompletionRoutine: Pointer; // void* optional
WakeContext: Pointer; // REASON_CONTEXT* optional
TolerableDelay: DWORD // DWORD
): BOOL; stdcall;
external 'KERNEL32.dll' name 'SetWaitableTimerEx';result := DllCall("KERNEL32\SetWaitableTimerEx"
, "Ptr", hTimer ; HANDLE
, "Ptr", lpDueTime ; LONGLONG*
, "Int", lPeriod ; INT
, "Ptr", pfnCompletionRoutine ; PTIMERAPCROUTINE optional
, "Ptr", lpArgToCompletionRoutine ; void* optional
, "Ptr", WakeContext ; REASON_CONTEXT* optional
, "UInt", TolerableDelay ; DWORD
, "Int") ; return: BOOL●SetWaitableTimerEx(hTimer, lpDueTime, lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, WakeContext, TolerableDelay) = DLL("KERNEL32.dll", "bool SetWaitableTimerEx(void*, void*, int, void*, void*, void*, dword)")
# 呼び出し: SetWaitableTimerEx(hTimer, lpDueTime, lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, WakeContext, TolerableDelay)
# hTimer : HANDLE -> "void*"
# lpDueTime : LONGLONG* -> "void*"
# lPeriod : INT -> "int"
# pfnCompletionRoutine : PTIMERAPCROUTINE optional -> "void*"
# lpArgToCompletionRoutine : void* optional -> "void*"
# WakeContext : REASON_CONTEXT* optional -> "void*"
# TolerableDelay : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "kernel32" fn SetWaitableTimerEx(
hTimer: ?*anyopaque, // HANDLE
lpDueTime: [*c]i64, // LONGLONG*
lPeriod: i32, // INT
pfnCompletionRoutine: ?*anyopaque, // PTIMERAPCROUTINE optional
lpArgToCompletionRoutine: ?*anyopaque, // void* optional
WakeContext: [*c]REASON_CONTEXT, // REASON_CONTEXT* optional
TolerableDelay: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;proc SetWaitableTimerEx(
hTimer: pointer, # HANDLE
lpDueTime: ptr int64, # LONGLONG*
lPeriod: int32, # INT
pfnCompletionRoutine: pointer, # PTIMERAPCROUTINE optional
lpArgToCompletionRoutine: pointer, # void* optional
WakeContext: ptr REASON_CONTEXT, # REASON_CONTEXT* optional
TolerableDelay: uint32 # DWORD
): int32 {.importc: "SetWaitableTimerEx", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
int SetWaitableTimerEx(
void* hTimer, // HANDLE
long* lpDueTime, // LONGLONG*
int lPeriod, // INT
void* pfnCompletionRoutine, // PTIMERAPCROUTINE optional
void* lpArgToCompletionRoutine, // void* optional
REASON_CONTEXT* WakeContext, // REASON_CONTEXT* optional
uint TolerableDelay // DWORD
);ccall((:SetWaitableTimerEx, "KERNEL32.dll"), stdcall, Int32,
(Ptr{Cvoid}, Ptr{Int64}, Int32, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{REASON_CONTEXT}, UInt32),
hTimer, lpDueTime, lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, WakeContext, TolerableDelay)
# hTimer : HANDLE -> Ptr{Cvoid}
# lpDueTime : LONGLONG* -> Ptr{Int64}
# lPeriod : INT -> Int32
# pfnCompletionRoutine : PTIMERAPCROUTINE optional -> Ptr{Cvoid}
# lpArgToCompletionRoutine : void* optional -> Ptr{Cvoid}
# WakeContext : REASON_CONTEXT* optional -> Ptr{REASON_CONTEXT}
# TolerableDelay : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t SetWaitableTimerEx(
void* hTimer,
int64_t* lpDueTime,
int32_t lPeriod,
void* pfnCompletionRoutine,
void* lpArgToCompletionRoutine,
void* WakeContext,
uint32_t TolerableDelay);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.SetWaitableTimerEx(hTimer, lpDueTime, lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, WakeContext, TolerableDelay)
-- hTimer : HANDLE
-- lpDueTime : LONGLONG*
-- lPeriod : INT
-- pfnCompletionRoutine : PTIMERAPCROUTINE optional
-- lpArgToCompletionRoutine : void* optional
-- WakeContext : REASON_CONTEXT* optional
-- TolerableDelay : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const SetWaitableTimerEx = lib.func('__stdcall', 'SetWaitableTimerEx', 'int32_t', ['void *', 'int64_t *', 'int32_t', 'void *', 'void *', 'void *', 'uint32_t']);
// SetWaitableTimerEx(hTimer, lpDueTime, lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, WakeContext, TolerableDelay)
// hTimer : HANDLE -> 'void *'
// lpDueTime : LONGLONG* -> 'int64_t *'
// lPeriod : INT -> 'int32_t'
// pfnCompletionRoutine : PTIMERAPCROUTINE optional -> 'void *'
// lpArgToCompletionRoutine : void* optional -> 'void *'
// WakeContext : REASON_CONTEXT* optional -> 'void *'
// TolerableDelay : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
// コールバック(関数ポインタ)は koffi.proto/koffi.register で型を定義して渡す(素の void* では JS 関数を渡せない)。const lib = Deno.dlopen("KERNEL32.dll", {
SetWaitableTimerEx: { parameters: ["pointer", "pointer", "i32", "pointer", "pointer", "pointer", "u32"], result: "i32" },
});
// lib.symbols.SetWaitableTimerEx(hTimer, lpDueTime, lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, WakeContext, TolerableDelay)
// hTimer : HANDLE -> "pointer"
// lpDueTime : LONGLONG* -> "pointer"
// lPeriod : INT -> "i32"
// pfnCompletionRoutine : PTIMERAPCROUTINE optional -> "pointer"
// lpArgToCompletionRoutine : void* optional -> "pointer"
// WakeContext : REASON_CONTEXT* optional -> "pointer"
// TolerableDelay : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t SetWaitableTimerEx(
void* hTimer,
int64_t* lpDueTime,
int32_t lPeriod,
void* pfnCompletionRoutine,
void* lpArgToCompletionRoutine,
void* WakeContext,
uint32_t TolerableDelay);
C, "KERNEL32.dll");
// $ffi->SetWaitableTimerEx(hTimer, lpDueTime, lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, WakeContext, TolerableDelay);
// hTimer : HANDLE
// lpDueTime : LONGLONG*
// lPeriod : INT
// pfnCompletionRoutine : PTIMERAPCROUTINE optional
// lpArgToCompletionRoutine : void* optional
// WakeContext : REASON_CONTEXT* optional
// TolerableDelay : DWORD
// 構造体/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 SetWaitableTimerEx(
Pointer hTimer, // HANDLE
LongByReference lpDueTime, // LONGLONG*
int lPeriod, // INT
Callback pfnCompletionRoutine, // PTIMERAPCROUTINE optional
Pointer lpArgToCompletionRoutine, // void* optional
Pointer WakeContext, // REASON_CONTEXT* optional
int TolerableDelay // DWORD
);
}@[Link("kernel32")]
lib LibKERNEL32
fun SetWaitableTimerEx = SetWaitableTimerEx(
hTimer : Void*, # HANDLE
lpDueTime : Int64*, # LONGLONG*
lPeriod : Int32, # INT
pfnCompletionRoutine : Void*, # PTIMERAPCROUTINE optional
lpArgToCompletionRoutine : Void*, # void* optional
WakeContext : REASON_CONTEXT*, # REASON_CONTEXT* optional
TolerableDelay : UInt32 # DWORD
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef SetWaitableTimerExNative = Int32 Function(Pointer<Void>, Pointer<Int64>, Int32, Pointer<Void>, Pointer<Void>, Pointer<Void>, Uint32);
typedef SetWaitableTimerExDart = int Function(Pointer<Void>, Pointer<Int64>, int, Pointer<Void>, Pointer<Void>, Pointer<Void>, int);
final SetWaitableTimerEx = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<SetWaitableTimerExNative, SetWaitableTimerExDart>('SetWaitableTimerEx');
// hTimer : HANDLE -> Pointer<Void>
// lpDueTime : LONGLONG* -> Pointer<Int64>
// lPeriod : INT -> Int32
// pfnCompletionRoutine : PTIMERAPCROUTINE optional -> Pointer<Void>
// lpArgToCompletionRoutine : void* optional -> Pointer<Void>
// WakeContext : REASON_CONTEXT* optional -> Pointer<Void>
// TolerableDelay : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function SetWaitableTimerEx(
hTimer: THandle; // HANDLE
lpDueTime: Pointer; // LONGLONG*
lPeriod: Integer; // INT
pfnCompletionRoutine: Pointer; // PTIMERAPCROUTINE optional
lpArgToCompletionRoutine: Pointer; // void* optional
WakeContext: Pointer; // REASON_CONTEXT* optional
TolerableDelay: DWORD // DWORD
): BOOL; stdcall;
external 'KERNEL32.dll' name 'SetWaitableTimerEx';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SetWaitableTimerEx"
c_SetWaitableTimerEx :: Ptr () -> Ptr Int64 -> Int32 -> Ptr () -> Ptr () -> Ptr () -> Word32 -> IO CInt
-- hTimer : HANDLE -> Ptr ()
-- lpDueTime : LONGLONG* -> Ptr Int64
-- lPeriod : INT -> Int32
-- pfnCompletionRoutine : PTIMERAPCROUTINE optional -> Ptr ()
-- lpArgToCompletionRoutine : void* optional -> Ptr ()
-- WakeContext : REASON_CONTEXT* optional -> Ptr ()
-- TolerableDelay : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let setwaitabletimerex =
foreign "SetWaitableTimerEx"
((ptr void) @-> (ptr int64_t) @-> int32_t @-> (ptr void) @-> (ptr void) @-> (ptr void) @-> uint32_t @-> returning int32_t)
(* hTimer : HANDLE -> (ptr void) *)
(* lpDueTime : LONGLONG* -> (ptr int64_t) *)
(* lPeriod : INT -> int32_t *)
(* pfnCompletionRoutine : PTIMERAPCROUTINE optional -> (ptr void) *)
(* lpArgToCompletionRoutine : void* optional -> (ptr void) *)
(* WakeContext : REASON_CONTEXT* optional -> (ptr void) *)
(* TolerableDelay : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("SetWaitableTimerEx" set-waitable-timer-ex :convention :stdcall) :int32
(h-timer :pointer) ; HANDLE
(lp-due-time :pointer) ; LONGLONG*
(l-period :int32) ; INT
(pfn-completion-routine :pointer) ; PTIMERAPCROUTINE optional
(lp-arg-to-completion-routine :pointer) ; void* optional
(wake-context :pointer) ; REASON_CONTEXT* optional
(tolerable-delay :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SetWaitableTimerEx = Win32::API::More->new('KERNEL32',
'BOOL SetWaitableTimerEx(HANDLE hTimer, LPVOID lpDueTime, int lPeriod, LPVOID pfnCompletionRoutine, LPVOID lpArgToCompletionRoutine, LPVOID WakeContext, DWORD TolerableDelay)');
# my $ret = $SetWaitableTimerEx->Call($hTimer, $lpDueTime, $lPeriod, $pfnCompletionRoutine, $lpArgToCompletionRoutine, $WakeContext, $TolerableDelay);
# hTimer : HANDLE -> HANDLE
# lpDueTime : LONGLONG* -> LPVOID
# lPeriod : INT -> int
# pfnCompletionRoutine : PTIMERAPCROUTINE optional -> LPVOID
# lpArgToCompletionRoutine : void* optional -> LPVOID
# WakeContext : REASON_CONTEXT* optional -> LPVOID
# TolerableDelay : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# コールバック(関数ポインタ)は Perl sub を直接渡せません。Win32::API::Callback を使用してください。関連項目
- f SetWaitableTimer — 発火時刻と周期を指定して待機可能タイマーを設定する。