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

WSASocketA

関数
プロトコル情報やフラグを指定してソケットを作成する。
DLLWS2_32.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows 8.1 以降

シグネチャ

// WS2_32.dll  (ANSI / -A)
#include <windows.h>

SOCKET WSASocketA(
    INT af,
    INT type,
    INT protocol,
    WSAPROTOCOL_INFOA* lpProtocolInfo,   // optional
    DWORD g,
    DWORD dwFlags
);

パラメーター

名前方向
afINTin
typeINTin
protocolINTin
lpProtocolInfoWSAPROTOCOL_INFOA*inoptional
gDWORDin
dwFlagsDWORDin

戻り値の型: SOCKET

各言語での呼び出し定義

// WS2_32.dll  (ANSI / -A)
#include <windows.h>

SOCKET WSASocketA(
    INT af,
    INT type,
    INT protocol,
    WSAPROTOCOL_INFOA* lpProtocolInfo,   // optional
    DWORD g,
    DWORD dwFlags
);
[DllImport("WS2_32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern UIntPtr WSASocketA(
    int af,   // INT
    int type,   // INT
    int protocol,   // INT
    IntPtr lpProtocolInfo,   // WSAPROTOCOL_INFOA* optional
    uint g,   // DWORD
    uint dwFlags   // DWORD
);
<DllImport("WS2_32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WSASocketA(
    af As Integer,   ' INT
    type As Integer,   ' INT
    protocol As Integer,   ' INT
    lpProtocolInfo As IntPtr,   ' WSAPROTOCOL_INFOA* optional
    g As UInteger,   ' DWORD
    dwFlags As UInteger   ' DWORD
) As UIntPtr
End Function
' af : INT
' type : INT
' protocol : INT
' lpProtocolInfo : WSAPROTOCOL_INFOA* optional
' g : DWORD
' dwFlags : DWORD
Declare PtrSafe Function WSASocketA Lib "ws2_32" ( _
    ByVal af As Long, _
    ByVal type As Long, _
    ByVal protocol As Long, _
    ByVal lpProtocolInfo As LongPtr, _
    ByVal g As Long, _
    ByVal dwFlags As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WSASocketA = ctypes.windll.ws2_32.WSASocketA
WSASocketA.restype = ctypes.c_size_t
WSASocketA.argtypes = [
    ctypes.c_int,  # af : INT
    ctypes.c_int,  # type : INT
    ctypes.c_int,  # protocol : INT
    ctypes.c_void_p,  # lpProtocolInfo : WSAPROTOCOL_INFOA* optional
    wintypes.DWORD,  # g : DWORD
    wintypes.DWORD,  # dwFlags : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
	procWSASocketA = ws2_32.NewProc("WSASocketA")
)

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