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
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)。