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

ioctlsocket

関数
ソケットの入出力モードを制御する。
DLLWS2_32.dll呼出規約winapiSetLastErrorあり対応OSWindows 8.1 以降

シグネチャ

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

INT ioctlsocket(
    SOCKET s,
    INT cmd,
    DWORD* argp
);

パラメーター

名前方向
sSOCKETin
cmdINTin
argpDWORD*inout

戻り値の型: INT

各言語での呼び出し定義

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

INT ioctlsocket(
    SOCKET s,
    INT cmd,
    DWORD* argp
);
[DllImport("WS2_32.dll", SetLastError = true, ExactSpelling = true)]
static extern int ioctlsocket(
    UIntPtr s,   // SOCKET
    int cmd,   // INT
    ref uint argp   // DWORD* in/out
);
<DllImport("WS2_32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function ioctlsocket(
    s As UIntPtr,   ' SOCKET
    cmd As Integer,   ' INT
    ByRef argp As UInteger   ' DWORD* in/out
) As Integer
End Function
' s : SOCKET
' cmd : INT
' argp : DWORD* in/out
Declare PtrSafe Function ioctlsocket Lib "ws2_32" ( _
    ByVal s As LongPtr, _
    ByVal cmd As Long, _
    ByRef argp As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

ioctlsocket = ctypes.windll.ws2_32.ioctlsocket
ioctlsocket.restype = ctypes.c_int
ioctlsocket.argtypes = [
    ctypes.c_size_t,  # s : SOCKET
    ctypes.c_int,  # cmd : INT
    ctypes.POINTER(wintypes.DWORD),  # argp : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
	procioctlsocket = ws2_32.NewProc("ioctlsocket")
)

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