ホーム › UI.Accessibility › SetWinEventHook
SetWinEventHook
関数指定範囲のWinEventを受け取るフックを設定する。
シグネチャ
// USER32.dll
#include <windows.h>
HWINEVENTHOOK SetWinEventHook(
DWORD eventMin,
DWORD eventMax,
HMODULE hmodWinEventProc, // optional
WINEVENTPROC pfnWinEventProc,
DWORD idProcess,
DWORD idThread,
DWORD dwFlags
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| eventMin | DWORD | in | フックする最小のイベント定数。監視範囲の下限を示す。 |
| eventMax | DWORD | in | フックする最大のイベント定数。監視範囲の上限を示す。 |
| hmodWinEventProc | HMODULE | inoptional | コールバック関数を含むDLLのモジュールハンドル。インプロセス時に指定、それ以外はNULL。 |
| pfnWinEventProc | WINEVENTPROC | in | イベント発生時に呼び出されるWINEVENTPROCコールバック関数へのポインター。 |
| idProcess | DWORD | in | 監視対象のプロセスID。全プロセス監視は0を指定する。 |
| idThread | DWORD | in | 監視対象のスレッドID。全スレッド監視は0を指定する。 |
| dwFlags | DWORD | in | フックの動作を制御するフラグ(WINEVENT_INCONTEXT・WINEVENT_OUTOFCONTEXT等)。 |
戻り値の型: HWINEVENTHOOK
各言語での呼び出し定義
// USER32.dll
#include <windows.h>
HWINEVENTHOOK SetWinEventHook(
DWORD eventMin,
DWORD eventMax,
HMODULE hmodWinEventProc, // optional
WINEVENTPROC pfnWinEventProc,
DWORD idProcess,
DWORD idThread,
DWORD dwFlags
);[DllImport("USER32.dll", ExactSpelling = true)]
static extern IntPtr SetWinEventHook(
uint eventMin, // DWORD
uint eventMax, // DWORD
IntPtr hmodWinEventProc, // HMODULE optional
IntPtr pfnWinEventProc, // WINEVENTPROC
uint idProcess, // DWORD
uint idThread, // DWORD
uint dwFlags // DWORD
);<DllImport("USER32.dll", ExactSpelling:=True)>
Public Shared Function SetWinEventHook(
eventMin As UInteger, ' DWORD
eventMax As UInteger, ' DWORD
hmodWinEventProc As IntPtr, ' HMODULE optional
pfnWinEventProc As IntPtr, ' WINEVENTPROC
idProcess As UInteger, ' DWORD
idThread As UInteger, ' DWORD
dwFlags As UInteger ' DWORD
) As IntPtr
End Function' eventMin : DWORD
' eventMax : DWORD
' hmodWinEventProc : HMODULE optional
' pfnWinEventProc : WINEVENTPROC
' idProcess : DWORD
' idThread : DWORD
' dwFlags : DWORD
Declare PtrSafe Function SetWinEventHook Lib "user32" ( _
ByVal eventMin As Long, _
ByVal eventMax As Long, _
ByVal hmodWinEventProc As LongPtr, _
ByVal pfnWinEventProc As LongPtr, _
ByVal idProcess As Long, _
ByVal idThread As Long, _
ByVal dwFlags As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
SetWinEventHook = ctypes.windll.user32.SetWinEventHook
SetWinEventHook.restype = ctypes.c_void_p
SetWinEventHook.argtypes = [
wintypes.DWORD, # eventMin : DWORD
wintypes.DWORD, # eventMax : DWORD
wintypes.HANDLE, # hmodWinEventProc : HMODULE optional
ctypes.c_void_p, # pfnWinEventProc : WINEVENTPROC
wintypes.DWORD, # idProcess : DWORD
wintypes.DWORD, # idThread : DWORD
wintypes.DWORD, # dwFlags : DWORD
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('USER32.dll')
SetWinEventHook = Fiddle::Function.new(
lib['SetWinEventHook'],
[
-Fiddle::TYPE_INT, # eventMin : DWORD
-Fiddle::TYPE_INT, # eventMax : DWORD
Fiddle::TYPE_VOIDP, # hmodWinEventProc : HMODULE optional
Fiddle::TYPE_VOIDP, # pfnWinEventProc : WINEVENTPROC
-Fiddle::TYPE_INT, # idProcess : DWORD
-Fiddle::TYPE_INT, # idThread : DWORD
-Fiddle::TYPE_INT, # dwFlags : DWORD
],
Fiddle::TYPE_VOIDP)#[link(name = "user32")]
extern "system" {
fn SetWinEventHook(
eventMin: u32, // DWORD
eventMax: u32, // DWORD
hmodWinEventProc: *mut core::ffi::c_void, // HMODULE optional
pfnWinEventProc: *const core::ffi::c_void, // WINEVENTPROC
idProcess: u32, // DWORD
idThread: u32, // DWORD
dwFlags: u32 // DWORD
) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("USER32.dll")]
public static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, IntPtr pfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_SetWinEventHook' -Namespace Win32 -PassThru
# $api::SetWinEventHook(eventMin, eventMax, hmodWinEventProc, pfnWinEventProc, idProcess, idThread, dwFlags)#uselib "USER32.dll"
#func global SetWinEventHook "SetWinEventHook" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; SetWinEventHook eventMin, eventMax, hmodWinEventProc, pfnWinEventProc, idProcess, idThread, dwFlags ; 戻り値は stat
; eventMin : DWORD -> "sptr"
; eventMax : DWORD -> "sptr"
; hmodWinEventProc : HMODULE optional -> "sptr"
; pfnWinEventProc : WINEVENTPROC -> "sptr"
; idProcess : DWORD -> "sptr"
; idThread : DWORD -> "sptr"
; dwFlags : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "USER32.dll"
#cfunc global SetWinEventHook "SetWinEventHook" int, int, sptr, sptr, int, int, int
; res = SetWinEventHook(eventMin, eventMax, hmodWinEventProc, pfnWinEventProc, idProcess, idThread, dwFlags)
; eventMin : DWORD -> "int"
; eventMax : DWORD -> "int"
; hmodWinEventProc : HMODULE optional -> "sptr"
; pfnWinEventProc : WINEVENTPROC -> "sptr"
; idProcess : DWORD -> "int"
; idThread : DWORD -> "int"
; dwFlags : DWORD -> "int"; HWINEVENTHOOK SetWinEventHook(DWORD eventMin, DWORD eventMax, HMODULE hmodWinEventProc, WINEVENTPROC pfnWinEventProc, DWORD idProcess, DWORD idThread, DWORD dwFlags)
#uselib "USER32.dll"
#cfunc global SetWinEventHook "SetWinEventHook" int, int, intptr, intptr, int, int, int
; res = SetWinEventHook(eventMin, eventMax, hmodWinEventProc, pfnWinEventProc, idProcess, idThread, dwFlags)
; eventMin : DWORD -> "int"
; eventMax : DWORD -> "int"
; hmodWinEventProc : HMODULE optional -> "intptr"
; pfnWinEventProc : WINEVENTPROC -> "intptr"
; idProcess : DWORD -> "int"
; idThread : DWORD -> "int"
; dwFlags : DWORD -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
user32 = windows.NewLazySystemDLL("USER32.dll")
procSetWinEventHook = user32.NewProc("SetWinEventHook")
)
// eventMin (DWORD), eventMax (DWORD), hmodWinEventProc (HMODULE optional), pfnWinEventProc (WINEVENTPROC), idProcess (DWORD), idThread (DWORD), dwFlags (DWORD)
r1, _, err := procSetWinEventHook.Call(
uintptr(eventMin),
uintptr(eventMax),
uintptr(hmodWinEventProc),
uintptr(pfnWinEventProc),
uintptr(idProcess),
uintptr(idThread),
uintptr(dwFlags),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HWINEVENTHOOKfunction SetWinEventHook(
eventMin: DWORD; // DWORD
eventMax: DWORD; // DWORD
hmodWinEventProc: THandle; // HMODULE optional
pfnWinEventProc: Pointer; // WINEVENTPROC
idProcess: DWORD; // DWORD
idThread: DWORD; // DWORD
dwFlags: DWORD // DWORD
): THandle; stdcall;
external 'USER32.dll' name 'SetWinEventHook';result := DllCall("USER32\SetWinEventHook"
, "UInt", eventMin ; DWORD
, "UInt", eventMax ; DWORD
, "Ptr", hmodWinEventProc ; HMODULE optional
, "Ptr", pfnWinEventProc ; WINEVENTPROC
, "UInt", idProcess ; DWORD
, "UInt", idThread ; DWORD
, "UInt", dwFlags ; DWORD
, "Ptr") ; return: HWINEVENTHOOK●SetWinEventHook(eventMin, eventMax, hmodWinEventProc, pfnWinEventProc, idProcess, idThread, dwFlags) = DLL("USER32.dll", "void* SetWinEventHook(dword, dword, void*, void*, dword, dword, dword)")
# 呼び出し: SetWinEventHook(eventMin, eventMax, hmodWinEventProc, pfnWinEventProc, idProcess, idThread, dwFlags)
# eventMin : DWORD -> "dword"
# eventMax : DWORD -> "dword"
# hmodWinEventProc : HMODULE optional -> "void*"
# pfnWinEventProc : WINEVENTPROC -> "void*"
# idProcess : DWORD -> "dword"
# idThread : DWORD -> "dword"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "user32" fn SetWinEventHook(
eventMin: u32, // DWORD
eventMax: u32, // DWORD
hmodWinEventProc: ?*anyopaque, // HMODULE optional
pfnWinEventProc: ?*anyopaque, // WINEVENTPROC
idProcess: u32, // DWORD
idThread: u32, // DWORD
dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) ?*anyopaque;proc SetWinEventHook(
eventMin: uint32, # DWORD
eventMax: uint32, # DWORD
hmodWinEventProc: pointer, # HMODULE optional
pfnWinEventProc: pointer, # WINEVENTPROC
idProcess: uint32, # DWORD
idThread: uint32, # DWORD
dwFlags: uint32 # DWORD
): pointer {.importc: "SetWinEventHook", stdcall, dynlib: "USER32.dll".}pragma(lib, "user32");
extern(Windows)
void* SetWinEventHook(
uint eventMin, // DWORD
uint eventMax, // DWORD
void* hmodWinEventProc, // HMODULE optional
void* pfnWinEventProc, // WINEVENTPROC
uint idProcess, // DWORD
uint idThread, // DWORD
uint dwFlags // DWORD
);ccall((:SetWinEventHook, "USER32.dll"), stdcall, Ptr{Cvoid},
(UInt32, UInt32, Ptr{Cvoid}, Ptr{Cvoid}, UInt32, UInt32, UInt32),
eventMin, eventMax, hmodWinEventProc, pfnWinEventProc, idProcess, idThread, dwFlags)
# eventMin : DWORD -> UInt32
# eventMax : DWORD -> UInt32
# hmodWinEventProc : HMODULE optional -> Ptr{Cvoid}
# pfnWinEventProc : WINEVENTPROC -> Ptr{Cvoid}
# idProcess : DWORD -> UInt32
# idThread : DWORD -> UInt32
# dwFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
void* SetWinEventHook(
uint32_t eventMin,
uint32_t eventMax,
void* hmodWinEventProc,
void* pfnWinEventProc,
uint32_t idProcess,
uint32_t idThread,
uint32_t dwFlags);
]]
local user32 = ffi.load("user32")
-- user32.SetWinEventHook(eventMin, eventMax, hmodWinEventProc, pfnWinEventProc, idProcess, idThread, dwFlags)
-- eventMin : DWORD
-- eventMax : DWORD
-- hmodWinEventProc : HMODULE optional
-- pfnWinEventProc : WINEVENTPROC
-- idProcess : DWORD
-- idThread : DWORD
-- dwFlags : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const SetWinEventHook = lib.func('__stdcall', 'SetWinEventHook', 'void *', ['uint32_t', 'uint32_t', 'void *', 'void *', 'uint32_t', 'uint32_t', 'uint32_t']);
// SetWinEventHook(eventMin, eventMax, hmodWinEventProc, pfnWinEventProc, idProcess, idThread, dwFlags)
// eventMin : DWORD -> 'uint32_t'
// eventMax : DWORD -> 'uint32_t'
// hmodWinEventProc : HMODULE optional -> 'void *'
// pfnWinEventProc : WINEVENTPROC -> 'void *'
// idProcess : DWORD -> 'uint32_t'
// idThread : DWORD -> 'uint32_t'
// dwFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
// コールバック(関数ポインタ)は koffi.proto/koffi.register で型を定義して渡す(素の void* では JS 関数を渡せない)。const lib = Deno.dlopen("USER32.dll", {
SetWinEventHook: { parameters: ["u32", "u32", "pointer", "pointer", "u32", "u32", "u32"], result: "pointer" },
});
// lib.symbols.SetWinEventHook(eventMin, eventMax, hmodWinEventProc, pfnWinEventProc, idProcess, idThread, dwFlags)
// eventMin : DWORD -> "u32"
// eventMax : DWORD -> "u32"
// hmodWinEventProc : HMODULE optional -> "pointer"
// pfnWinEventProc : WINEVENTPROC -> "pointer"
// idProcess : DWORD -> "u32"
// idThread : DWORD -> "u32"
// dwFlags : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
void* SetWinEventHook(
uint32_t eventMin,
uint32_t eventMax,
void* hmodWinEventProc,
void* pfnWinEventProc,
uint32_t idProcess,
uint32_t idThread,
uint32_t dwFlags);
C, "USER32.dll");
// $ffi->SetWinEventHook(eventMin, eventMax, hmodWinEventProc, pfnWinEventProc, idProcess, idThread, dwFlags);
// eventMin : DWORD
// eventMax : DWORD
// hmodWinEventProc : HMODULE optional
// pfnWinEventProc : WINEVENTPROC
// idProcess : DWORD
// idThread : DWORD
// dwFlags : 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 User32 extends StdCallLibrary {
User32 INSTANCE = Native.load("user32", User32.class);
Pointer SetWinEventHook(
int eventMin, // DWORD
int eventMax, // DWORD
Pointer hmodWinEventProc, // HMODULE optional
Callback pfnWinEventProc, // WINEVENTPROC
int idProcess, // DWORD
int idThread, // DWORD
int dwFlags // DWORD
);
}@[Link("user32")]
lib LibUSER32
fun SetWinEventHook = SetWinEventHook(
eventMin : UInt32, # DWORD
eventMax : UInt32, # DWORD
hmodWinEventProc : Void*, # HMODULE optional
pfnWinEventProc : Void*, # WINEVENTPROC
idProcess : UInt32, # DWORD
idThread : UInt32, # DWORD
dwFlags : UInt32 # DWORD
) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef SetWinEventHookNative = Pointer<Void> Function(Uint32, Uint32, Pointer<Void>, Pointer<Void>, Uint32, Uint32, Uint32);
typedef SetWinEventHookDart = Pointer<Void> Function(int, int, Pointer<Void>, Pointer<Void>, int, int, int);
final SetWinEventHook = DynamicLibrary.open('USER32.dll')
.lookupFunction<SetWinEventHookNative, SetWinEventHookDart>('SetWinEventHook');
// eventMin : DWORD -> Uint32
// eventMax : DWORD -> Uint32
// hmodWinEventProc : HMODULE optional -> Pointer<Void>
// pfnWinEventProc : WINEVENTPROC -> Pointer<Void>
// idProcess : DWORD -> Uint32
// idThread : DWORD -> Uint32
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function SetWinEventHook(
eventMin: DWORD; // DWORD
eventMax: DWORD; // DWORD
hmodWinEventProc: THandle; // HMODULE optional
pfnWinEventProc: Pointer; // WINEVENTPROC
idProcess: DWORD; // DWORD
idThread: DWORD; // DWORD
dwFlags: DWORD // DWORD
): THandle; stdcall;
external 'USER32.dll' name 'SetWinEventHook';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SetWinEventHook"
c_SetWinEventHook :: Word32 -> Word32 -> Ptr () -> Ptr () -> Word32 -> Word32 -> Word32 -> IO (Ptr ())
-- eventMin : DWORD -> Word32
-- eventMax : DWORD -> Word32
-- hmodWinEventProc : HMODULE optional -> Ptr ()
-- pfnWinEventProc : WINEVENTPROC -> Ptr ()
-- idProcess : DWORD -> Word32
-- idThread : DWORD -> Word32
-- dwFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let setwineventhook =
foreign "SetWinEventHook"
(uint32_t @-> uint32_t @-> (ptr void) @-> (ptr void) @-> uint32_t @-> uint32_t @-> uint32_t @-> returning (ptr void))
(* eventMin : DWORD -> uint32_t *)
(* eventMax : DWORD -> uint32_t *)
(* hmodWinEventProc : HMODULE optional -> (ptr void) *)
(* pfnWinEventProc : WINEVENTPROC -> (ptr void) *)
(* idProcess : DWORD -> uint32_t *)
(* idThread : DWORD -> uint32_t *)
(* dwFlags : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)
(cffi:defcfun ("SetWinEventHook" set-win-event-hook :convention :stdcall) :pointer
(event-min :uint32) ; DWORD
(event-max :uint32) ; DWORD
(hmod-win-event-proc :pointer) ; HMODULE optional
(pfn-win-event-proc :pointer) ; WINEVENTPROC
(id-process :uint32) ; DWORD
(id-thread :uint32) ; DWORD
(dw-flags :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SetWinEventHook = Win32::API::More->new('USER32',
'HANDLE SetWinEventHook(DWORD eventMin, DWORD eventMax, HANDLE hmodWinEventProc, LPVOID pfnWinEventProc, DWORD idProcess, DWORD idThread, DWORD dwFlags)');
# my $ret = $SetWinEventHook->Call($eventMin, $eventMax, $hmodWinEventProc, $pfnWinEventProc, $idProcess, $idThread, $dwFlags);
# eventMin : DWORD -> DWORD
# eventMax : DWORD -> DWORD
# hmodWinEventProc : HMODULE optional -> HANDLE
# pfnWinEventProc : WINEVENTPROC -> LPVOID
# idProcess : DWORD -> DWORD
# idThread : DWORD -> DWORD
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# コールバック(関数ポインタ)は Perl sub を直接渡せません。Win32::API::Callback を使用してください。関連項目
使用する型