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

CreateThreadpoolTimer

関数
スレッドプールのタイマーオブジェクトを作成する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

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

PTP_TIMER CreateThreadpoolTimer(
    PTP_TIMER_CALLBACK pfnti,
    void* pv,   // optional
    TP_CALLBACK_ENVIRON_V3* pcbe   // optional
);

パラメーター

名前方向説明
pfntiPTP_TIMER_CALLBACKinタイマーオブジェクトが満了するたびに呼び出されるコールバック関数。詳細については、TimerCallback を参照してください。
pvvoid*inoutoptionalコールバック関数に渡す、アプリケーション定義の省略可能なデータ。
pcbeTP_CALLBACK_ENVIRON_V3*inoptional

コールバックを実行する環境を定義する TP_CALLBACK_ENVIRON 構造体。この構造体は InitializeThreadpoolEnvironment 関数によって返されます。

このパラメーターが NULL の場合、コールバックは既定のコールバック環境で実行されます。詳細については、InitializeThreadpoolEnvironment を参照してください。

戻り値の型: PTP_TIMER

公式ドキュメント

新しいタイマーオブジェクトを作成します。

戻り値

関数が成功した場合、タイマーオブジェクトを定義する TP_TIMER 構造体へのポインターを返します。アプリケーションはこの構造体のメンバーを変更しません。

関数が失敗した場合は NULL を返します。拡張エラー情報を取得するには、GetLastError を呼び出します。

解説(Remarks)

タイマーオブジェクトを設定するには、SetThreadpoolTimer 関数を呼び出します。

この関数を使用するアプリケーションをコンパイルするには、_WIN32_WINNT を 0x0600 以上として定義します。

例については、スレッドプール関数の使用を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

PTP_TIMER CreateThreadpoolTimer(
    PTP_TIMER_CALLBACK pfnti,
    void* pv,   // optional
    TP_CALLBACK_ENVIRON_V3* pcbe   // optional
);
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr CreateThreadpoolTimer(
    IntPtr pfnti,   // PTP_TIMER_CALLBACK
    IntPtr pv,   // void* optional, in/out
    IntPtr pcbe   // TP_CALLBACK_ENVIRON_V3* optional
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CreateThreadpoolTimer(
    pfnti As IntPtr,   ' PTP_TIMER_CALLBACK
    pv As IntPtr,   ' void* optional, in/out
    pcbe As IntPtr   ' TP_CALLBACK_ENVIRON_V3* optional
) As IntPtr
End Function
' pfnti : PTP_TIMER_CALLBACK
' pv : void* optional, in/out
' pcbe : TP_CALLBACK_ENVIRON_V3* optional
Declare PtrSafe Function CreateThreadpoolTimer Lib "kernel32" ( _
    ByVal pfnti As LongPtr, _
    ByVal pv As LongPtr, _
    ByVal pcbe As LongPtr) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CreateThreadpoolTimer = ctypes.windll.kernel32.CreateThreadpoolTimer
CreateThreadpoolTimer.restype = ctypes.c_ssize_t
CreateThreadpoolTimer.argtypes = [
    ctypes.c_void_p,  # pfnti : PTP_TIMER_CALLBACK
    ctypes.POINTER(None),  # pv : void* optional, in/out
    ctypes.c_void_p,  # pcbe : TP_CALLBACK_ENVIRON_V3* optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
CreateThreadpoolTimer = Fiddle::Function.new(
  lib['CreateThreadpoolTimer'],
  [
    Fiddle::TYPE_VOIDP,  # pfnti : PTP_TIMER_CALLBACK
    Fiddle::TYPE_VOIDP,  # pv : void* optional, in/out
    Fiddle::TYPE_VOIDP,  # pcbe : TP_CALLBACK_ENVIRON_V3* optional
  ],
  Fiddle::TYPE_INTPTR_T)
#[link(name = "kernel32")]
extern "system" {
    fn CreateThreadpoolTimer(
        pfnti: *const core::ffi::c_void,  // PTP_TIMER_CALLBACK
        pv: *mut (),  // void* optional, in/out
        pcbe: *mut TP_CALLBACK_ENVIRON_V3  // TP_CALLBACK_ENVIRON_V3* optional
    ) -> isize;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern IntPtr CreateThreadpoolTimer(IntPtr pfnti, IntPtr pv, IntPtr pcbe);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CreateThreadpoolTimer' -Namespace Win32 -PassThru
# $api::CreateThreadpoolTimer(pfnti, pv, pcbe)
#uselib "KERNEL32.dll"
#func global CreateThreadpoolTimer "CreateThreadpoolTimer" sptr, sptr, sptr
; CreateThreadpoolTimer pfnti, pv, varptr(pcbe)   ; 戻り値は stat
; pfnti : PTP_TIMER_CALLBACK -> "sptr"
; pv : void* optional, in/out -> "sptr"
; pcbe : TP_CALLBACK_ENVIRON_V3* optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global CreateThreadpoolTimer "CreateThreadpoolTimer" sptr, sptr, var
; res = CreateThreadpoolTimer(pfnti, pv, pcbe)
; pfnti : PTP_TIMER_CALLBACK -> "sptr"
; pv : void* optional, in/out -> "sptr"
; pcbe : TP_CALLBACK_ENVIRON_V3* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; PTP_TIMER CreateThreadpoolTimer(PTP_TIMER_CALLBACK pfnti, void* pv, TP_CALLBACK_ENVIRON_V3* pcbe)
#uselib "KERNEL32.dll"
#cfunc global CreateThreadpoolTimer "CreateThreadpoolTimer" intptr, intptr, var
; res = CreateThreadpoolTimer(pfnti, pv, pcbe)
; pfnti : PTP_TIMER_CALLBACK -> "intptr"
; pv : void* optional, in/out -> "intptr"
; pcbe : TP_CALLBACK_ENVIRON_V3* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procCreateThreadpoolTimer = kernel32.NewProc("CreateThreadpoolTimer")
)

// pfnti (PTP_TIMER_CALLBACK), pv (void* optional, in/out), pcbe (TP_CALLBACK_ENVIRON_V3* optional)
r1, _, err := procCreateThreadpoolTimer.Call(
	uintptr(pfnti),
	uintptr(pv),
	uintptr(pcbe),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // PTP_TIMER
function CreateThreadpoolTimer(
  pfnti: Pointer;   // PTP_TIMER_CALLBACK
  pv: Pointer;   // void* optional, in/out
  pcbe: Pointer   // TP_CALLBACK_ENVIRON_V3* optional
): NativeInt; stdcall;
  external 'KERNEL32.dll' name 'CreateThreadpoolTimer';
result := DllCall("KERNEL32\CreateThreadpoolTimer"
    , "Ptr", pfnti   ; PTP_TIMER_CALLBACK
    , "Ptr", pv   ; void* optional, in/out
    , "Ptr", pcbe   ; TP_CALLBACK_ENVIRON_V3* optional
    , "Ptr")   ; return: PTP_TIMER
●CreateThreadpoolTimer(pfnti, pv, pcbe) = DLL("KERNEL32.dll", "int CreateThreadpoolTimer(void*, void*, void*)")
# 呼び出し: CreateThreadpoolTimer(pfnti, pv, pcbe)
# pfnti : PTP_TIMER_CALLBACK -> "void*"
# pv : void* optional, in/out -> "void*"
# pcbe : TP_CALLBACK_ENVIRON_V3* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn CreateThreadpoolTimer(
    pfnti: ?*anyopaque, // PTP_TIMER_CALLBACK
    pv: ?*anyopaque, // void* optional, in/out
    pcbe: [*c]TP_CALLBACK_ENVIRON_V3 // TP_CALLBACK_ENVIRON_V3* optional
) callconv(std.os.windows.WINAPI) isize;
proc CreateThreadpoolTimer(
    pfnti: pointer,  # PTP_TIMER_CALLBACK
    pv: pointer,  # void* optional, in/out
    pcbe: ptr TP_CALLBACK_ENVIRON_V3  # TP_CALLBACK_ENVIRON_V3* optional
): int {.importc: "CreateThreadpoolTimer", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
ptrdiff_t CreateThreadpoolTimer(
    void* pfnti,   // PTP_TIMER_CALLBACK
    void* pv,   // void* optional, in/out
    TP_CALLBACK_ENVIRON_V3* pcbe   // TP_CALLBACK_ENVIRON_V3* optional
);
ccall((:CreateThreadpoolTimer, "KERNEL32.dll"), stdcall, Int,
      (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{TP_CALLBACK_ENVIRON_V3}),
      pfnti, pv, pcbe)
# pfnti : PTP_TIMER_CALLBACK -> Ptr{Cvoid}
# pv : void* optional, in/out -> Ptr{Cvoid}
# pcbe : TP_CALLBACK_ENVIRON_V3* optional -> Ptr{TP_CALLBACK_ENVIRON_V3}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
intptr_t CreateThreadpoolTimer(
    void* pfnti,
    void* pv,
    void* pcbe);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.CreateThreadpoolTimer(pfnti, pv, pcbe)
-- pfnti : PTP_TIMER_CALLBACK
-- pv : void* optional, in/out
-- pcbe : TP_CALLBACK_ENVIRON_V3* optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const CreateThreadpoolTimer = lib.func('__stdcall', 'CreateThreadpoolTimer', 'intptr_t', ['void *', 'void *', 'void *']);
// CreateThreadpoolTimer(pfnti, pv, pcbe)
// pfnti : PTP_TIMER_CALLBACK -> 'void *'
// pv : void* optional, in/out -> 'void *'
// pcbe : TP_CALLBACK_ENVIRON_V3* optional -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
// コールバック(関数ポインタ)は koffi.proto/koffi.register で型を定義して渡す(素の void* では JS 関数を渡せない)。
const lib = Deno.dlopen("KERNEL32.dll", {
  CreateThreadpoolTimer: { parameters: ["pointer", "pointer", "pointer"], result: "isize" },
});
// lib.symbols.CreateThreadpoolTimer(pfnti, pv, pcbe)
// pfnti : PTP_TIMER_CALLBACK -> "pointer"
// pv : void* optional, in/out -> "pointer"
// pcbe : TP_CALLBACK_ENVIRON_V3* optional -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
intptr_t CreateThreadpoolTimer(
    void* pfnti,
    void* pv,
    void* pcbe);
C, "KERNEL32.dll");
// $ffi->CreateThreadpoolTimer(pfnti, pv, pcbe);
// pfnti : PTP_TIMER_CALLBACK
// pv : void* optional, in/out
// pcbe : TP_CALLBACK_ENVIRON_V3* optional
// 構造体/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);
    long CreateThreadpoolTimer(
        Callback pfnti,   // PTP_TIMER_CALLBACK
        Pointer pv,   // void* optional, in/out
        Pointer pcbe   // TP_CALLBACK_ENVIRON_V3* optional
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun CreateThreadpoolTimer = CreateThreadpoolTimer(
    pfnti : Void*,   # PTP_TIMER_CALLBACK
    pv : Void*,   # void* optional, in/out
    pcbe : TP_CALLBACK_ENVIRON_V3*   # TP_CALLBACK_ENVIRON_V3* optional
  ) : LibC::SSizeT
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef CreateThreadpoolTimerNative = IntPtr Function(Pointer<Void>, Pointer<Void>, Pointer<Void>);
typedef CreateThreadpoolTimerDart = int Function(Pointer<Void>, Pointer<Void>, Pointer<Void>);
final CreateThreadpoolTimer = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<CreateThreadpoolTimerNative, CreateThreadpoolTimerDart>('CreateThreadpoolTimer');
// pfnti : PTP_TIMER_CALLBACK -> Pointer<Void>
// pv : void* optional, in/out -> Pointer<Void>
// pcbe : TP_CALLBACK_ENVIRON_V3* optional -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CreateThreadpoolTimer(
  pfnti: Pointer;   // PTP_TIMER_CALLBACK
  pv: Pointer;   // void* optional, in/out
  pcbe: Pointer   // TP_CALLBACK_ENVIRON_V3* optional
): NativeInt; stdcall;
  external 'KERNEL32.dll' name 'CreateThreadpoolTimer';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CreateThreadpoolTimer"
  c_CreateThreadpoolTimer :: Ptr () -> Ptr () -> Ptr () -> IO CIntPtr
-- pfnti : PTP_TIMER_CALLBACK -> Ptr ()
-- pv : void* optional, in/out -> Ptr ()
-- pcbe : TP_CALLBACK_ENVIRON_V3* optional -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let createthreadpooltimer =
  foreign "CreateThreadpoolTimer"
    ((ptr void) @-> (ptr void) @-> (ptr void) @-> returning intptr_t)
(* pfnti : PTP_TIMER_CALLBACK -> (ptr void) *)
(* pv : void* optional, in/out -> (ptr void) *)
(* pcbe : TP_CALLBACK_ENVIRON_V3* optional -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("CreateThreadpoolTimer" create-threadpool-timer :convention :stdcall) :int64
  (pfnti :pointer)   ; PTP_TIMER_CALLBACK
  (pv :pointer)   ; void* optional, in/out
  (pcbe :pointer))   ; TP_CALLBACK_ENVIRON_V3* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CreateThreadpoolTimer = Win32::API::More->new('KERNEL32',
    'LPARAM CreateThreadpoolTimer(LPVOID pfnti, LPVOID pv, LPVOID pcbe)');
# my $ret = $CreateThreadpoolTimer->Call($pfnti, $pv, $pcbe);
# pfnti : PTP_TIMER_CALLBACK -> LPVOID
# pv : void* optional, in/out -> LPVOID
# pcbe : TP_CALLBACK_ENVIRON_V3* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# コールバック(関数ポインタ)は Perl sub を直接渡せません。Win32::API::Callback を使用してください。

関連項目

公式の関連項目
使用する型