ホーム › Networking.WinSock › WSASocketW
WSASocketW
関数プロトコル情報やフラグを指定してソケットを作成する。
シグネチャ
// WS2_32.dll (Unicode / -W)
#include <windows.h>
SOCKET WSASocketW(
INT af,
INT type,
INT protocol,
WSAPROTOCOL_INFOW* lpProtocolInfo, // optional
DWORD g,
DWORD dwFlags
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| af | INT | in | アドレスファミリを指定する(AF_INET/AF_INET6等)。 |
| type | INT | in | ソケットの型を指定する(SOCK_STREAM/SOCK_DGRAM等)。 |
| protocol | INT | in | 使用するプロトコルを指定する(IPPROTO_TCP/IPPROTO_UDP等)。0で既定。 |
| lpProtocolInfo | WSAPROTOCOL_INFOW* | inoptional | 使用するプロトコルを定義するWSAPROTOCOL_INFOWへのポインタ。NULL可。 |
| g | DWORD | in | ソケットグループの指定。新規グループ作成や既存グループID等。 |
| dwFlags | DWORD | in | ソケット属性フラグ(WSA_FLAG_OVERLAPPED等)。 |
戻り値の型: SOCKET
各言語での呼び出し定義
// WS2_32.dll (Unicode / -W)
#include <windows.h>
SOCKET WSASocketW(
INT af,
INT type,
INT protocol,
WSAPROTOCOL_INFOW* lpProtocolInfo, // optional
DWORD g,
DWORD dwFlags
);[DllImport("WS2_32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern UIntPtr WSASocketW(
int af, // INT
int type, // INT
int protocol, // INT
IntPtr lpProtocolInfo, // WSAPROTOCOL_INFOW* optional
uint g, // DWORD
uint dwFlags // DWORD
);<DllImport("WS2_32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WSASocketW(
af As Integer, ' INT
type As Integer, ' INT
protocol As Integer, ' INT
lpProtocolInfo As IntPtr, ' WSAPROTOCOL_INFOW* optional
g As UInteger, ' DWORD
dwFlags As UInteger ' DWORD
) As UIntPtr
End Function' af : INT
' type : INT
' protocol : INT
' lpProtocolInfo : WSAPROTOCOL_INFOW* optional
' g : DWORD
' dwFlags : DWORD
Declare PtrSafe Function WSASocketW 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
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
WSASocketW = ctypes.windll.ws2_32.WSASocketW
WSASocketW.restype = ctypes.c_size_t
WSASocketW.argtypes = [
ctypes.c_int, # af : INT
ctypes.c_int, # type : INT
ctypes.c_int, # protocol : INT
ctypes.c_void_p, # lpProtocolInfo : WSAPROTOCOL_INFOW* 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')
WSASocketW = Fiddle::Function.new(
lib['WSASocketW'],
[
Fiddle::TYPE_INT, # af : INT
Fiddle::TYPE_INT, # type : INT
Fiddle::TYPE_INT, # protocol : INT
Fiddle::TYPE_VOIDP, # lpProtocolInfo : WSAPROTOCOL_INFOW* optional
-Fiddle::TYPE_INT, # g : DWORD
-Fiddle::TYPE_INT, # dwFlags : DWORD
],
Fiddle::TYPE_UINTPTR_T)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "ws2_32")]
extern "system" {
fn WSASocketW(
af: i32, // INT
type: i32, // INT
protocol: i32, // INT
lpProtocolInfo: *mut WSAPROTOCOL_INFOW, // WSAPROTOCOL_INFOW* 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.Unicode, SetLastError = true)]
public static extern UIntPtr WSASocketW(int af, int type, int protocol, IntPtr lpProtocolInfo, uint g, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_WSASocketW' -Namespace Win32 -PassThru
# $api::WSASocketW(af, type, protocol, lpProtocolInfo, g, dwFlags)#uselib "WS2_32.dll"
#func global WSASocketW "WSASocketW" wptr, wptr, wptr, wptr, wptr, wptr
; WSASocketW af, type, protocol, varptr(lpProtocolInfo), g, dwFlags ; 戻り値は stat
; af : INT -> "wptr"
; type : INT -> "wptr"
; protocol : INT -> "wptr"
; lpProtocolInfo : WSAPROTOCOL_INFOW* optional -> "wptr"
; g : DWORD -> "wptr"
; dwFlags : DWORD -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "WS2_32.dll" #cfunc global WSASocketW "WSASocketW" int, int, int, var, int, int ; res = WSASocketW(af, type, protocol, lpProtocolInfo, g, dwFlags) ; af : INT -> "int" ; type : INT -> "int" ; protocol : INT -> "int" ; lpProtocolInfo : WSAPROTOCOL_INFOW* optional -> "var" ; g : DWORD -> "int" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "WS2_32.dll" #cfunc global WSASocketW "WSASocketW" int, int, int, sptr, int, int ; res = WSASocketW(af, type, protocol, varptr(lpProtocolInfo), g, dwFlags) ; af : INT -> "int" ; type : INT -> "int" ; protocol : INT -> "int" ; lpProtocolInfo : WSAPROTOCOL_INFOW* optional -> "sptr" ; g : DWORD -> "int" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; SOCKET WSASocketW(INT af, INT type, INT protocol, WSAPROTOCOL_INFOW* lpProtocolInfo, DWORD g, DWORD dwFlags) #uselib "WS2_32.dll" #cfunc global WSASocketW "WSASocketW" int, int, int, var, int, int ; res = WSASocketW(af, type, protocol, lpProtocolInfo, g, dwFlags) ; af : INT -> "int" ; type : INT -> "int" ; protocol : INT -> "int" ; lpProtocolInfo : WSAPROTOCOL_INFOW* optional -> "var" ; g : DWORD -> "int" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; SOCKET WSASocketW(INT af, INT type, INT protocol, WSAPROTOCOL_INFOW* lpProtocolInfo, DWORD g, DWORD dwFlags) #uselib "WS2_32.dll" #cfunc global WSASocketW "WSASocketW" int, int, int, intptr, int, int ; res = WSASocketW(af, type, protocol, varptr(lpProtocolInfo), g, dwFlags) ; af : INT -> "int" ; type : INT -> "int" ; protocol : INT -> "int" ; lpProtocolInfo : WSAPROTOCOL_INFOW* optional -> "intptr" ; g : DWORD -> "int" ; dwFlags : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
procWSASocketW = ws2_32.NewProc("WSASocketW")
)
// af (INT), type (INT), protocol (INT), lpProtocolInfo (WSAPROTOCOL_INFOW* optional), g (DWORD), dwFlags (DWORD)
r1, _, err := procWSASocketW.Call(
uintptr(af),
uintptr(type),
uintptr(protocol),
uintptr(lpProtocolInfo),
uintptr(g),
uintptr(dwFlags),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // SOCKETfunction WSASocketW(
af: Integer; // INT
type: Integer; // INT
protocol: Integer; // INT
lpProtocolInfo: Pointer; // WSAPROTOCOL_INFOW* optional
g: DWORD; // DWORD
dwFlags: DWORD // DWORD
): NativeUInt; stdcall;
external 'WS2_32.dll' name 'WSASocketW';result := DllCall("WS2_32\WSASocketW"
, "Int", af ; INT
, "Int", type ; INT
, "Int", protocol ; INT
, "Ptr", lpProtocolInfo ; WSAPROTOCOL_INFOW* optional
, "UInt", g ; DWORD
, "UInt", dwFlags ; DWORD
, "UPtr") ; return: SOCKET●WSASocketW(af, type, protocol, lpProtocolInfo, g, dwFlags) = DLL("WS2_32.dll", "int WSASocketW(int, int, int, void*, dword, dword)")
# 呼び出し: WSASocketW(af, type, protocol, lpProtocolInfo, g, dwFlags)
# af : INT -> "int"
# type : INT -> "int"
# protocol : INT -> "int"
# lpProtocolInfo : WSAPROTOCOL_INFOW* optional -> "void*"
# g : DWORD -> "dword"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。