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

SetWaitableTimer

関数
発火時刻と周期を指定して待機可能タイマーを設定する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL SetWaitableTimer(
    HANDLE hTimer,
    const LONGLONG* lpDueTime,
    INT lPeriod,
    PTIMERAPCROUTINE pfnCompletionRoutine,   // optional
    void* lpArgToCompletionRoutine,   // optional
    BOOL fResume
);

パラメーター

名前方向
hTimerHANDLEin
lpDueTimeLONGLONG*in
lPeriodINTin
pfnCompletionRoutinePTIMERAPCROUTINEinoptional
lpArgToCompletionRoutinevoid*inoptional
fResumeBOOLin

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL SetWaitableTimer(
    HANDLE hTimer,
    const LONGLONG* lpDueTime,
    INT lPeriod,
    PTIMERAPCROUTINE pfnCompletionRoutine,   // optional
    void* lpArgToCompletionRoutine,   // optional
    BOOL fResume
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool SetWaitableTimer(
    IntPtr hTimer,   // HANDLE
    ref long lpDueTime,   // LONGLONG*
    int lPeriod,   // INT
    IntPtr pfnCompletionRoutine,   // PTIMERAPCROUTINE optional
    IntPtr lpArgToCompletionRoutine,   // void* optional
    bool fResume   // BOOL
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetWaitableTimer(
    hTimer As IntPtr,   ' HANDLE
    ByRef lpDueTime As Long,   ' LONGLONG*
    lPeriod As Integer,   ' INT
    pfnCompletionRoutine As IntPtr,   ' PTIMERAPCROUTINE optional
    lpArgToCompletionRoutine As IntPtr,   ' void* optional
    fResume As Boolean   ' BOOL
) As Boolean
End Function
' hTimer : HANDLE
' lpDueTime : LONGLONG*
' lPeriod : INT
' pfnCompletionRoutine : PTIMERAPCROUTINE optional
' lpArgToCompletionRoutine : void* optional
' fResume : BOOL
Declare PtrSafe Function SetWaitableTimer Lib "kernel32" ( _
    ByVal hTimer As LongPtr, _
    ByRef lpDueTime As LongLong, _
    ByVal lPeriod As Long, _
    ByVal pfnCompletionRoutine As LongPtr, _
    ByVal lpArgToCompletionRoutine As LongPtr, _
    ByVal fResume As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetWaitableTimer = ctypes.windll.kernel32.SetWaitableTimer
SetWaitableTimer.restype = wintypes.BOOL
SetWaitableTimer.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
    wintypes.BOOL,  # fResume : BOOL
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
SetWaitableTimer = Fiddle::Function.new(
  lib['SetWaitableTimer'],
  [
    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_INT,  # fResume : BOOL
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn SetWaitableTimer(
        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
        fResume: i32  // BOOL
    ) -> 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 SetWaitableTimer(IntPtr hTimer, ref long lpDueTime, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr lpArgToCompletionRoutine, bool fResume);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_SetWaitableTimer' -Namespace Win32 -PassThru
# $api::SetWaitableTimer(hTimer, lpDueTime, lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, fResume)
#uselib "KERNEL32.dll"
#func global SetWaitableTimer "SetWaitableTimer" sptr, sptr, sptr, sptr, sptr, sptr
; SetWaitableTimer hTimer, varptr(lpDueTime), lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, fResume   ; 戻り値は stat
; hTimer : HANDLE -> "sptr"
; lpDueTime : LONGLONG* -> "sptr"
; lPeriod : INT -> "sptr"
; pfnCompletionRoutine : PTIMERAPCROUTINE optional -> "sptr"
; lpArgToCompletionRoutine : void* optional -> "sptr"
; fResume : BOOL -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global SetWaitableTimer "SetWaitableTimer" sptr, var, int, sptr, sptr, int
; res = SetWaitableTimer(hTimer, lpDueTime, lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, fResume)
; hTimer : HANDLE -> "sptr"
; lpDueTime : LONGLONG* -> "var"
; lPeriod : INT -> "int"
; pfnCompletionRoutine : PTIMERAPCROUTINE optional -> "sptr"
; lpArgToCompletionRoutine : void* optional -> "sptr"
; fResume : BOOL -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL SetWaitableTimer(HANDLE hTimer, LONGLONG* lpDueTime, INT lPeriod, PTIMERAPCROUTINE pfnCompletionRoutine, void* lpArgToCompletionRoutine, BOOL fResume)
#uselib "KERNEL32.dll"
#cfunc global SetWaitableTimer "SetWaitableTimer" intptr, var, int, intptr, intptr, int
; res = SetWaitableTimer(hTimer, lpDueTime, lPeriod, pfnCompletionRoutine, lpArgToCompletionRoutine, fResume)
; hTimer : HANDLE -> "intptr"
; lpDueTime : LONGLONG* -> "var"
; lPeriod : INT -> "int"
; pfnCompletionRoutine : PTIMERAPCROUTINE optional -> "intptr"
; lpArgToCompletionRoutine : void* optional -> "intptr"
; fResume : BOOL -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procSetWaitableTimer = kernel32.NewProc("SetWaitableTimer")
)

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