WSAHtons
関数シグネチャ
// WS2_32.dll
#include <windows.h>
INT WSAHtons(
SOCKET s,
WORD hostshort,
WORD* lpnetshort
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| s | SOCKET | in | ソケットを識別する記述子。 |
| hostshort | WORD | in | ホストバイトオーダーの 16 ビットの数値。 |
| lpnetshort | WORD* | out | ネットワークバイトオーダーの数値を受け取る 16 ビットバッファーへのポインター。 |
戻り値の型: INT
公式ドキュメント
WSAHtons 関数は、u_short をホストバイトオーダーからネットワークバイトオーダーへ変換します。
戻り値
エラーが発生しなかった場合、 WSAHtons は 0 を返します。それ以外の場合は SOCKET_ERROR が返され、 WSAGetLastError を呼び出すことで特定のエラーコードを取得できます。
| エラーコード | 意味 |
|---|---|
| この関数を使用する前に、 WSAStartup の呼び出しが成功している必要があります。 | |
| ネットワークサブシステムで障害が発生しました。 | |
| 記述子がソケットではありません。 | |
| lpnetshort パラメーターが NULL であるか、指し示すアドレスがユーザーアドレス空間の有効な部分に完全には含まれていません。 |
解説(Remarks)
WSAHtons 関数は、ホストバイトオーダーの 16 ビットの数値を受け取り、ネットワークバイトオーダーの 16 ビットの数値を lpnetshort パラメーターが指す 16 ビットの数値に格納して返します。s パラメーターで渡されたソケットは、そのソケットに関連付けられた Winsock カタログのプロトコルエントリに基づいて、必要なネットワークバイトオーダーを判別するために使用されます。この機能により、異なるネットワークバイトオーダーを使用する Winsock プロバイダーがサポートされます。
ソケットが AF_INET または AF_INET6 アドレスファミリ用である場合、 WSAHtons 関数を使用して、ホストバイトオーダーの IP ポート番号をネットワークバイトオーダーの IP ポート番号に変換できます。
WSAHtons 関数を使用するには、事前に WSAStartup 関数の呼び出しが成功し、Winsock DLL が読み込まれている必要があります。AF_INET または AF_INET6 アドレスファミリで使用する場合、htons 関数は Winsock DLL の読み込みを必要としません。
Windows Phone 8: この関数は、Windows Phone 8 以降の Windows Phone ストアアプリでサポートされています。
Windows 8.1 および Windows Server 2012 R2: この関数は、Windows 8.1、Windows Server 2012 R2 以降の Windows ストアアプリでサポートされています。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// 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 方式にも切替可。#uselib "WS2_32.dll" #cfunc global WSAHtons "WSAHtons" sptr, int, sptr ; res = WSAHtons(s, hostshort, varptr(lpnetshort)) ; s : SOCKET -> "sptr" ; hostshort : WORD -> "int" ; lpnetshort : WORD* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは 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 方式にも切替可。; INT WSAHtons(SOCKET s, WORD hostshort, WORD* lpnetshort) #uselib "WS2_32.dll" #cfunc global WSAHtons "WSAHtons" intptr, int, intptr ; res = WSAHtons(s, hostshort, varptr(lpnetshort)) ; s : SOCKET -> "intptr" ; hostshort : WORD -> "int" ; lpnetshort : WORD* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは 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 // INTfunction 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)。const std = @import("std");
extern "ws2_32" fn WSAHtons(
s: usize, // SOCKET
hostshort: u16, // WORD
lpnetshort: [*c]u16 // WORD* out
) callconv(std.os.windows.WINAPI) i32;proc WSAHtons(
s: uint, # SOCKET
hostshort: uint16, # WORD
lpnetshort: ptr uint16 # WORD* out
): int32 {.importc: "WSAHtons", stdcall, dynlib: "WS2_32.dll".}pragma(lib, "ws2_32");
extern(Windows)
int WSAHtons(
size_t s, // SOCKET
ushort hostshort, // WORD
ushort* lpnetshort // WORD* out
);ccall((:WSAHtons, "WS2_32.dll"), stdcall, Int32,
(Csize_t, UInt16, Ptr{UInt16}),
s, hostshort, lpnetshort)
# s : SOCKET -> Csize_t
# hostshort : WORD -> UInt16
# lpnetshort : WORD* out -> Ptr{UInt16}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t WSAHtons(
uintptr_t s,
uint16_t hostshort,
uint16_t* lpnetshort);
]]
local ws2_32 = ffi.load("ws2_32")
-- ws2_32.WSAHtons(s, hostshort, lpnetshort)
-- s : SOCKET
-- hostshort : WORD
-- lpnetshort : WORD* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('WS2_32.dll');
const WSAHtons = lib.func('__stdcall', 'WSAHtons', 'int32_t', ['uintptr_t', 'uint16_t', 'uint16_t *']);
// WSAHtons(s, hostshort, lpnetshort)
// s : SOCKET -> 'uintptr_t'
// hostshort : WORD -> 'uint16_t'
// lpnetshort : WORD* out -> 'uint16_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("WS2_32.dll", {
WSAHtons: { parameters: ["usize", "u16", "pointer"], result: "i32" },
});
// lib.symbols.WSAHtons(s, hostshort, lpnetshort)
// s : SOCKET -> "usize"
// hostshort : WORD -> "u16"
// lpnetshort : WORD* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t WSAHtons(
size_t s,
uint16_t hostshort,
uint16_t* lpnetshort);
C, "WS2_32.dll");
// $ffi->WSAHtons(s, hostshort, lpnetshort);
// s : SOCKET
// hostshort : WORD
// lpnetshort : WORD* out
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Ws2_32 extends StdCallLibrary {
Ws2_32 INSTANCE = Native.load("ws2_32", Ws2_32.class);
int WSAHtons(
long s, // SOCKET
short hostshort, // WORD
ShortByReference lpnetshort // WORD* out
);
}@[Link("ws2_32")]
lib LibWS2_32
fun WSAHtons = WSAHtons(
s : LibC::SizeT, # SOCKET
hostshort : UInt16, # WORD
lpnetshort : UInt16* # WORD* out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef WSAHtonsNative = Int32 Function(UintPtr, Uint16, Pointer<Uint16>);
typedef WSAHtonsDart = int Function(int, int, Pointer<Uint16>);
final WSAHtons = DynamicLibrary.open('WS2_32.dll')
.lookupFunction<WSAHtonsNative, WSAHtonsDart>('WSAHtons');
// s : SOCKET -> UintPtr
// hostshort : WORD -> Uint16
// lpnetshort : WORD* out -> Pointer<Uint16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function WSAHtons(
s: NativeUInt; // SOCKET
hostshort: Word; // WORD
lpnetshort: Pointer // WORD* out
): Integer; stdcall;
external 'WS2_32.dll' name 'WSAHtons';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "WSAHtons"
c_WSAHtons :: CUIntPtr -> Word16 -> Ptr Word16 -> IO Int32
-- s : SOCKET -> CUIntPtr
-- hostshort : WORD -> Word16
-- lpnetshort : WORD* out -> Ptr Word16
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let wsahtons =
foreign "WSAHtons"
(size_t @-> uint16_t @-> (ptr uint16_t) @-> returning int32_t)
(* s : SOCKET -> size_t *)
(* hostshort : WORD -> uint16_t *)
(* lpnetshort : WORD* out -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library ws2_32 (t "WS2_32.dll"))
(cffi:use-foreign-library ws2_32)
(cffi:defcfun ("WSAHtons" wsahtons :convention :stdcall) :int32
(s :uint64) ; SOCKET
(hostshort :uint16) ; WORD
(lpnetshort :pointer)) ; WORD* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $WSAHtons = Win32::API::More->new('WS2_32',
'int WSAHtons(WPARAM s, WORD hostshort, LPVOID lpnetshort)');
# my $ret = $WSAHtons->Call($s, $hostshort, $lpnetshort);
# s : SOCKET -> WPARAM
# hostshort : WORD -> WORD
# lpnetshort : WORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f InetNtopW — バイナリ形式のIPアドレスをUnicode文字列形式に変換する。
- f WSAHtonl — 32ビット値をホスト順からネットワークバイト順へ変換する。
- f WSANtohl — 32ビット値をネットワーク順からホストバイト順へ変換する。
- f WSANtohs — 16ビット値をネットワーク順からホストバイト順へ変換する。
- f htonl — 32ビット値をホスト順からネットワークバイト順へ変換する。
- f htons — 16ビット値をホスト順からネットワークバイト順へ変換する。
- f inet_addr — IPアドレス文字列を32ビットアドレスへ変換する。
- f inet_ntoa — IPアドレスをドット区切りの文字列へ変換する。
- f ntohl — 32ビット値をネットワーク順からホストバイト順へ変換する。
- f ntohs — 16ビット値をネットワーク順からホストバイト順へ変換する。