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

CreateThreadpoolWait

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

シグネチャ

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

PTP_WAIT CreateThreadpoolWait(
    PTP_WAIT_CALLBACK pfnwa,
    void* pv,   // optional
    TP_CALLBACK_ENVIRON_V3* pcbe   // optional
);

パラメーター

名前方向説明
pfnwaPTP_WAIT_CALLBACKin待機が完了またはタイムアウトしたときに呼び出されるコールバック関数です。詳細については、WaitCallback を参照してください。
pvvoid*inoutoptionalコールバック関数に渡す、アプリケーション定義の省略可能なデータです。
pcbeTP_CALLBACK_ENVIRON_V3*inoptional

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

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

戻り値の型: PTP_WAIT

公式ドキュメント

新しい待機オブジェクトを作成します。

戻り値

関数が成功した場合、待機オブジェクトを定義する TP_WAIT 構造体へのポインターを返します。アプリケーションはこの構造体のメンバーを変更しないでください。

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

解説(Remarks)

待機オブジェクトを設定するには、SetThreadpoolWait または SetThreadpoolWaitEx 関数を呼び出します。

作業項目および作業項目が呼び出すすべての関数は、戻った後にスレッドに依存してはなりません。したがって、既定のコールバック環境からは、REG_NOTIFY_THREAD_AGNOSTIC フラグを指定しない RegNotifyChangeKeyValue 関数のように、永続的なスレッドを必要とする非同期呼び出しを行うことはできません。代わりに、自身でライフタイムを制御するスレッドを使用してください。

この関数を使用するアプリケーションをコンパイルするには、_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_WAIT CreateThreadpoolWait(
    PTP_WAIT_CALLBACK pfnwa,
    void* pv,   // optional
    TP_CALLBACK_ENVIRON_V3* pcbe   // optional
);
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr CreateThreadpoolWait(
    IntPtr pfnwa,   // PTP_WAIT_CALLBACK
    IntPtr pv,   // void* optional, in/out
    IntPtr pcbe   // TP_CALLBACK_ENVIRON_V3* optional
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CreateThreadpoolWait(
    pfnwa As IntPtr,   ' PTP_WAIT_CALLBACK
    pv As IntPtr,   ' void* optional, in/out
    pcbe As IntPtr   ' TP_CALLBACK_ENVIRON_V3* optional
) As IntPtr
End Function
' pfnwa : PTP_WAIT_CALLBACK
' pv : void* optional, in/out
' pcbe : TP_CALLBACK_ENVIRON_V3* optional
Declare PtrSafe Function CreateThreadpoolWait Lib "kernel32" ( _
    ByVal pfnwa 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

CreateThreadpoolWait = ctypes.windll.kernel32.CreateThreadpoolWait
CreateThreadpoolWait.restype = ctypes.c_ssize_t
CreateThreadpoolWait.argtypes = [
    ctypes.c_void_p,  # pfnwa : PTP_WAIT_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')
CreateThreadpoolWait = Fiddle::Function.new(
  lib['CreateThreadpoolWait'],
  [
    Fiddle::TYPE_VOIDP,  # pfnwa : PTP_WAIT_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 CreateThreadpoolWait(
        pfnwa: *const core::ffi::c_void,  // PTP_WAIT_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 CreateThreadpoolWait(IntPtr pfnwa, IntPtr pv, IntPtr pcbe);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CreateThreadpoolWait' -Namespace Win32 -PassThru
# $api::CreateThreadpoolWait(pfnwa, pv, pcbe)
#uselib "KERNEL32.dll"
#func global CreateThreadpoolWait "CreateThreadpoolWait" sptr, sptr, sptr
; CreateThreadpoolWait pfnwa, pv, varptr(pcbe)   ; 戻り値は stat
; pfnwa : PTP_WAIT_CALLBACK -> "sptr"
; pv : void* optional, in/out -> "sptr"
; pcbe : TP_CALLBACK_ENVIRON_V3* optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global CreateThreadpoolWait "CreateThreadpoolWait" sptr, sptr, var
; res = CreateThreadpoolWait(pfnwa, pv, pcbe)
; pfnwa : PTP_WAIT_CALLBACK -> "sptr"
; pv : void* optional, in/out -> "sptr"
; pcbe : TP_CALLBACK_ENVIRON_V3* optional -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; PTP_WAIT CreateThreadpoolWait(PTP_WAIT_CALLBACK pfnwa, void* pv, TP_CALLBACK_ENVIRON_V3* pcbe)
#uselib "KERNEL32.dll"
#cfunc global CreateThreadpoolWait "CreateThreadpoolWait" intptr, intptr, var
; res = CreateThreadpoolWait(pfnwa, pv, pcbe)
; pfnwa : PTP_WAIT_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")
	procCreateThreadpoolWait = kernel32.NewProc("CreateThreadpoolWait")
)

// pfnwa (PTP_WAIT_CALLBACK), pv (void* optional, in/out), pcbe (TP_CALLBACK_ENVIRON_V3* optional)
r1, _, err := procCreateThreadpoolWait.Call(
	uintptr(pfnwa),
	uintptr(pv),
	uintptr(pcbe),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // PTP_WAIT
function CreateThreadpoolWait(
  pfnwa: Pointer;   // PTP_WAIT_CALLBACK
  pv: Pointer;   // void* optional, in/out
  pcbe: Pointer   // TP_CALLBACK_ENVIRON_V3* optional
): NativeInt; stdcall;
  external 'KERNEL32.dll' name 'CreateThreadpoolWait';
result := DllCall("KERNEL32\CreateThreadpoolWait"
    , "Ptr", pfnwa   ; PTP_WAIT_CALLBACK
    , "Ptr", pv   ; void* optional, in/out
    , "Ptr", pcbe   ; TP_CALLBACK_ENVIRON_V3* optional
    , "Ptr")   ; return: PTP_WAIT
●CreateThreadpoolWait(pfnwa, pv, pcbe) = DLL("KERNEL32.dll", "int CreateThreadpoolWait(void*, void*, void*)")
# 呼び出し: CreateThreadpoolWait(pfnwa, pv, pcbe)
# pfnwa : PTP_WAIT_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 CreateThreadpoolWait(
    pfnwa: ?*anyopaque, // PTP_WAIT_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 CreateThreadpoolWait(
    pfnwa: pointer,  # PTP_WAIT_CALLBACK
    pv: pointer,  # void* optional, in/out
    pcbe: ptr TP_CALLBACK_ENVIRON_V3  # TP_CALLBACK_ENVIRON_V3* optional
): int {.importc: "CreateThreadpoolWait", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
ptrdiff_t CreateThreadpoolWait(
    void* pfnwa,   // PTP_WAIT_CALLBACK
    void* pv,   // void* optional, in/out
    TP_CALLBACK_ENVIRON_V3* pcbe   // TP_CALLBACK_ENVIRON_V3* optional
);
ccall((:CreateThreadpoolWait, "KERNEL32.dll"), stdcall, Int,
      (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{TP_CALLBACK_ENVIRON_V3}),
      pfnwa, pv, pcbe)
# pfnwa : PTP_WAIT_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 CreateThreadpoolWait(
    void* pfnwa,
    void* pv,
    void* pcbe);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.CreateThreadpoolWait(pfnwa, pv, pcbe)
-- pfnwa : PTP_WAIT_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 CreateThreadpoolWait = lib.func('__stdcall', 'CreateThreadpoolWait', 'intptr_t', ['void *', 'void *', 'void *']);
// CreateThreadpoolWait(pfnwa, pv, pcbe)
// pfnwa : PTP_WAIT_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", {
  CreateThreadpoolWait: { parameters: ["pointer", "pointer", "pointer"], result: "isize" },
});
// lib.symbols.CreateThreadpoolWait(pfnwa, pv, pcbe)
// pfnwa : PTP_WAIT_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 CreateThreadpoolWait(
    void* pfnwa,
    void* pv,
    void* pcbe);
C, "KERNEL32.dll");
// $ffi->CreateThreadpoolWait(pfnwa, pv, pcbe);
// pfnwa : PTP_WAIT_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 CreateThreadpoolWait(
        Callback pfnwa,   // PTP_WAIT_CALLBACK
        Pointer pv,   // void* optional, in/out
        Pointer pcbe   // TP_CALLBACK_ENVIRON_V3* optional
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun CreateThreadpoolWait = CreateThreadpoolWait(
    pfnwa : Void*,   # PTP_WAIT_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 CreateThreadpoolWaitNative = IntPtr Function(Pointer<Void>, Pointer<Void>, Pointer<Void>);
typedef CreateThreadpoolWaitDart = int Function(Pointer<Void>, Pointer<Void>, Pointer<Void>);
final CreateThreadpoolWait = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<CreateThreadpoolWaitNative, CreateThreadpoolWaitDart>('CreateThreadpoolWait');
// pfnwa : PTP_WAIT_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 CreateThreadpoolWait(
  pfnwa: Pointer;   // PTP_WAIT_CALLBACK
  pv: Pointer;   // void* optional, in/out
  pcbe: Pointer   // TP_CALLBACK_ENVIRON_V3* optional
): NativeInt; stdcall;
  external 'KERNEL32.dll' name 'CreateThreadpoolWait';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CreateThreadpoolWait"
  c_CreateThreadpoolWait :: Ptr () -> Ptr () -> Ptr () -> IO CIntPtr
-- pfnwa : PTP_WAIT_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 createthreadpoolwait =
  foreign "CreateThreadpoolWait"
    ((ptr void) @-> (ptr void) @-> (ptr void) @-> returning intptr_t)
(* pfnwa : PTP_WAIT_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 ("CreateThreadpoolWait" create-threadpool-wait :convention :stdcall) :int64
  (pfnwa :pointer)   ; PTP_WAIT_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 $CreateThreadpoolWait = Win32::API::More->new('KERNEL32',
    'LPARAM CreateThreadpoolWait(LPVOID pfnwa, LPVOID pv, LPVOID pcbe)');
# my $ret = $CreateThreadpoolWait->Call($pfnwa, $pv, $pcbe);
# pfnwa : PTP_WAIT_CALLBACK -> LPVOID
# pv : void* optional, in/out -> LPVOID
# pcbe : TP_CALLBACK_ENVIRON_V3* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# コールバック(関数ポインタ)は Perl sub を直接渡せません。Win32::API::Callback を使用してください。

関連項目

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