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

select

関数
複数ソケットの入出力可否を監視する。
DLLWS2_32.dll呼出規約winapiSetLastErrorあり対応OSWindows 8.1 以降

シグネチャ

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

INT select(
    INT nfds,
    FD_SET* readfds,   // optional
    FD_SET* writefds,   // optional
    FD_SET* exceptfds,   // optional
    const TIMEVAL* timeout   // optional
);

パラメーター

名前方向
nfdsINTin
readfdsFD_SET*inoutoptional
writefdsFD_SET*inoutoptional
exceptfdsFD_SET*inoutoptional
timeoutTIMEVAL*inoptional

戻り値の型: INT

各言語での呼び出し定義

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

INT select(
    INT nfds,
    FD_SET* readfds,   // optional
    FD_SET* writefds,   // optional
    FD_SET* exceptfds,   // optional
    const TIMEVAL* timeout   // optional
);
[DllImport("WS2_32.dll", SetLastError = true, ExactSpelling = true)]
static extern int select(
    int nfds,   // INT
    IntPtr readfds,   // FD_SET* optional, in/out
    IntPtr writefds,   // FD_SET* optional, in/out
    IntPtr exceptfds,   // FD_SET* optional, in/out
    IntPtr timeout   // TIMEVAL* optional
);
<DllImport("WS2_32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function select(
    nfds As Integer,   ' INT
    readfds As IntPtr,   ' FD_SET* optional, in/out
    writefds As IntPtr,   ' FD_SET* optional, in/out
    exceptfds As IntPtr,   ' FD_SET* optional, in/out
    timeout As IntPtr   ' TIMEVAL* optional
) As Integer
End Function
' nfds : INT
' readfds : FD_SET* optional, in/out
' writefds : FD_SET* optional, in/out
' exceptfds : FD_SET* optional, in/out
' timeout : TIMEVAL* optional
Declare PtrSafe Function select Lib "ws2_32" ( _
    ByVal nfds As Long, _
    ByVal readfds As LongPtr, _
    ByVal writefds As LongPtr, _
    ByVal exceptfds As LongPtr, _
    ByVal timeout As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

select = ctypes.windll.ws2_32.select
select.restype = ctypes.c_int
select.argtypes = [
    ctypes.c_int,  # nfds : INT
    ctypes.c_void_p,  # readfds : FD_SET* optional, in/out
    ctypes.c_void_p,  # writefds : FD_SET* optional, in/out
    ctypes.c_void_p,  # exceptfds : FD_SET* optional, in/out
    ctypes.c_void_p,  # timeout : TIMEVAL* optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
	procselect = ws2_32.NewProc("select")
)

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