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インストールするフックの種別。WH_KEYBOARDやWH_MOUSEなどのフックIDを指定する。
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 版の利用を推奨。
const std = @import("std");

extern "user32" fn SetWindowsHookW(
    nFilterType: i32, // INT
    pfnFilterProc: ?*anyopaque // HOOKPROC
) callconv(std.os.windows.WINAPI) ?*anyopaque;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc SetWindowsHookW(
    nFilterType: int32,  # INT
    pfnFilterProc: pointer  # HOOKPROC
): pointer {.importc: "SetWindowsHookW", stdcall, dynlib: "USER32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "user32");
extern(Windows)
void* SetWindowsHookW(
    int nFilterType,   // INT
    void* pfnFilterProc   // HOOKPROC
);
ccall((:SetWindowsHookW, "USER32.dll"), stdcall, Ptr{Cvoid},
      (Int32, Ptr{Cvoid}),
      nFilterType, pfnFilterProc)
# nFilterType : INT -> Int32
# pfnFilterProc : HOOKPROC -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
void* SetWindowsHookW(
    int32_t nFilterType,
    void* pfnFilterProc);
]]
local user32 = ffi.load("user32")
-- user32.SetWindowsHookW(nFilterType, pfnFilterProc)
-- nFilterType : INT
-- pfnFilterProc : HOOKPROC
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const SetWindowsHookW = lib.func('__stdcall', 'SetWindowsHookW', 'void *', ['int32_t', 'void *']);
// SetWindowsHookW(nFilterType, pfnFilterProc)
// nFilterType : INT -> 'int32_t'
// pfnFilterProc : HOOKPROC -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
// コールバック(関数ポインタ)は koffi.proto/koffi.register で型を定義して渡す(素の void* では JS 関数を渡せない)。
const lib = Deno.dlopen("USER32.dll", {
  SetWindowsHookW: { parameters: ["i32", "pointer"], result: "pointer" },
});
// lib.symbols.SetWindowsHookW(nFilterType, pfnFilterProc)
// nFilterType : INT -> "i32"
// pfnFilterProc : HOOKPROC -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* SetWindowsHookW(
    int32_t nFilterType,
    void* pfnFilterProc);
C, "USER32.dll");
// $ffi->SetWindowsHookW(nFilterType, pfnFilterProc);
// nFilterType : INT
// pfnFilterProc : HOOKPROC
// 構造体/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, W32APIOptions.UNICODE_OPTIONS);
    Pointer SetWindowsHookW(
        int nFilterType,   // INT
        Callback pfnFilterProc   // HOOKPROC
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("user32")]
lib LibUSER32
  fun SetWindowsHookW = SetWindowsHookW(
    nFilterType : Int32,   # INT
    pfnFilterProc : Void*   # HOOKPROC
  ) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SetWindowsHookWNative = Pointer<Void> Function(Int32, Pointer<Void>);
typedef SetWindowsHookWDart = Pointer<Void> Function(int, Pointer<Void>);
final SetWindowsHookW = DynamicLibrary.open('USER32.dll')
    .lookupFunction<SetWindowsHookWNative, SetWindowsHookWDart>('SetWindowsHookW');
// nFilterType : INT -> Int32
// pfnFilterProc : HOOKPROC -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetWindowsHookW(
  nFilterType: Integer;   // INT
  pfnFilterProc: Pointer   // HOOKPROC
): THandle; stdcall;
  external 'USER32.dll' name 'SetWindowsHookW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetWindowsHookW"
  c_SetWindowsHookW :: Int32 -> Ptr () -> IO (Ptr ())
-- nFilterType : INT -> Int32
-- pfnFilterProc : HOOKPROC -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setwindowshookw =
  foreign "SetWindowsHookW"
    (int32_t @-> (ptr void) @-> returning (ptr void))
(* nFilterType : INT -> int32_t *)
(* pfnFilterProc : HOOKPROC -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)

(cffi:defcfun ("SetWindowsHookW" set-windows-hook-w :convention :stdcall) :pointer
  (n-filter-type :int32)   ; INT
  (pfn-filter-proc :pointer))   ; HOOKPROC
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetWindowsHookW = Win32::API::More->new('USER32',
    'HANDLE SetWindowsHookW(int nFilterType, LPVOID pfnFilterProc)');
# my $ret = $SetWindowsHookW->Call($nFilterType, $pfnFilterProc);
# nFilterType : INT -> int
# pfnFilterProc : HOOKPROC -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# コールバック(関数ポインタ)は Perl sub を直接渡せません。Win32::API::Callback を使用してください。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
類似 API
使用する型