Win32 API 日本語リファレンス
ホームUI.WindowsAndMessaging › SetWindowsHookW

SetWindowsHookW

関数
指定種別のフックプロシージャを設定する旧式関数(Unicode版)。
DLLUSER32.dll文字セットUnicode (-W)呼出規約winapi

シグネチャ

// USER32.dll  (Unicode / -W)
#include <windows.h>

HHOOK SetWindowsHookW(
    INT nFilterType,
    HOOKPROC pfnFilterProc
);

パラメーター

名前方向
nFilterTypeINTin
pfnFilterProcHOOKPROCin

戻り値の型: HHOOK

各言語での呼び出し定義

// USER32.dll  (Unicode / -W)
#include <windows.h>

HHOOK SetWindowsHookW(
    INT nFilterType,
    HOOKPROC pfnFilterProc
);
[DllImport("USER32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern IntPtr SetWindowsHookW(
    int nFilterType,   // INT
    IntPtr pfnFilterProc   // HOOKPROC
);
<DllImport("USER32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function SetWindowsHookW(
    nFilterType As Integer,   ' INT
    pfnFilterProc As IntPtr   ' HOOKPROC
) As IntPtr
End Function
' nFilterType : INT
' pfnFilterProc : HOOKPROC
Declare PtrSafe Function SetWindowsHookW Lib "user32" ( _
    ByVal nFilterType As Long, _
    ByVal pfnFilterProc As LongPtr) As LongPtr
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetWindowsHookW = ctypes.windll.user32.SetWindowsHookW
SetWindowsHookW.restype = ctypes.c_void_p
SetWindowsHookW.argtypes = [
    ctypes.c_int,  # nFilterType : INT
    ctypes.c_void_p,  # pfnFilterProc : HOOKPROC
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
SetWindowsHookW = Fiddle::Function.new(
  lib['SetWindowsHookW'],
  [
    Fiddle::TYPE_INT,  # nFilterType : INT
    Fiddle::TYPE_VOIDP,  # pfnFilterProc : HOOKPROC
  ],
  Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "user32")]
extern "system" {
    fn SetWindowsHookW(
        nFilterType: i32,  // INT
        pfnFilterProc: *const core::ffi::c_void  // HOOKPROC
    ) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("USER32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SetWindowsHookW(int nFilterType, IntPtr pfnFilterProc);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_SetWindowsHookW' -Namespace Win32 -PassThru
# $api::SetWindowsHookW(nFilterType, pfnFilterProc)
#uselib "USER32.dll"
#func global SetWindowsHookW "SetWindowsHookW" wptr, wptr
; SetWindowsHookW nFilterType, pfnFilterProc   ; 戻り値は stat
; nFilterType : INT -> "wptr"
; pfnFilterProc : HOOKPROC -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global SetWindowsHookW "SetWindowsHookW" int, sptr
; res = SetWindowsHookW(nFilterType, pfnFilterProc)
; nFilterType : INT -> "int"
; pfnFilterProc : HOOKPROC -> "sptr"
; HHOOK SetWindowsHookW(INT nFilterType, HOOKPROC pfnFilterProc)
#uselib "USER32.dll"
#cfunc global SetWindowsHookW "SetWindowsHookW" int, intptr
; res = SetWindowsHookW(nFilterType, pfnFilterProc)
; nFilterType : INT -> "int"
; pfnFilterProc : HOOKPROC -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSetWindowsHookW = user32.NewProc("SetWindowsHookW")
)

// nFilterType (INT), pfnFilterProc (HOOKPROC)
r1, _, err := procSetWindowsHookW.Call(
	uintptr(nFilterType),
	uintptr(pfnFilterProc),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HHOOK
function SetWindowsHookW(
  nFilterType: Integer;   // INT
  pfnFilterProc: Pointer   // HOOKPROC
): THandle; stdcall;
  external 'USER32.dll' name 'SetWindowsHookW';
result := DllCall("USER32\SetWindowsHookW"
    , "Int", nFilterType   ; INT
    , "Ptr", pfnFilterProc   ; HOOKPROC
    , "Ptr")   ; return: HHOOK
●SetWindowsHookW(nFilterType, pfnFilterProc) = DLL("USER32.dll", "void* SetWindowsHookW(int, void*)")
# 呼び出し: SetWindowsHookW(nFilterType, pfnFilterProc)
# nFilterType : INT -> "int"
# pfnFilterProc : HOOKPROC -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。