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

htonl

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

シグネチャ

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

DWORD htonl(
    DWORD hostlong
);

パラメーター

名前方向
hostlongDWORDin

戻り値の型: DWORD

各言語での呼び出し定義

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

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

htonl = ctypes.windll.ws2_32.htonl
htonl.restype = wintypes.DWORD
htonl.argtypes = [
    wintypes.DWORD,  # hostlong : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
	prochtonl = ws2_32.NewProc("htonl")
)

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