Win32 API 日本語リファレンス
ホームNetworking.WinHttp › WinHttpWebSocketClose

WinHttpWebSocketClose

関数
WebSocket接続を閉じる。
DLLWINHTTP.dll呼出規約winapi対応OSwindows8.0

シグネチャ

// WINHTTP.dll
#include <windows.h>

DWORD WinHttpWebSocketClose(
    void* hWebSocket,
    WORD usStatus,
    void* pvReason,   // optional
    DWORD dwReasonLength
);

パラメーター

名前方向説明
hWebSocketvoid*in

WebSocket へのハンドルです。

注意 WinHttpWebSocketClose はこのハンドルを閉じません。ハンドルを閉じるには、hWebSocket が不要になった時点で WinHttpCloseHandle を呼び出してください。

usStatusWORDinクローズ ステータス コードです。指定可能な値については WINHTTP_WEB_SOCKET_CLOSE_STATUS を参照してください。
pvReasonvoid*inoptionalクローズの詳細な理由です。
dwReasonLengthDWORDin

pvReason の長さ (バイト単位) です。

pvReason が NULL の場合、この値は 0 でなければなりません。この値は 0 から 123 の範囲内である必要があります。

戻り値の型: DWORD

公式ドキュメント

WebSocket 接続を閉じます。

戻り値

型: DWORD

次の例外を除き、すべてのエラー コードは基盤となる TCP 接続が中断されたことを示します。

説明
ERROR_INVALID_OPERATION
クローズまたは送信が保留中です。
ERROR_INVALID_PARAMETER
パラメーターが無効です。
ERROR_INVALID_SERVER_RESPONSE
サーバーから無効なデータを受信しました。

解説(Remarks)

WinHttpWebSocketClose は WebSocket 接続を完全に閉じます。受信チャネルを開いたまま送信チャネルのみを閉じるには、WinHttpWebSocketShutdown を使用してください。

通常の受信操作中にクローズ フレームを受信する場合があります。この場合、WinHttpWebSocketClose もクローズ フレームを送信します。

クローズ タイマーはプロパティ WINHTTP_OPTION_WEB_SOCKET_CLOSE_TIMEOUT で設定できます。 既定値は 10 秒です。

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

各言語での呼び出し定義

// WINHTTP.dll
#include <windows.h>

DWORD WinHttpWebSocketClose(
    void* hWebSocket,
    WORD usStatus,
    void* pvReason,   // optional
    DWORD dwReasonLength
);
[DllImport("WINHTTP.dll", ExactSpelling = true)]
static extern uint WinHttpWebSocketClose(
    IntPtr hWebSocket,   // void*
    ushort usStatus,   // WORD
    IntPtr pvReason,   // void* optional
    uint dwReasonLength   // DWORD
);
<DllImport("WINHTTP.dll", ExactSpelling:=True)>
Public Shared Function WinHttpWebSocketClose(
    hWebSocket As IntPtr,   ' void*
    usStatus As UShort,   ' WORD
    pvReason As IntPtr,   ' void* optional
    dwReasonLength As UInteger   ' DWORD
) As UInteger
End Function
' hWebSocket : void*
' usStatus : WORD
' pvReason : void* optional
' dwReasonLength : DWORD
Declare PtrSafe Function WinHttpWebSocketClose Lib "winhttp" ( _
    ByVal hWebSocket As LongPtr, _
    ByVal usStatus As Integer, _
    ByVal pvReason As LongPtr, _
    ByVal dwReasonLength As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WinHttpWebSocketClose = ctypes.windll.winhttp.WinHttpWebSocketClose
WinHttpWebSocketClose.restype = wintypes.DWORD
WinHttpWebSocketClose.argtypes = [
    ctypes.POINTER(None),  # hWebSocket : void*
    ctypes.c_ushort,  # usStatus : WORD
    ctypes.POINTER(None),  # pvReason : void* optional
    wintypes.DWORD,  # dwReasonLength : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WINHTTP.dll')
WinHttpWebSocketClose = Fiddle::Function.new(
  lib['WinHttpWebSocketClose'],
  [
    Fiddle::TYPE_VOIDP,  # hWebSocket : void*
    -Fiddle::TYPE_SHORT,  # usStatus : WORD
    Fiddle::TYPE_VOIDP,  # pvReason : void* optional
    -Fiddle::TYPE_INT,  # dwReasonLength : DWORD
  ],
  -Fiddle::TYPE_INT)
#[link(name = "winhttp")]
extern "system" {
    fn WinHttpWebSocketClose(
        hWebSocket: *mut (),  // void*
        usStatus: u16,  // WORD
        pvReason: *mut (),  // void* optional
        dwReasonLength: u32  // DWORD
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("WINHTTP.dll")]
public static extern uint WinHttpWebSocketClose(IntPtr hWebSocket, ushort usStatus, IntPtr pvReason, uint dwReasonLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WINHTTP_WinHttpWebSocketClose' -Namespace Win32 -PassThru
# $api::WinHttpWebSocketClose(hWebSocket, usStatus, pvReason, dwReasonLength)
#uselib "WINHTTP.dll"
#func global WinHttpWebSocketClose "WinHttpWebSocketClose" sptr, sptr, sptr, sptr
; WinHttpWebSocketClose hWebSocket, usStatus, pvReason, dwReasonLength   ; 戻り値は stat
; hWebSocket : void* -> "sptr"
; usStatus : WORD -> "sptr"
; pvReason : void* optional -> "sptr"
; dwReasonLength : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "WINHTTP.dll"
#cfunc global WinHttpWebSocketClose "WinHttpWebSocketClose" sptr, int, sptr, int
; res = WinHttpWebSocketClose(hWebSocket, usStatus, pvReason, dwReasonLength)
; hWebSocket : void* -> "sptr"
; usStatus : WORD -> "int"
; pvReason : void* optional -> "sptr"
; dwReasonLength : DWORD -> "int"
; DWORD WinHttpWebSocketClose(void* hWebSocket, WORD usStatus, void* pvReason, DWORD dwReasonLength)
#uselib "WINHTTP.dll"
#cfunc global WinHttpWebSocketClose "WinHttpWebSocketClose" intptr, int, intptr, int
; res = WinHttpWebSocketClose(hWebSocket, usStatus, pvReason, dwReasonLength)
; hWebSocket : void* -> "intptr"
; usStatus : WORD -> "int"
; pvReason : void* optional -> "intptr"
; dwReasonLength : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	winhttp = windows.NewLazySystemDLL("WINHTTP.dll")
	procWinHttpWebSocketClose = winhttp.NewProc("WinHttpWebSocketClose")
)

// hWebSocket (void*), usStatus (WORD), pvReason (void* optional), dwReasonLength (DWORD)
r1, _, err := procWinHttpWebSocketClose.Call(
	uintptr(hWebSocket),
	uintptr(usStatus),
	uintptr(pvReason),
	uintptr(dwReasonLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function WinHttpWebSocketClose(
  hWebSocket: Pointer;   // void*
  usStatus: Word;   // WORD
  pvReason: Pointer;   // void* optional
  dwReasonLength: DWORD   // DWORD
): DWORD; stdcall;
  external 'WINHTTP.dll' name 'WinHttpWebSocketClose';
result := DllCall("WINHTTP\WinHttpWebSocketClose"
    , "Ptr", hWebSocket   ; void*
    , "UShort", usStatus   ; WORD
    , "Ptr", pvReason   ; void* optional
    , "UInt", dwReasonLength   ; DWORD
    , "UInt")   ; return: DWORD
●WinHttpWebSocketClose(hWebSocket, usStatus, pvReason, dwReasonLength) = DLL("WINHTTP.dll", "dword WinHttpWebSocketClose(void*, int, void*, dword)")
# 呼び出し: WinHttpWebSocketClose(hWebSocket, usStatus, pvReason, dwReasonLength)
# hWebSocket : void* -> "void*"
# usStatus : WORD -> "int"
# pvReason : void* optional -> "void*"
# dwReasonLength : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "winhttp" fn WinHttpWebSocketClose(
    hWebSocket: ?*anyopaque, // void*
    usStatus: u16, // WORD
    pvReason: ?*anyopaque, // void* optional
    dwReasonLength: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
proc WinHttpWebSocketClose(
    hWebSocket: pointer,  # void*
    usStatus: uint16,  # WORD
    pvReason: pointer,  # void* optional
    dwReasonLength: uint32  # DWORD
): uint32 {.importc: "WinHttpWebSocketClose", stdcall, dynlib: "WINHTTP.dll".}
pragma(lib, "winhttp");
extern(Windows)
uint WinHttpWebSocketClose(
    void* hWebSocket,   // void*
    ushort usStatus,   // WORD
    void* pvReason,   // void* optional
    uint dwReasonLength   // DWORD
);
ccall((:WinHttpWebSocketClose, "WINHTTP.dll"), stdcall, UInt32,
      (Ptr{Cvoid}, UInt16, Ptr{Cvoid}, UInt32),
      hWebSocket, usStatus, pvReason, dwReasonLength)
# hWebSocket : void* -> Ptr{Cvoid}
# usStatus : WORD -> UInt16
# pvReason : void* optional -> Ptr{Cvoid}
# dwReasonLength : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t WinHttpWebSocketClose(
    void* hWebSocket,
    uint16_t usStatus,
    void* pvReason,
    uint32_t dwReasonLength);
]]
local winhttp = ffi.load("winhttp")
-- winhttp.WinHttpWebSocketClose(hWebSocket, usStatus, pvReason, dwReasonLength)
-- hWebSocket : void*
-- usStatus : WORD
-- pvReason : void* optional
-- dwReasonLength : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('WINHTTP.dll');
const WinHttpWebSocketClose = lib.func('__stdcall', 'WinHttpWebSocketClose', 'uint32_t', ['void *', 'uint16_t', 'void *', 'uint32_t']);
// WinHttpWebSocketClose(hWebSocket, usStatus, pvReason, dwReasonLength)
// hWebSocket : void* -> 'void *'
// usStatus : WORD -> 'uint16_t'
// pvReason : void* optional -> 'void *'
// dwReasonLength : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WINHTTP.dll", {
  WinHttpWebSocketClose: { parameters: ["pointer", "u16", "pointer", "u32"], result: "u32" },
});
// lib.symbols.WinHttpWebSocketClose(hWebSocket, usStatus, pvReason, dwReasonLength)
// hWebSocket : void* -> "pointer"
// usStatus : WORD -> "u16"
// pvReason : void* optional -> "pointer"
// dwReasonLength : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t WinHttpWebSocketClose(
    void* hWebSocket,
    uint16_t usStatus,
    void* pvReason,
    uint32_t dwReasonLength);
C, "WINHTTP.dll");
// $ffi->WinHttpWebSocketClose(hWebSocket, usStatus, pvReason, dwReasonLength);
// hWebSocket : void*
// usStatus : WORD
// pvReason : void* optional
// dwReasonLength : DWORD
// 構造体/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 Winhttp extends StdCallLibrary {
    Winhttp INSTANCE = Native.load("winhttp", Winhttp.class);
    int WinHttpWebSocketClose(
        Pointer hWebSocket,   // void*
        short usStatus,   // WORD
        Pointer pvReason,   // void* optional
        int dwReasonLength   // DWORD
    );
}
@[Link("winhttp")]
lib LibWINHTTP
  fun WinHttpWebSocketClose = WinHttpWebSocketClose(
    hWebSocket : Void*,   # void*
    usStatus : UInt16,   # WORD
    pvReason : Void*,   # void* optional
    dwReasonLength : UInt32   # DWORD
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef WinHttpWebSocketCloseNative = Uint32 Function(Pointer<Void>, Uint16, Pointer<Void>, Uint32);
typedef WinHttpWebSocketCloseDart = int Function(Pointer<Void>, int, Pointer<Void>, int);
final WinHttpWebSocketClose = DynamicLibrary.open('WINHTTP.dll')
    .lookupFunction<WinHttpWebSocketCloseNative, WinHttpWebSocketCloseDart>('WinHttpWebSocketClose');
// hWebSocket : void* -> Pointer<Void>
// usStatus : WORD -> Uint16
// pvReason : void* optional -> Pointer<Void>
// dwReasonLength : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WinHttpWebSocketClose(
  hWebSocket: Pointer;   // void*
  usStatus: Word;   // WORD
  pvReason: Pointer;   // void* optional
  dwReasonLength: DWORD   // DWORD
): DWORD; stdcall;
  external 'WINHTTP.dll' name 'WinHttpWebSocketClose';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WinHttpWebSocketClose"
  c_WinHttpWebSocketClose :: Ptr () -> Word16 -> Ptr () -> Word32 -> IO Word32
-- hWebSocket : void* -> Ptr ()
-- usStatus : WORD -> Word16
-- pvReason : void* optional -> Ptr ()
-- dwReasonLength : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let winhttpwebsocketclose =
  foreign "WinHttpWebSocketClose"
    ((ptr void) @-> uint16_t @-> (ptr void) @-> uint32_t @-> returning uint32_t)
(* hWebSocket : void* -> (ptr void) *)
(* usStatus : WORD -> uint16_t *)
(* pvReason : void* optional -> (ptr void) *)
(* dwReasonLength : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library winhttp (t "WINHTTP.dll"))
(cffi:use-foreign-library winhttp)

(cffi:defcfun ("WinHttpWebSocketClose" win-http-web-socket-close :convention :stdcall) :uint32
  (h-web-socket :pointer)   ; void*
  (us-status :uint16)   ; WORD
  (pv-reason :pointer)   ; void* optional
  (dw-reason-length :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WinHttpWebSocketClose = Win32::API::More->new('WINHTTP',
    'DWORD WinHttpWebSocketClose(LPVOID hWebSocket, WORD usStatus, LPVOID pvReason, DWORD dwReasonLength)');
# my $ret = $WinHttpWebSocketClose->Call($hWebSocket, $usStatus, $pvReason, $dwReasonLength);
# hWebSocket : void* -> LPVOID
# usStatus : WORD -> WORD
# pvReason : void* optional -> LPVOID
# dwReasonLength : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目