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

WSARecvEx

関数
メッセージ境界情報付きでソケットからデータを受信する。
DLLMSWSOCK.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

INT WSARecvEx(
    SOCKET s,
    LPSTR buf,
    INT len,
    INT* flags
);

パラメーター

名前方向
sSOCKETin
bufLPSTRout
lenINTin
flagsINT*inout

戻り値の型: INT

各言語での呼び出し定義

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

INT WSARecvEx(
    SOCKET s,
    LPSTR buf,
    INT len,
    INT* flags
);
[DllImport("MSWSOCK.dll", SetLastError = true, ExactSpelling = true)]
static extern int WSARecvEx(
    UIntPtr s,   // SOCKET
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder buf,   // LPSTR out
    int len,   // INT
    ref int flags   // INT* in/out
);
<DllImport("MSWSOCK.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WSARecvEx(
    s As UIntPtr,   ' SOCKET
    <MarshalAs(UnmanagedType.LPStr)> buf As System.Text.StringBuilder,   ' LPSTR out
    len As Integer,   ' INT
    ByRef flags As Integer   ' INT* in/out
) As Integer
End Function
' s : SOCKET
' buf : LPSTR out
' len : INT
' flags : INT* in/out
Declare PtrSafe Function WSARecvEx Lib "mswsock" ( _
    ByVal s As LongPtr, _
    ByVal buf As String, _
    ByVal len As Long, _
    ByRef flags As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WSARecvEx = ctypes.windll.mswsock.WSARecvEx
WSARecvEx.restype = ctypes.c_int
WSARecvEx.argtypes = [
    ctypes.c_size_t,  # s : SOCKET
    wintypes.LPSTR,  # buf : LPSTR out
    ctypes.c_int,  # len : INT
    ctypes.POINTER(ctypes.c_int),  # flags : INT* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('MSWSOCK.dll')
WSARecvEx = Fiddle::Function.new(
  lib['WSARecvEx'],
  [
    Fiddle::TYPE_UINTPTR_T,  # s : SOCKET
    Fiddle::TYPE_VOIDP,  # buf : LPSTR out
    Fiddle::TYPE_INT,  # len : INT
    Fiddle::TYPE_VOIDP,  # flags : INT* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "mswsock")]
extern "system" {
    fn WSARecvEx(
        s: usize,  // SOCKET
        buf: *mut u8,  // LPSTR out
        len: i32,  // INT
        flags: *mut i32  // INT* in/out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("MSWSOCK.dll", SetLastError = true)]
public static extern int WSARecvEx(UIntPtr s, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder buf, int len, ref int flags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'MSWSOCK_WSARecvEx' -Namespace Win32 -PassThru
# $api::WSARecvEx(s, buf, len, flags)
#uselib "MSWSOCK.dll"
#func global WSARecvEx "WSARecvEx" sptr, sptr, sptr, sptr
; WSARecvEx s, varptr(buf), len, varptr(flags)   ; 戻り値は stat
; s : SOCKET -> "sptr"
; buf : LPSTR out -> "sptr"
; len : INT -> "sptr"
; flags : INT* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "MSWSOCK.dll"
#cfunc global WSARecvEx "WSARecvEx" sptr, var, int, var
; res = WSARecvEx(s, buf, len, flags)
; s : SOCKET -> "sptr"
; buf : LPSTR out -> "var"
; len : INT -> "int"
; flags : INT* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT WSARecvEx(SOCKET s, LPSTR buf, INT len, INT* flags)
#uselib "MSWSOCK.dll"
#cfunc global WSARecvEx "WSARecvEx" intptr, var, int, var
; res = WSARecvEx(s, buf, len, flags)
; s : SOCKET -> "intptr"
; buf : LPSTR out -> "var"
; len : INT -> "int"
; flags : INT* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	mswsock = windows.NewLazySystemDLL("MSWSOCK.dll")
	procWSARecvEx = mswsock.NewProc("WSARecvEx")
)

// s (SOCKET), buf (LPSTR out), len (INT), flags (INT* in/out)
r1, _, err := procWSARecvEx.Call(
	uintptr(s),
	uintptr(buf),
	uintptr(len),
	uintptr(flags),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function WSARecvEx(
  s: NativeUInt;   // SOCKET
  buf: PAnsiChar;   // LPSTR out
  len: Integer;   // INT
  flags: Pointer   // INT* in/out
): Integer; stdcall;
  external 'MSWSOCK.dll' name 'WSARecvEx';
result := DllCall("MSWSOCK\WSARecvEx"
    , "UPtr", s   ; SOCKET
    , "Ptr", buf   ; LPSTR out
    , "Int", len   ; INT
    , "Ptr", flags   ; INT* in/out
    , "Int")   ; return: INT
●WSARecvEx(s, buf, len, flags) = DLL("MSWSOCK.dll", "int WSARecvEx(int, char*, int, void*)")
# 呼び出し: WSARecvEx(s, buf, len, flags)
# s : SOCKET -> "int"
# buf : LPSTR out -> "char*"
# len : INT -> "int"
# flags : INT* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。