Win32 API 日本語リファレンス
ホーム › System.RemoteDesktop › WTSSendMessageW

WTSSendMessageW

関数
指定セッションにメッセージボックスを表示して応答を取得する。
DLLWTSAPI32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

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

BOOL WTSSendMessageW(
    HANDLE hServer,   // optional
    DWORD SessionId,
    LPWSTR pTitle,
    DWORD TitleLength,
    LPWSTR pMessage,
    DWORD MessageLength,
    MESSAGEBOX_STYLE Style,
    DWORD Timeout,
    MESSAGEBOX_RESULT* pResponse,
    BOOL bWait
);

パラメーター

名前型方向説明
hServerHANDLEoptionalRD セッションホストサーバーのハンドル。 WTSOpenServer 関数で開いたハンドルを指定するか、 アプリケーションが実行されている RD セッションホストサーバーを示す WTS_CURRENT_SERVER_HANDLE を指定します。
SessionIdDWORDin

Remote Desktop Services のセッション識別子。現在のセッションを示すには WTS_CURRENT_SESSION を指定します。指定した RD セッションホストサーバー上のすべてのセッションの識別子を取得するには、 WTSEnumerateSessions 関数を使用できます。

他のユーザーのセッションにメッセージを送信するには、Message 権限が必要です。詳細については、 Remote Desktop Services
Permissions
を参照してください。セッションの権限を変更するには、リモート デスクトップ サービスの構成管理ツールを使用します。

pTitleLPWSTRinメッセージボックスのタイトルバーに表示する、null で終わる文字列へのポインター。
TitleLengthDWORDinタイトルバーの文字列の長さ(バイト単位)。
pMessageLPWSTRin表示するメッセージを含む、null で終わる文字列へのポインター。
MessageLengthDWORDinメッセージ文字列の長さ(バイト単位)。
StyleMESSAGEBOX_STYLEinメッセージボックスの内容と動作。この値は通常 MB_OK です。値の完全な一覧については、MessageBox 関数の uType パラメーターを参照してください。
TimeoutDWORDinWTSSendMessage 関数がユーザーの応答を待機する時間(秒単位)。 タイムアウト間隔内にユーザーが応答しない場合、 pResponse パラメーターは IDTIMEOUT を返します。 Timeout パラメーターが 0 の場合、WTSSendMessage はユーザーが応答するまで無期限に待機します。
pResponseMESSAGEBOX_RESULT*out

ユーザーの応答を受け取る変数へのポインター。次のいずれかの値になります。

IDABORT (3)

Abort(中止)

IDCANCEL (2)

Cancel(キャンセル)

IDCONTINUE (11)

Continue(続行)

IDIGNORE (5)

Ignore(無視)

IDNO (7)

No(いいえ)

IDOK (1)

OK

IDRETRY (4)

Retry(再試行)

IDTRYAGAIN (10)

Try Again(やり直し)

IDYES (6)

Yes(はい)

IDASYNC (32001 (0x7D01))

bWait パラメーターが FALSE であったため、関数は応答を待たずに戻りました。

IDTIMEOUT (32000 (0x7D00))

bWait パラメーターが TRUE で、タイムアウト間隔が経過しました。

bWaitBOOLin

TRUE の場合、WTSSendMessage はユーザーが応答するか タイムアウト間隔が経過するまで戻りません。Timeout パラメーターが 0 の場合、 関数はユーザーが応答するまで戻りません。

FALSE の場合、関数は直ちに戻り、 pResponse パラメーターは IDASYNC を返します。ユーザーの応答を呼び出し元のプログラムに返す必要がない 単純な情報メッセージ(印刷ジョブの通知メッセージなど)には、この方法を使用します。

- pResponse.IDABORT (3)

Abort(中止)

- pResponse.IDASYNC (32001 (0x7D01))

bWait パラメーターが FALSE であったため、関数は応答を待たずに戻りました。

- pResponse.IDCANCEL (2)

Cancel(キャンセル)

- pResponse.IDCONTINUE (11)

Continue(続行)

- pResponse.IDIGNORE (5)

Ignore(無視)

- pResponse.IDNO (7)

No(いいえ)

- pResponse.IDOK (1)

OK

- pResponse.IDRETRY (4)

Retry(再試行)

- pResponse.IDTIMEOUT (32000 (0x7D00))

bWait パラメーターが TRUE で、タイムアウト間隔が経過しました。

- pResponse.IDTRYAGAIN (10)

Try Again(やり直し)

- pResponse.IDYES (6)

Yes(はい)

戻り値の型: BOOL

公式ドキュメント

指定した Remote Desktop Services セッションのクライアントデスクトップにメッセージボックスを表示します。(Unicode)

戻り値

関数が成功した場合、戻り値は 0 以外の値です。

関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、 GetLastError を呼び出します。

解説(Remarks)

メモ

wtsapi32.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版と Unicode 版を自動的に選択するエイリアスとして WTSSendMessage を定義します。エンコーディング中立のエイリアスの使用と、エンコーディング中立でないコードを混在させると、不一致が生じ、コンパイルエラーや実行時エラーの原因になることがあります。詳細については、関数プロトタイプの規則を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

BOOL WTSSendMessageW(
    HANDLE hServer,   // optional
    DWORD SessionId,
    LPWSTR pTitle,
    DWORD TitleLength,
    LPWSTR pMessage,
    DWORD MessageLength,
    MESSAGEBOX_STYLE Style,
    DWORD Timeout,
    MESSAGEBOX_RESULT* pResponse,
    BOOL bWait
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WTSAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool WTSSendMessageW(
    IntPtr hServer,   // HANDLE optional
    uint SessionId,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] string pTitle,   // LPWSTR
    uint TitleLength,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] string pMessage,   // LPWSTR
    uint MessageLength,   // DWORD
    uint Style,   // MESSAGEBOX_STYLE
    uint Timeout,   // DWORD
    out int pResponse,   // MESSAGEBOX_RESULT* out
    bool bWait   // BOOL
);
<DllImport("WTSAPI32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WTSSendMessageW(
    hServer As IntPtr,   ' HANDLE optional
    SessionId As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> pTitle As String,   ' LPWSTR
    TitleLength As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> pMessage As String,   ' LPWSTR
    MessageLength As UInteger,   ' DWORD
    Style As UInteger,   ' MESSAGEBOX_STYLE
    Timeout As UInteger,   ' DWORD
    <Out> ByRef pResponse As Integer,   ' MESSAGEBOX_RESULT* out
    bWait As Boolean   ' BOOL
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hServer : HANDLE optional
' SessionId : DWORD
' pTitle : LPWSTR
' TitleLength : DWORD
' pMessage : LPWSTR
' MessageLength : DWORD
' Style : MESSAGEBOX_STYLE
' Timeout : DWORD
' pResponse : MESSAGEBOX_RESULT* out
' bWait : BOOL
Declare PtrSafe Function WTSSendMessageW Lib "wtsapi32" ( _
    ByVal hServer As LongPtr, _
    ByVal SessionId As Long, _
    ByVal pTitle As LongPtr, _
    ByVal TitleLength As Long, _
    ByVal pMessage As LongPtr, _
    ByVal MessageLength As Long, _
    ByVal Style As Long, _
    ByVal Timeout As Long, _
    ByRef pResponse As Long, _
    ByVal bWait As Long) As Long
' 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

WTSSendMessageW = ctypes.windll.wtsapi32.WTSSendMessageW
WTSSendMessageW.restype = wintypes.BOOL
WTSSendMessageW.argtypes = [
    wintypes.HANDLE,  # hServer : HANDLE optional
    wintypes.DWORD,  # SessionId : DWORD
    wintypes.LPCWSTR,  # pTitle : LPWSTR
    wintypes.DWORD,  # TitleLength : DWORD
    wintypes.LPCWSTR,  # pMessage : LPWSTR
    wintypes.DWORD,  # MessageLength : DWORD
    wintypes.DWORD,  # Style : MESSAGEBOX_STYLE
    wintypes.DWORD,  # Timeout : DWORD
    ctypes.c_void_p,  # pResponse : MESSAGEBOX_RESULT* out
    wintypes.BOOL,  # bWait : BOOL
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WTSAPI32.dll')
WTSSendMessageW = Fiddle::Function.new(
  lib['WTSSendMessageW'],
  [
    Fiddle::TYPE_VOIDP,  # hServer : HANDLE optional
    -Fiddle::TYPE_INT,  # SessionId : DWORD
    Fiddle::TYPE_VOIDP,  # pTitle : LPWSTR
    -Fiddle::TYPE_INT,  # TitleLength : DWORD
    Fiddle::TYPE_VOIDP,  # pMessage : LPWSTR
    -Fiddle::TYPE_INT,  # MessageLength : DWORD
    -Fiddle::TYPE_INT,  # Style : MESSAGEBOX_STYLE
    -Fiddle::TYPE_INT,  # Timeout : DWORD
    Fiddle::TYPE_VOIDP,  # pResponse : MESSAGEBOX_RESULT* out
    Fiddle::TYPE_INT,  # bWait : BOOL
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "wtsapi32")]
extern "system" {
    fn WTSSendMessageW(
        hServer: *mut core::ffi::c_void,  // HANDLE optional
        SessionId: u32,  // DWORD
        pTitle: *mut u16,  // LPWSTR
        TitleLength: u32,  // DWORD
        pMessage: *mut u16,  // LPWSTR
        MessageLength: u32,  // DWORD
        Style: u32,  // MESSAGEBOX_STYLE
        Timeout: u32,  // DWORD
        pResponse: *mut i32,  // MESSAGEBOX_RESULT* out
        bWait: i32  // BOOL
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WTSAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool WTSSendMessageW(IntPtr hServer, uint SessionId, [MarshalAs(UnmanagedType.LPWStr)] string pTitle, uint TitleLength, [MarshalAs(UnmanagedType.LPWStr)] string pMessage, uint MessageLength, uint Style, uint Timeout, out int pResponse, bool bWait);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WTSAPI32_WTSSendMessageW' -Namespace Win32 -PassThru
# $api::WTSSendMessageW(hServer, SessionId, pTitle, TitleLength, pMessage, MessageLength, Style, Timeout, pResponse, bWait)
#uselib "WTSAPI32.dll"
#func global WTSSendMessageW "WTSSendMessageW" wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr
; WTSSendMessageW hServer, SessionId, pTitle, TitleLength, pMessage, MessageLength, Style, Timeout, varptr(pResponse), bWait   ; 戻り値は stat
; hServer : HANDLE optional -> "wptr"
; SessionId : DWORD -> "wptr"
; pTitle : LPWSTR -> "wptr"
; TitleLength : DWORD -> "wptr"
; pMessage : LPWSTR -> "wptr"
; MessageLength : DWORD -> "wptr"
; Style : MESSAGEBOX_STYLE -> "wptr"
; Timeout : DWORD -> "wptr"
; pResponse : MESSAGEBOX_RESULT* out -> "wptr"
; bWait : BOOL -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "WTSAPI32.dll"
#cfunc global WTSSendMessageW "WTSSendMessageW" sptr, int, wstr, int, wstr, int, int, int, var, int
; res = WTSSendMessageW(hServer, SessionId, pTitle, TitleLength, pMessage, MessageLength, Style, Timeout, pResponse, bWait)
; hServer : HANDLE optional -> "sptr"
; SessionId : DWORD -> "int"
; pTitle : LPWSTR -> "wstr"
; TitleLength : DWORD -> "int"
; pMessage : LPWSTR -> "wstr"
; MessageLength : DWORD -> "int"
; Style : MESSAGEBOX_STYLE -> "int"
; Timeout : DWORD -> "int"
; pResponse : MESSAGEBOX_RESULT* out -> "var"
; bWait : BOOL -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL WTSSendMessageW(HANDLE hServer, DWORD SessionId, LPWSTR pTitle, DWORD TitleLength, LPWSTR pMessage, DWORD MessageLength, MESSAGEBOX_STYLE Style, DWORD Timeout, MESSAGEBOX_RESULT* pResponse, BOOL bWait)
#uselib "WTSAPI32.dll"
#cfunc global WTSSendMessageW "WTSSendMessageW" intptr, int, wstr, int, wstr, int, int, int, var, int
; res = WTSSendMessageW(hServer, SessionId, pTitle, TitleLength, pMessage, MessageLength, Style, Timeout, pResponse, bWait)
; hServer : HANDLE optional -> "intptr"
; SessionId : DWORD -> "int"
; pTitle : LPWSTR -> "wstr"
; TitleLength : DWORD -> "int"
; pMessage : LPWSTR -> "wstr"
; MessageLength : DWORD -> "int"
; Style : MESSAGEBOX_STYLE -> "int"
; Timeout : DWORD -> "int"
; pResponse : MESSAGEBOX_RESULT* out -> "var"
; bWait : BOOL -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	wtsapi32 = windows.NewLazySystemDLL("WTSAPI32.dll")
	procWTSSendMessageW = wtsapi32.NewProc("WTSSendMessageW")
)

// hServer (HANDLE optional), SessionId (DWORD), pTitle (LPWSTR), TitleLength (DWORD), pMessage (LPWSTR), MessageLength (DWORD), Style (MESSAGEBOX_STYLE), Timeout (DWORD), pResponse (MESSAGEBOX_RESULT* out), bWait (BOOL)
r1, _, err := procWTSSendMessageW.Call(
	uintptr(hServer),
	uintptr(SessionId),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pTitle))),
	uintptr(TitleLength),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pMessage))),
	uintptr(MessageLength),
	uintptr(Style),
	uintptr(Timeout),
	uintptr(pResponse),
	uintptr(bWait),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function WTSSendMessageW(
  hServer: THandle;   // HANDLE optional
  SessionId: DWORD;   // DWORD
  pTitle: PWideChar;   // LPWSTR
  TitleLength: DWORD;   // DWORD
  pMessage: PWideChar;   // LPWSTR
  MessageLength: DWORD;   // DWORD
  Style: DWORD;   // MESSAGEBOX_STYLE
  Timeout: DWORD;   // DWORD
  pResponse: Pointer;   // MESSAGEBOX_RESULT* out
  bWait: BOOL   // BOOL
): BOOL; stdcall;
  external 'WTSAPI32.dll' name 'WTSSendMessageW';
result := DllCall("WTSAPI32\WTSSendMessageW"
    , "Ptr", hServer   ; HANDLE optional
    , "UInt", SessionId   ; DWORD
    , "WStr", pTitle   ; LPWSTR
    , "UInt", TitleLength   ; DWORD
    , "WStr", pMessage   ; LPWSTR
    , "UInt", MessageLength   ; DWORD
    , "UInt", Style   ; MESSAGEBOX_STYLE
    , "UInt", Timeout   ; DWORD
    , "Ptr", pResponse   ; MESSAGEBOX_RESULT* out
    , "Int", bWait   ; BOOL
    , "Int")   ; return: BOOL
●WTSSendMessageW(hServer, SessionId, pTitle, TitleLength, pMessage, MessageLength, Style, Timeout, pResponse, bWait) = DLL("WTSAPI32.dll", "bool WTSSendMessageW(void*, dword, char*, dword, char*, dword, dword, dword, void*, bool)")
# 呼び出し: WTSSendMessageW(hServer, SessionId, pTitle, TitleLength, pMessage, MessageLength, Style, Timeout, pResponse, bWait)
# hServer : HANDLE optional -> "void*"
# SessionId : DWORD -> "dword"
# pTitle : LPWSTR -> "char*"
# TitleLength : DWORD -> "dword"
# pMessage : LPWSTR -> "char*"
# MessageLength : DWORD -> "dword"
# Style : MESSAGEBOX_STYLE -> "dword"
# Timeout : DWORD -> "dword"
# pResponse : MESSAGEBOX_RESULT* out -> "void*"
# bWait : BOOL -> "bool"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "wtsapi32" fn WTSSendMessageW(
    hServer: ?*anyopaque, // HANDLE optional
    SessionId: u32, // DWORD
    pTitle: [*c]const u16, // LPWSTR
    TitleLength: u32, // DWORD
    pMessage: [*c]const u16, // LPWSTR
    MessageLength: u32, // DWORD
    Style: u32, // MESSAGEBOX_STYLE
    Timeout: u32, // DWORD
    pResponse: [*c]i32, // MESSAGEBOX_RESULT* out
    bWait: i32 // BOOL
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc WTSSendMessageW(
    hServer: pointer,  # HANDLE optional
    SessionId: uint32,  # DWORD
    pTitle: WideCString,  # LPWSTR
    TitleLength: uint32,  # DWORD
    pMessage: WideCString,  # LPWSTR
    MessageLength: uint32,  # DWORD
    Style: uint32,  # MESSAGEBOX_STYLE
    Timeout: uint32,  # DWORD
    pResponse: ptr int32,  # MESSAGEBOX_RESULT* out
    bWait: int32  # BOOL
): int32 {.importc: "WTSSendMessageW", stdcall, dynlib: "WTSAPI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "wtsapi32");
extern(Windows)
int WTSSendMessageW(
    void* hServer,   // HANDLE optional
    uint SessionId,   // DWORD
    const(wchar)* pTitle,   // LPWSTR
    uint TitleLength,   // DWORD
    const(wchar)* pMessage,   // LPWSTR
    uint MessageLength,   // DWORD
    uint Style,   // MESSAGEBOX_STYLE
    uint Timeout,   // DWORD
    int* pResponse,   // MESSAGEBOX_RESULT* out
    int bWait   // BOOL
);
ccall((:WTSSendMessageW, "WTSAPI32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, UInt32, Cwstring, UInt32, Cwstring, UInt32, UInt32, UInt32, Ptr{Int32}, Int32),
      hServer, SessionId, pTitle, TitleLength, pMessage, MessageLength, Style, Timeout, pResponse, bWait)
# hServer : HANDLE optional -> Ptr{Cvoid}
# SessionId : DWORD -> UInt32
# pTitle : LPWSTR -> Cwstring
# TitleLength : DWORD -> UInt32
# pMessage : LPWSTR -> Cwstring
# MessageLength : DWORD -> UInt32
# Style : MESSAGEBOX_STYLE -> UInt32
# Timeout : DWORD -> UInt32
# pResponse : MESSAGEBOX_RESULT* out -> Ptr{Int32}
# bWait : BOOL -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t WTSSendMessageW(
    void* hServer,
    uint32_t SessionId,
    const uint16_t* pTitle,
    uint32_t TitleLength,
    const uint16_t* pMessage,
    uint32_t MessageLength,
    uint32_t Style,
    uint32_t Timeout,
    int32_t* pResponse,
    int32_t bWait);
]]
local wtsapi32 = ffi.load("wtsapi32")
-- wtsapi32.WTSSendMessageW(hServer, SessionId, pTitle, TitleLength, pMessage, MessageLength, Style, Timeout, pResponse, bWait)
-- hServer : HANDLE optional
-- SessionId : DWORD
-- pTitle : LPWSTR
-- TitleLength : DWORD
-- pMessage : LPWSTR
-- MessageLength : DWORD
-- Style : MESSAGEBOX_STYLE
-- Timeout : DWORD
-- pResponse : MESSAGEBOX_RESULT* out
-- bWait : BOOL
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('WTSAPI32.dll');
const WTSSendMessageW = lib.func('__stdcall', 'WTSSendMessageW', 'int32_t', ['void *', 'uint32_t', 'str16', 'uint32_t', 'str16', 'uint32_t', 'uint32_t', 'uint32_t', 'int32_t *', 'int32_t']);
// WTSSendMessageW(hServer, SessionId, pTitle, TitleLength, pMessage, MessageLength, Style, Timeout, pResponse, bWait)
// hServer : HANDLE optional -> 'void *'
// SessionId : DWORD -> 'uint32_t'
// pTitle : LPWSTR -> 'str16'
// TitleLength : DWORD -> 'uint32_t'
// pMessage : LPWSTR -> 'str16'
// MessageLength : DWORD -> 'uint32_t'
// Style : MESSAGEBOX_STYLE -> 'uint32_t'
// Timeout : DWORD -> 'uint32_t'
// pResponse : MESSAGEBOX_RESULT* out -> 'int32_t *'
// bWait : BOOL -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WTSAPI32.dll", {
  WTSSendMessageW: { parameters: ["pointer", "u32", "buffer", "u32", "buffer", "u32", "u32", "u32", "pointer", "i32"], result: "i32" },
});
// lib.symbols.WTSSendMessageW(hServer, SessionId, pTitle, TitleLength, pMessage, MessageLength, Style, Timeout, pResponse, bWait)
// hServer : HANDLE optional -> "pointer"
// SessionId : DWORD -> "u32"
// pTitle : LPWSTR -> "buffer"
// TitleLength : DWORD -> "u32"
// pMessage : LPWSTR -> "buffer"
// MessageLength : DWORD -> "u32"
// Style : MESSAGEBOX_STYLE -> "u32"
// Timeout : DWORD -> "u32"
// pResponse : MESSAGEBOX_RESULT* out -> "pointer"
// bWait : BOOL -> "i32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t WTSSendMessageW(
    void* hServer,
    uint32_t SessionId,
    const uint16_t* pTitle,
    uint32_t TitleLength,
    const uint16_t* pMessage,
    uint32_t MessageLength,
    uint32_t Style,
    uint32_t Timeout,
    int32_t* pResponse,
    int32_t bWait);
C, "WTSAPI32.dll");
// $ffi->WTSSendMessageW(hServer, SessionId, pTitle, TitleLength, pMessage, MessageLength, Style, Timeout, pResponse, bWait);
// hServer : HANDLE optional
// SessionId : DWORD
// pTitle : LPWSTR
// TitleLength : DWORD
// pMessage : LPWSTR
// MessageLength : DWORD
// Style : MESSAGEBOX_STYLE
// Timeout : DWORD
// pResponse : MESSAGEBOX_RESULT* out
// bWait : BOOL
// 構造体/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 Wtsapi32 extends StdCallLibrary {
    Wtsapi32 INSTANCE = Native.load("wtsapi32", Wtsapi32.class, W32APIOptions.UNICODE_OPTIONS);
    boolean WTSSendMessageW(
        Pointer hServer,   // HANDLE optional
        int SessionId,   // DWORD
        WString pTitle,   // LPWSTR
        int TitleLength,   // DWORD
        WString pMessage,   // LPWSTR
        int MessageLength,   // DWORD
        int Style,   // MESSAGEBOX_STYLE
        int Timeout,   // DWORD
        IntByReference pResponse,   // MESSAGEBOX_RESULT* out
        boolean bWait   // BOOL
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("wtsapi32")]
lib LibWTSAPI32
  fun WTSSendMessageW = WTSSendMessageW(
    hServer : Void*,   # HANDLE optional
    SessionId : UInt32,   # DWORD
    pTitle : UInt16*,   # LPWSTR
    TitleLength : UInt32,   # DWORD
    pMessage : UInt16*,   # LPWSTR
    MessageLength : UInt32,   # DWORD
    Style : UInt32,   # MESSAGEBOX_STYLE
    Timeout : UInt32,   # DWORD
    pResponse : Int32*,   # MESSAGEBOX_RESULT* out
    bWait : Int32   # BOOL
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef WTSSendMessageWNative = Int32 Function(Pointer<Void>, Uint32, Pointer<Utf16>, Uint32, Pointer<Utf16>, Uint32, Uint32, Uint32, Pointer<Int32>, Int32);
typedef WTSSendMessageWDart = int Function(Pointer<Void>, int, Pointer<Utf16>, int, Pointer<Utf16>, int, int, int, Pointer<Int32>, int);
final WTSSendMessageW = DynamicLibrary.open('WTSAPI32.dll')
    .lookupFunction<WTSSendMessageWNative, WTSSendMessageWDart>('WTSSendMessageW');
// hServer : HANDLE optional -> Pointer<Void>
// SessionId : DWORD -> Uint32
// pTitle : LPWSTR -> Pointer<Utf16>
// TitleLength : DWORD -> Uint32
// pMessage : LPWSTR -> Pointer<Utf16>
// MessageLength : DWORD -> Uint32
// Style : MESSAGEBOX_STYLE -> Uint32
// Timeout : DWORD -> Uint32
// pResponse : MESSAGEBOX_RESULT* out -> Pointer<Int32>
// bWait : BOOL -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WTSSendMessageW(
  hServer: THandle;   // HANDLE optional
  SessionId: DWORD;   // DWORD
  pTitle: PWideChar;   // LPWSTR
  TitleLength: DWORD;   // DWORD
  pMessage: PWideChar;   // LPWSTR
  MessageLength: DWORD;   // DWORD
  Style: DWORD;   // MESSAGEBOX_STYLE
  Timeout: DWORD;   // DWORD
  pResponse: Pointer;   // MESSAGEBOX_RESULT* out
  bWait: BOOL   // BOOL
): BOOL; stdcall;
  external 'WTSAPI32.dll' name 'WTSSendMessageW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WTSSendMessageW"
  c_WTSSendMessageW :: Ptr () -> Word32 -> CWString -> Word32 -> CWString -> Word32 -> Word32 -> Word32 -> Ptr Int32 -> CInt -> IO CInt
-- hServer : HANDLE optional -> Ptr ()
-- SessionId : DWORD -> Word32
-- pTitle : LPWSTR -> CWString
-- TitleLength : DWORD -> Word32
-- pMessage : LPWSTR -> CWString
-- MessageLength : DWORD -> Word32
-- Style : MESSAGEBOX_STYLE -> Word32
-- Timeout : DWORD -> Word32
-- pResponse : MESSAGEBOX_RESULT* out -> Ptr Int32
-- bWait : BOOL -> CInt
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let wtssendmessagew =
  foreign "WTSSendMessageW"
    ((ptr void) @-> uint32_t @-> (ptr uint16_t) @-> uint32_t @-> (ptr uint16_t) @-> uint32_t @-> uint32_t @-> uint32_t @-> (ptr int32_t) @-> int32_t @-> returning int32_t)
(* hServer : HANDLE optional -> (ptr void) *)
(* SessionId : DWORD -> uint32_t *)
(* pTitle : LPWSTR -> (ptr uint16_t) *)
(* TitleLength : DWORD -> uint32_t *)
(* pMessage : LPWSTR -> (ptr uint16_t) *)
(* MessageLength : DWORD -> uint32_t *)
(* Style : MESSAGEBOX_STYLE -> uint32_t *)
(* Timeout : DWORD -> uint32_t *)
(* pResponse : MESSAGEBOX_RESULT* out -> (ptr int32_t) *)
(* bWait : BOOL -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library wtsapi32 (t "WTSAPI32.dll"))
(cffi:use-foreign-library wtsapi32)

(cffi:defcfun ("WTSSendMessageW" wtssend-message-w :convention :stdcall) :int32
  (h-server :pointer)   ; HANDLE optional
  (session-id :uint32)   ; DWORD
  (p-title (:string :encoding :utf-16le))   ; LPWSTR
  (title-length :uint32)   ; DWORD
  (p-message (:string :encoding :utf-16le))   ; LPWSTR
  (message-length :uint32)   ; DWORD
  (style :uint32)   ; MESSAGEBOX_STYLE
  (timeout :uint32)   ; DWORD
  (p-response :pointer)   ; MESSAGEBOX_RESULT* out
  (b-wait :int32))   ; BOOL
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WTSSendMessageW = Win32::API::More->new('WTSAPI32',
    'BOOL WTSSendMessageW(HANDLE hServer, DWORD SessionId, LPCWSTR pTitle, DWORD TitleLength, LPCWSTR pMessage, DWORD MessageLength, DWORD Style, DWORD Timeout, LPVOID pResponse, BOOL bWait)');
# my $ret = $WTSSendMessageW->Call($hServer, $SessionId, $pTitle, $TitleLength, $pMessage, $MessageLength, $Style, $Timeout, $pResponse, $bWait);
# hServer : HANDLE optional -> HANDLE
# SessionId : DWORD -> DWORD
# pTitle : LPWSTR -> LPCWSTR
# TitleLength : DWORD -> DWORD
# pMessage : LPWSTR -> LPCWSTR
# MessageLength : DWORD -> DWORD
# Style : MESSAGEBOX_STYLE -> DWORD
# Timeout : DWORD -> DWORD
# pResponse : MESSAGEBOX_RESULT* out -> LPVOID
# bWait : BOOL -> BOOL
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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