AttachThreadInput
関数シグネチャ
// USER32.dll
#include <windows.h>
BOOL AttachThreadInput(
DWORD idAttach,
DWORD idAttachTo,
BOOL fAttach
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| idAttach | DWORD | in | 別のスレッドにアタッチするスレッドの識別子。アタッチするスレッドはシステムスレッドであってはなりません。 |
| idAttachTo | DWORD | in | idAttach をアタッチする先のスレッドの識別子。このスレッドはシステムスレッドであってはなりません。 スレッドは自分自身にはアタッチできません。したがって、idAttachTo を idAttach と等しくすることはできません。 |
| fAttach | BOOL | in | このパラメーターが TRUE の場合、2 つのスレッドがアタッチされます。FALSE の場合、スレッドはデタッチされます。 |
戻り値の型: BOOL
公式ドキュメント
あるスレッドの入力処理メカニズムを別のスレッドのものにアタッチ、またはデタッチします。
戻り値
関数が成功した場合、戻り値は 0 以外になります。
関数が失敗した場合、戻り値は 0 になります。拡張エラー情報を取得するには、 GetLastError を呼び出します。
Windows Server 2003 および Windows XP: 拡張エラー情報はありません。GetLastError を呼び出さないでください。この動作は Windows Vista 以降で変更されました。
解説(Remarks)
AttachThreadInput 関数を使用すると、スレッドは自身の入力状態(キーボード状態や現在のフォーカスウィンドウなど)を別のスレッドと共有できます。両方のスレッドが受け取るキーボードイベントおよびマウスイベントは、AttachThreadInput を再度呼び出して fAttach パラメーターに FALSE を指定することでスレッドがデタッチされるまで、受け取った順序で処理されます。
指定したスレッドのいずれかがメッセージキューを持っていない場合、 AttachThreadInput 関数は失敗します。システムは、スレッドが USER 関数または GDI 関数のいずれかを初めて呼び出したときに、そのスレッドのメッセージキューを作成します。また、ジャーナルレコードフックがインストールされている場合も、 AttachThreadInput 関数は失敗します。ジャーナルレコードフックは、すべての入力キューを互いにアタッチします。
GetKeyState 関数または GetKeyboardState 関数の呼び出しによって取得できるキー状態は、 AttachThreadInput の呼び出し後にリセットされることに注意してください。別のデスクトップ内のスレッドにスレッドをアタッチすることはできません。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// USER32.dll
#include <windows.h>
BOOL AttachThreadInput(
DWORD idAttach,
DWORD idAttachTo,
BOOL fAttach
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", ExactSpelling = true)]
static extern bool AttachThreadInput(
uint idAttach, // DWORD
uint idAttachTo, // DWORD
bool fAttach // BOOL
);<DllImport("USER32.dll", ExactSpelling:=True)>
Public Shared Function AttachThreadInput(
idAttach As UInteger, ' DWORD
idAttachTo As UInteger, ' DWORD
fAttach As Boolean ' BOOL
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' idAttach : DWORD
' idAttachTo : DWORD
' fAttach : BOOL
Declare PtrSafe Function AttachThreadInput Lib "user32" ( _
ByVal idAttach As Long, _
ByVal idAttachTo As Long, _
ByVal fAttach As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
AttachThreadInput = ctypes.windll.user32.AttachThreadInput
AttachThreadInput.restype = wintypes.BOOL
AttachThreadInput.argtypes = [
wintypes.DWORD, # idAttach : DWORD
wintypes.DWORD, # idAttachTo : DWORD
wintypes.BOOL, # fAttach : BOOL
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('USER32.dll')
AttachThreadInput = Fiddle::Function.new(
lib['AttachThreadInput'],
[
-Fiddle::TYPE_INT, # idAttach : DWORD
-Fiddle::TYPE_INT, # idAttachTo : DWORD
Fiddle::TYPE_INT, # fAttach : BOOL
],
Fiddle::TYPE_INT)#[link(name = "user32")]
extern "system" {
fn AttachThreadInput(
idAttach: u32, // DWORD
idAttachTo: u32, // DWORD
fAttach: i32 // BOOL
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll")]
public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_AttachThreadInput' -Namespace Win32 -PassThru
# $api::AttachThreadInput(idAttach, idAttachTo, fAttach)#uselib "USER32.dll"
#func global AttachThreadInput "AttachThreadInput" sptr, sptr, sptr
; AttachThreadInput idAttach, idAttachTo, fAttach ; 戻り値は stat
; idAttach : DWORD -> "sptr"
; idAttachTo : DWORD -> "sptr"
; fAttach : BOOL -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "USER32.dll"
#cfunc global AttachThreadInput "AttachThreadInput" int, int, int
; res = AttachThreadInput(idAttach, idAttachTo, fAttach)
; idAttach : DWORD -> "int"
; idAttachTo : DWORD -> "int"
; fAttach : BOOL -> "int"; BOOL AttachThreadInput(DWORD idAttach, DWORD idAttachTo, BOOL fAttach)
#uselib "USER32.dll"
#cfunc global AttachThreadInput "AttachThreadInput" int, int, int
; res = AttachThreadInput(idAttach, idAttachTo, fAttach)
; idAttach : DWORD -> "int"
; idAttachTo : DWORD -> "int"
; fAttach : BOOL -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
user32 = windows.NewLazySystemDLL("USER32.dll")
procAttachThreadInput = user32.NewProc("AttachThreadInput")
)
// idAttach (DWORD), idAttachTo (DWORD), fAttach (BOOL)
r1, _, err := procAttachThreadInput.Call(
uintptr(idAttach),
uintptr(idAttachTo),
uintptr(fAttach),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction AttachThreadInput(
idAttach: DWORD; // DWORD
idAttachTo: DWORD; // DWORD
fAttach: BOOL // BOOL
): BOOL; stdcall;
external 'USER32.dll' name 'AttachThreadInput';result := DllCall("USER32\AttachThreadInput"
, "UInt", idAttach ; DWORD
, "UInt", idAttachTo ; DWORD
, "Int", fAttach ; BOOL
, "Int") ; return: BOOL●AttachThreadInput(idAttach, idAttachTo, fAttach) = DLL("USER32.dll", "bool AttachThreadInput(dword, dword, bool)")
# 呼び出し: AttachThreadInput(idAttach, idAttachTo, fAttach)
# idAttach : DWORD -> "dword"
# idAttachTo : DWORD -> "dword"
# fAttach : BOOL -> "bool"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "user32" fn AttachThreadInput(
idAttach: u32, // DWORD
idAttachTo: u32, // DWORD
fAttach: i32 // BOOL
) callconv(std.os.windows.WINAPI) i32;proc AttachThreadInput(
idAttach: uint32, # DWORD
idAttachTo: uint32, # DWORD
fAttach: int32 # BOOL
): int32 {.importc: "AttachThreadInput", stdcall, dynlib: "USER32.dll".}pragma(lib, "user32");
extern(Windows)
int AttachThreadInput(
uint idAttach, // DWORD
uint idAttachTo, // DWORD
int fAttach // BOOL
);ccall((:AttachThreadInput, "USER32.dll"), stdcall, Int32,
(UInt32, UInt32, Int32),
idAttach, idAttachTo, fAttach)
# idAttach : DWORD -> UInt32
# idAttachTo : DWORD -> UInt32
# fAttach : BOOL -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t AttachThreadInput(
uint32_t idAttach,
uint32_t idAttachTo,
int32_t fAttach);
]]
local user32 = ffi.load("user32")
-- user32.AttachThreadInput(idAttach, idAttachTo, fAttach)
-- idAttach : DWORD
-- idAttachTo : DWORD
-- fAttach : BOOL
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const AttachThreadInput = lib.func('__stdcall', 'AttachThreadInput', 'int32_t', ['uint32_t', 'uint32_t', 'int32_t']);
// AttachThreadInput(idAttach, idAttachTo, fAttach)
// idAttach : DWORD -> 'uint32_t'
// idAttachTo : DWORD -> 'uint32_t'
// fAttach : BOOL -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("USER32.dll", {
AttachThreadInput: { parameters: ["u32", "u32", "i32"], result: "i32" },
});
// lib.symbols.AttachThreadInput(idAttach, idAttachTo, fAttach)
// idAttach : DWORD -> "u32"
// idAttachTo : DWORD -> "u32"
// fAttach : BOOL -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t AttachThreadInput(
uint32_t idAttach,
uint32_t idAttachTo,
int32_t fAttach);
C, "USER32.dll");
// $ffi->AttachThreadInput(idAttach, idAttachTo, fAttach);
// idAttach : DWORD
// idAttachTo : DWORD
// fAttach : BOOL
// 構造体/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 User32 extends StdCallLibrary {
User32 INSTANCE = Native.load("user32", User32.class);
boolean AttachThreadInput(
int idAttach, // DWORD
int idAttachTo, // DWORD
boolean fAttach // BOOL
);
}@[Link("user32")]
lib LibUSER32
fun AttachThreadInput = AttachThreadInput(
idAttach : UInt32, # DWORD
idAttachTo : UInt32, # DWORD
fAttach : Int32 # BOOL
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef AttachThreadInputNative = Int32 Function(Uint32, Uint32, Int32);
typedef AttachThreadInputDart = int Function(int, int, int);
final AttachThreadInput = DynamicLibrary.open('USER32.dll')
.lookupFunction<AttachThreadInputNative, AttachThreadInputDart>('AttachThreadInput');
// idAttach : DWORD -> Uint32
// idAttachTo : DWORD -> Uint32
// fAttach : BOOL -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function AttachThreadInput(
idAttach: DWORD; // DWORD
idAttachTo: DWORD; // DWORD
fAttach: BOOL // BOOL
): BOOL; stdcall;
external 'USER32.dll' name 'AttachThreadInput';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "AttachThreadInput"
c_AttachThreadInput :: Word32 -> Word32 -> CInt -> IO CInt
-- idAttach : DWORD -> Word32
-- idAttachTo : DWORD -> Word32
-- fAttach : BOOL -> CInt
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let attachthreadinput =
foreign "AttachThreadInput"
(uint32_t @-> uint32_t @-> int32_t @-> returning int32_t)
(* idAttach : DWORD -> uint32_t *)
(* idAttachTo : DWORD -> uint32_t *)
(* fAttach : BOOL -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)
(cffi:defcfun ("AttachThreadInput" attach-thread-input :convention :stdcall) :int32
(id-attach :uint32) ; DWORD
(id-attach-to :uint32) ; DWORD
(f-attach :int32)) ; BOOL
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $AttachThreadInput = Win32::API::More->new('USER32',
'BOOL AttachThreadInput(DWORD idAttach, DWORD idAttachTo, BOOL fAttach)');
# my $ret = $AttachThreadInput->Call($idAttach, $idAttachTo, $fAttach);
# idAttach : DWORD -> DWORD
# idAttachTo : DWORD -> DWORD
# fAttach : BOOL -> BOOL
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f GetCurrentThreadId — 現在のスレッドの識別子を取得する。
- f GetKeyState — 指定した仮想キーの押下・トグル状態を取得する。
- f GetKeyboardState — 全仮想キーの状態を配列としてまとめて取得する。
- f GetWindowThreadProcessId — ウィンドウを作成したスレッドとプロセスのIDを取得する。