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

WSAHtons

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

シグネチャ

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

INT WSAHtons(
    SOCKET s,
    WORD hostshort,
    WORD* lpnetshort
);

パラメーター

名前方向
sSOCKETin
hostshortWORDin
lpnetshortWORD*out

戻り値の型: INT

各言語での呼び出し定義

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

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

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

lib = Fiddle.dlopen('WS2_32.dll')
WSAHtons = Fiddle::Function.new(
  lib['WSAHtons'],
  [
    Fiddle::TYPE_UINTPTR_T,  # s : SOCKET
    -Fiddle::TYPE_SHORT,  # hostshort : WORD
    Fiddle::TYPE_VOIDP,  # lpnetshort : WORD* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "ws2_32")]
extern "system" {
    fn WSAHtons(
        s: usize,  // SOCKET
        hostshort: u16,  // WORD
        lpnetshort: *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 WSAHtons(UIntPtr s, ushort hostshort, out ushort lpnetshort);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_WSAHtons' -Namespace Win32 -PassThru
# $api::WSAHtons(s, hostshort, lpnetshort)
#uselib "WS2_32.dll"
#func global WSAHtons "WSAHtons" sptr, sptr, sptr
; WSAHtons s, hostshort, varptr(lpnetshort)   ; 戻り値は stat
; s : SOCKET -> "sptr"
; hostshort : WORD -> "sptr"
; lpnetshort : WORD* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "WS2_32.dll"
#cfunc global WSAHtons "WSAHtons" sptr, int, var
; res = WSAHtons(s, hostshort, lpnetshort)
; s : SOCKET -> "sptr"
; hostshort : WORD -> "int"
; lpnetshort : WORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT WSAHtons(SOCKET s, WORD hostshort, WORD* lpnetshort)
#uselib "WS2_32.dll"
#cfunc global WSAHtons "WSAHtons" intptr, int, var
; res = WSAHtons(s, hostshort, lpnetshort)
; s : SOCKET -> "intptr"
; hostshort : WORD -> "int"
; lpnetshort : WORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
	procWSAHtons = ws2_32.NewProc("WSAHtons")
)

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