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

SendMessageW

関数
ウィンドウにメッセージを送り処理完了まで待つ(Unicode)。
DLLUSER32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

LRESULT SendMessageW(
    HWND hWnd,
    DWORD Msg,
    WPARAM wParam,   // optional
    LPARAM lParam   // optional
);

パラメーター

名前方向
hWndHWNDin
MsgDWORDin
wParamWPARAMinoptional
lParamLPARAMinoptional

戻り値の型: LRESULT

各言語での呼び出し定義

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

LRESULT SendMessageW(
    HWND hWnd,
    DWORD Msg,
    WPARAM wParam,   // optional
    LPARAM lParam   // optional
);
[DllImport("USER32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern IntPtr SendMessageW(
    IntPtr hWnd,   // HWND
    uint Msg,   // DWORD
    UIntPtr wParam,   // WPARAM optional
    IntPtr lParam   // LPARAM optional
);
<DllImport("USER32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SendMessageW(
    hWnd As IntPtr,   ' HWND
    Msg As UInteger,   ' DWORD
    wParam As UIntPtr,   ' WPARAM optional
    lParam As IntPtr   ' LPARAM optional
) As IntPtr
End Function
' hWnd : HWND
' Msg : DWORD
' wParam : WPARAM optional
' lParam : LPARAM optional
Declare PtrSafe Function SendMessageW Lib "user32" ( _
    ByVal hWnd As LongPtr, _
    ByVal Msg As Long, _
    ByVal wParam As LongPtr, _
    ByVal lParam 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

SendMessageW = ctypes.windll.user32.SendMessageW
SendMessageW.restype = ctypes.c_ssize_t
SendMessageW.argtypes = [
    wintypes.HANDLE,  # hWnd : HWND
    wintypes.DWORD,  # Msg : DWORD
    ctypes.c_size_t,  # wParam : WPARAM optional
    ctypes.c_ssize_t,  # lParam : LPARAM optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
SendMessageW = Fiddle::Function.new(
  lib['SendMessageW'],
  [
    Fiddle::TYPE_VOIDP,  # hWnd : HWND
    -Fiddle::TYPE_INT,  # Msg : DWORD
    Fiddle::TYPE_UINTPTR_T,  # wParam : WPARAM optional
    Fiddle::TYPE_INTPTR_T,  # lParam : LPARAM optional
  ],
  Fiddle::TYPE_INTPTR_T)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "user32")]
extern "system" {
    fn SendMessageW(
        hWnd: *mut core::ffi::c_void,  // HWND
        Msg: u32,  // DWORD
        wParam: usize,  // WPARAM optional
        lParam: isize  // LPARAM optional
    ) -> isize;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("USER32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr SendMessageW(IntPtr hWnd, uint Msg, UIntPtr wParam, IntPtr lParam);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_SendMessageW' -Namespace Win32 -PassThru
# $api::SendMessageW(hWnd, Msg, wParam, lParam)
#uselib "USER32.dll"
#func global SendMessageW "SendMessageW" wptr, wptr, wptr, wptr
; SendMessageW hWnd, Msg, wParam, lParam   ; 戻り値は stat
; hWnd : HWND -> "wptr"
; Msg : DWORD -> "wptr"
; wParam : WPARAM optional -> "wptr"
; lParam : LPARAM optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global SendMessageW "SendMessageW" sptr, int, sptr, sptr
; res = SendMessageW(hWnd, Msg, wParam, lParam)
; hWnd : HWND -> "sptr"
; Msg : DWORD -> "int"
; wParam : WPARAM optional -> "sptr"
; lParam : LPARAM optional -> "sptr"
; LRESULT SendMessageW(HWND hWnd, DWORD Msg, WPARAM wParam, LPARAM lParam)
#uselib "USER32.dll"
#cfunc global SendMessageW "SendMessageW" intptr, int, intptr, intptr
; res = SendMessageW(hWnd, Msg, wParam, lParam)
; hWnd : HWND -> "intptr"
; Msg : DWORD -> "int"
; wParam : WPARAM optional -> "intptr"
; lParam : LPARAM optional -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSendMessageW = user32.NewProc("SendMessageW")
)

// hWnd (HWND), Msg (DWORD), wParam (WPARAM optional), lParam (LPARAM optional)
r1, _, err := procSendMessageW.Call(
	uintptr(hWnd),
	uintptr(Msg),
	uintptr(wParam),
	uintptr(lParam),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // LRESULT
function SendMessageW(
  hWnd: THandle;   // HWND
  Msg: DWORD;   // DWORD
  wParam: NativeUInt;   // WPARAM optional
  lParam: NativeInt   // LPARAM optional
): NativeInt; stdcall;
  external 'USER32.dll' name 'SendMessageW';
result := DllCall("USER32\SendMessageW"
    , "Ptr", hWnd   ; HWND
    , "UInt", Msg   ; DWORD
    , "UPtr", wParam   ; WPARAM optional
    , "Ptr", lParam   ; LPARAM optional
    , "Ptr")   ; return: LRESULT
●SendMessageW(hWnd, Msg, wParam, lParam) = DLL("USER32.dll", "int SendMessageW(void*, dword, int, int)")
# 呼び出し: SendMessageW(hWnd, Msg, wParam, lParam)
# hWnd : HWND -> "void*"
# Msg : DWORD -> "dword"
# wParam : WPARAM optional -> "int"
# lParam : LPARAM optional -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。