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

SetWindowsHookExA

関数
フックチェーンにフックプロシージャをインストールする(ANSI版)。
DLLUSER32.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

HHOOK SetWindowsHookExA(
    WINDOWS_HOOK_ID idHook,
    HOOKPROC lpfn,
    HINSTANCE hmod,   // optional
    DWORD dwThreadId
);

パラメーター

名前方向
idHookWINDOWS_HOOK_IDin
lpfnHOOKPROCin
hmodHINSTANCEinoptional
dwThreadIdDWORDin

戻り値の型: HHOOK

各言語での呼び出し定義

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

HHOOK SetWindowsHookExA(
    WINDOWS_HOOK_ID idHook,
    HOOKPROC lpfn,
    HINSTANCE hmod,   // optional
    DWORD dwThreadId
);
[DllImport("USER32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern IntPtr SetWindowsHookExA(
    int idHook,   // WINDOWS_HOOK_ID
    IntPtr lpfn,   // HOOKPROC
    IntPtr hmod,   // HINSTANCE optional
    uint dwThreadId   // DWORD
);
<DllImport("USER32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetWindowsHookExA(
    idHook As Integer,   ' WINDOWS_HOOK_ID
    lpfn As IntPtr,   ' HOOKPROC
    hmod As IntPtr,   ' HINSTANCE optional
    dwThreadId As UInteger   ' DWORD
) As IntPtr
End Function
' idHook : WINDOWS_HOOK_ID
' lpfn : HOOKPROC
' hmod : HINSTANCE optional
' dwThreadId : DWORD
Declare PtrSafe Function SetWindowsHookExA Lib "user32" ( _
    ByVal idHook As Long, _
    ByVal lpfn As LongPtr, _
    ByVal hmod As LongPtr, _
    ByVal dwThreadId As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetWindowsHookExA = ctypes.windll.user32.SetWindowsHookExA
SetWindowsHookExA.restype = ctypes.c_void_p
SetWindowsHookExA.argtypes = [
    ctypes.c_int,  # idHook : WINDOWS_HOOK_ID
    ctypes.c_void_p,  # lpfn : HOOKPROC
    wintypes.HANDLE,  # hmod : HINSTANCE optional
    wintypes.DWORD,  # dwThreadId : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
SetWindowsHookExA = Fiddle::Function.new(
  lib['SetWindowsHookExA'],
  [
    Fiddle::TYPE_INT,  # idHook : WINDOWS_HOOK_ID
    Fiddle::TYPE_VOIDP,  # lpfn : HOOKPROC
    Fiddle::TYPE_VOIDP,  # hmod : HINSTANCE optional
    -Fiddle::TYPE_INT,  # dwThreadId : DWORD
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "user32")]
extern "system" {
    fn SetWindowsHookExA(
        idHook: i32,  // WINDOWS_HOOK_ID
        lpfn: *const core::ffi::c_void,  // HOOKPROC
        hmod: *mut core::ffi::c_void,  // HINSTANCE optional
        dwThreadId: u32  // DWORD
    ) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("USER32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern IntPtr SetWindowsHookExA(int idHook, IntPtr lpfn, IntPtr hmod, uint dwThreadId);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_SetWindowsHookExA' -Namespace Win32 -PassThru
# $api::SetWindowsHookExA(idHook, lpfn, hmod, dwThreadId)
#uselib "USER32.dll"
#func global SetWindowsHookExA "SetWindowsHookExA" sptr, sptr, sptr, sptr
; SetWindowsHookExA idHook, lpfn, hmod, dwThreadId   ; 戻り値は stat
; idHook : WINDOWS_HOOK_ID -> "sptr"
; lpfn : HOOKPROC -> "sptr"
; hmod : HINSTANCE optional -> "sptr"
; dwThreadId : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global SetWindowsHookExA "SetWindowsHookExA" int, sptr, sptr, int
; res = SetWindowsHookExA(idHook, lpfn, hmod, dwThreadId)
; idHook : WINDOWS_HOOK_ID -> "int"
; lpfn : HOOKPROC -> "sptr"
; hmod : HINSTANCE optional -> "sptr"
; dwThreadId : DWORD -> "int"
; HHOOK SetWindowsHookExA(WINDOWS_HOOK_ID idHook, HOOKPROC lpfn, HINSTANCE hmod, DWORD dwThreadId)
#uselib "USER32.dll"
#cfunc global SetWindowsHookExA "SetWindowsHookExA" int, intptr, intptr, int
; res = SetWindowsHookExA(idHook, lpfn, hmod, dwThreadId)
; idHook : WINDOWS_HOOK_ID -> "int"
; lpfn : HOOKPROC -> "intptr"
; hmod : HINSTANCE optional -> "intptr"
; dwThreadId : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSetWindowsHookExA = user32.NewProc("SetWindowsHookExA")
)

// idHook (WINDOWS_HOOK_ID), lpfn (HOOKPROC), hmod (HINSTANCE optional), dwThreadId (DWORD)
r1, _, err := procSetWindowsHookExA.Call(
	uintptr(idHook),
	uintptr(lpfn),
	uintptr(hmod),
	uintptr(dwThreadId),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HHOOK
function SetWindowsHookExA(
  idHook: Integer;   // WINDOWS_HOOK_ID
  lpfn: Pointer;   // HOOKPROC
  hmod: THandle;   // HINSTANCE optional
  dwThreadId: DWORD   // DWORD
): THandle; stdcall;
  external 'USER32.dll' name 'SetWindowsHookExA';
result := DllCall("USER32\SetWindowsHookExA"
    , "Int", idHook   ; WINDOWS_HOOK_ID
    , "Ptr", lpfn   ; HOOKPROC
    , "Ptr", hmod   ; HINSTANCE optional
    , "UInt", dwThreadId   ; DWORD
    , "Ptr")   ; return: HHOOK
●SetWindowsHookExA(idHook, lpfn, hmod, dwThreadId) = DLL("USER32.dll", "void* SetWindowsHookExA(int, void*, void*, dword)")
# 呼び出し: SetWindowsHookExA(idHook, lpfn, hmod, dwThreadId)
# idHook : WINDOWS_HOOK_ID -> "int"
# lpfn : HOOKPROC -> "void*"
# hmod : HINSTANCE optional -> "void*"
# dwThreadId : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。