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

SetWindowsHookA

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

シグネチャ

// USER32.dll  (ANSI / -A)
#include <windows.h>

HHOOK SetWindowsHookA(
    INT nFilterType,
    HOOKPROC pfnFilterProc
);

パラメーター

名前方向説明
nFilterTypeINTinインストールするフックの種別。WH_KEYBOARDやWH_MOUSEなどのフックIDを指定する。
pfnFilterProcHOOKPROCinフックが発火した際に呼ばれるフックプロシージャへのポインタ。

戻り値の型: HHOOK

各言語での呼び出し定義

// USER32.dll  (ANSI / -A)
#include <windows.h>

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

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

lib = Fiddle.dlopen('USER32.dll')
SetWindowsHookA = Fiddle::Function.new(
  lib['SetWindowsHookA'],
  [
    Fiddle::TYPE_INT,  # nFilterType : INT
    Fiddle::TYPE_VOIDP,  # pfnFilterProc : HOOKPROC
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "user32")]
extern "system" {
    fn SetWindowsHookA(
        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.Ansi)]
public static extern IntPtr SetWindowsHookA(int nFilterType, IntPtr pfnFilterProc);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_SetWindowsHookA' -Namespace Win32 -PassThru
# $api::SetWindowsHookA(nFilterType, pfnFilterProc)
#uselib "USER32.dll"
#func global SetWindowsHookA "SetWindowsHookA" sptr, sptr
; SetWindowsHookA nFilterType, pfnFilterProc   ; 戻り値は stat
; nFilterType : INT -> "sptr"
; pfnFilterProc : HOOKPROC -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global SetWindowsHookA "SetWindowsHookA" int, sptr
; res = SetWindowsHookA(nFilterType, pfnFilterProc)
; nFilterType : INT -> "int"
; pfnFilterProc : HOOKPROC -> "sptr"
; HHOOK SetWindowsHookA(INT nFilterType, HOOKPROC pfnFilterProc)
#uselib "USER32.dll"
#cfunc global SetWindowsHookA "SetWindowsHookA" int, intptr
; res = SetWindowsHookA(nFilterType, pfnFilterProc)
; nFilterType : INT -> "int"
; pfnFilterProc : HOOKPROC -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSetWindowsHookA = user32.NewProc("SetWindowsHookA")
)

// nFilterType (INT), pfnFilterProc (HOOKPROC)
r1, _, err := procSetWindowsHookA.Call(
	uintptr(nFilterType),
	uintptr(pfnFilterProc),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HHOOK
function SetWindowsHookA(
  nFilterType: Integer;   // INT
  pfnFilterProc: Pointer   // HOOKPROC
): THandle; stdcall;
  external 'USER32.dll' name 'SetWindowsHookA';
result := DllCall("USER32\SetWindowsHookA"
    , "Int", nFilterType   ; INT
    , "Ptr", pfnFilterProc   ; HOOKPROC
    , "Ptr")   ; return: HHOOK
●SetWindowsHookA(nFilterType, pfnFilterProc) = DLL("USER32.dll", "void* SetWindowsHookA(int, void*)")
# 呼び出し: SetWindowsHookA(nFilterType, pfnFilterProc)
# nFilterType : INT -> "int"
# pfnFilterProc : HOOKPROC -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "user32" fn SetWindowsHookA(
    nFilterType: i32, // INT
    pfnFilterProc: ?*anyopaque // HOOKPROC
) callconv(std.os.windows.WINAPI) ?*anyopaque;
proc SetWindowsHookA(
    nFilterType: int32,  # INT
    pfnFilterProc: pointer  # HOOKPROC
): pointer {.importc: "SetWindowsHookA", stdcall, dynlib: "USER32.dll".}
pragma(lib, "user32");
extern(Windows)
void* SetWindowsHookA(
    int nFilterType,   // INT
    void* pfnFilterProc   // HOOKPROC
);
ccall((:SetWindowsHookA, "USER32.dll"), stdcall, Ptr{Cvoid},
      (Int32, Ptr{Cvoid}),
      nFilterType, pfnFilterProc)
# nFilterType : INT -> Int32
# pfnFilterProc : HOOKPROC -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
void* SetWindowsHookA(
    int32_t nFilterType,
    void* pfnFilterProc);
]]
local user32 = ffi.load("user32")
-- user32.SetWindowsHookA(nFilterType, pfnFilterProc)
-- nFilterType : INT
-- pfnFilterProc : HOOKPROC
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const SetWindowsHookA = lib.func('__stdcall', 'SetWindowsHookA', 'void *', ['int32_t', 'void *']);
// SetWindowsHookA(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", {
  SetWindowsHookA: { parameters: ["i32", "pointer"], result: "pointer" },
});
// lib.symbols.SetWindowsHookA(nFilterType, pfnFilterProc)
// nFilterType : INT -> "i32"
// pfnFilterProc : HOOKPROC -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* SetWindowsHookA(
    int32_t nFilterType,
    void* pfnFilterProc);
C, "USER32.dll");
// $ffi->SetWindowsHookA(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.ASCII_OPTIONS);
    Pointer SetWindowsHookA(
        int nFilterType,   // INT
        Callback pfnFilterProc   // HOOKPROC
    );
}
@[Link("user32")]
lib LibUSER32
  fun SetWindowsHookA = SetWindowsHookA(
    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 SetWindowsHookANative = Pointer<Void> Function(Int32, Pointer<Void>);
typedef SetWindowsHookADart = Pointer<Void> Function(int, Pointer<Void>);
final SetWindowsHookA = DynamicLibrary.open('USER32.dll')
    .lookupFunction<SetWindowsHookANative, SetWindowsHookADart>('SetWindowsHookA');
// nFilterType : INT -> Int32
// pfnFilterProc : HOOKPROC -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetWindowsHookA(
  nFilterType: Integer;   // INT
  pfnFilterProc: Pointer   // HOOKPROC
): THandle; stdcall;
  external 'USER32.dll' name 'SetWindowsHookA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let setwindowshooka =
  foreign "SetWindowsHookA"
    (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 ("SetWindowsHookA" set-windows-hook-a :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 $SetWindowsHookA = Win32::API::More->new('USER32',
    'HANDLE SetWindowsHookA(int nFilterType, LPVOID pfnFilterProc)');
# my $ret = $SetWindowsHookA->Call($nFilterType, $pfnFilterProc);
# nFilterType : INT -> int
# pfnFilterProc : HOOKPROC -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# コールバック(関数ポインタ)は Perl sub を直接渡せません。Win32::API::Callback を使用してください。

関連項目

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