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

htons

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

シグネチャ

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

WORD htons(
    WORD hostshort
);

パラメーター

名前方向
hostshortWORDin

戻り値の型: WORD

各言語での呼び出し定義

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

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

htons = ctypes.windll.ws2_32.htons
htons.restype = ctypes.c_ushort
htons.argtypes = [
    ctypes.c_ushort,  # hostshort : WORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WS2_32.dll')
htons = Fiddle::Function.new(
  lib['htons'],
  [
    -Fiddle::TYPE_SHORT,  # hostshort : WORD
  ],
  -Fiddle::TYPE_SHORT)
#[link(name = "ws2_32")]
extern "system" {
    fn htons(
        hostshort: u16  // WORD
    ) -> u16;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("WS2_32.dll", SetLastError = true)]
public static extern ushort htons(ushort hostshort);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_htons' -Namespace Win32 -PassThru
# $api::htons(hostshort)
#uselib "WS2_32.dll"
#func global htons "htons" sptr
; htons hostshort   ; 戻り値は stat
; hostshort : WORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "WS2_32.dll"
#cfunc global htons "htons" int
; res = htons(hostshort)
; hostshort : WORD -> "int"
; WORD htons(WORD hostshort)
#uselib "WS2_32.dll"
#cfunc global htons "htons" int
; res = htons(hostshort)
; hostshort : WORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
	prochtons = ws2_32.NewProc("htons")
)

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