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

WSANtohs

関数
16ビット値をネットワーク順からホストバイト順へ変換する。
DLLWS2_32.dll呼出規約winapiSetLastErrorあり対応OSWindows 8.1 以降

シグネチャ

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

INT WSANtohs(
    SOCKET s,
    WORD netshort,
    WORD* lphostshort
);

パラメーター

名前方向
sSOCKETin
netshortWORDin
lphostshortWORD*out

戻り値の型: INT

各言語での呼び出し定義

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

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

WSANtohs = ctypes.windll.ws2_32.WSANtohs
WSANtohs.restype = ctypes.c_int
WSANtohs.argtypes = [
    ctypes.c_size_t,  # s : SOCKET
    ctypes.c_ushort,  # netshort : WORD
    ctypes.POINTER(ctypes.c_ushort),  # lphostshort : WORD* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
	procWSANtohs = ws2_32.NewProc("WSANtohs")
)

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