InetPtonW
関数シグネチャ
// WS2_32.dll
#include <windows.h>
INT InetPtonW(
INT Family,
LPCWSTR pszAddrString,
void* pAddrBuf
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||
|---|---|---|---|---|---|---|---|---|---|
| Family | INT | in | アドレスファミリ。 アドレスファミリに指定できる値は Ws2def.h ヘッダーファイルで定義されています。Ws2def.h ヘッダーファイルは Winsock2.h に自動的にインクルードされるため、直接使用しないでください。AF_ アドレスファミリ定数と PF_ プロトコルファミリ定数の値は同一であり(たとえば AF_INET と PF_INET)、どちらの定数も使用できます。 現在サポートされている値は AF_INET と AF_INET6 です。
| ||||||
| pszAddrString | LPCWSTR | in | 数値のバイナリ形式に変換する IP アドレスのテキスト表現を含む、NULL で終端された文字列へのポインター。 Family パラメーターが AF_INET の場合、pszAddrString パラメーターは標準的なドット区切り 10 進表記の IPv4 アドレスのテキスト表現を指す必要があります。 Family パラメーターが AF_INET6 の場合、pszAddrString パラメーターは標準表記の IPv6 アドレスのテキスト表現を指す必要があります。 | ||||||
| pAddrBuf | void* | out | IP アドレスの数値のバイナリ表現を格納するバッファーへのポインター。IP アドレスはネットワークバイトオーダーで返されます。 Family パラメーターが AF_INET の場合、このバッファーは IN_ADDR 構造体を保持できる十分な大きさが必要です。 Family パラメーターが AF_INET6 の場合、このバッファーは IN6_ADDR 構造体を保持できる十分な大きさが必要です。 |
戻り値の型: INT
公式ドキュメント
InetPton 関数は、標準的なテキスト表現形式の IPv4 または IPv6 インターネットネットワークアドレスを、数値のバイナリ形式に変換します。この関数の ANSI バージョンは inet_pton です。(InetPtonW)
戻り値
エラーが発生しなかった場合、InetPton 関数は値 1 を返し、pAddrBuf パラメーターが指すバッファーにはネットワークバイトオーダーのバイナリ数値 IP アドレスが格納されます。
pAddrBuf パラメーターが有効な IPv4 ドット区切り 10 進文字列でも有効な IPv6 アドレス文字列でもない文字列を指している場合、InetPton 関数は値 0 を返します。それ以外の場合は値 -1 が返され、WSAGetLastError を呼び出すことで特定のエラーコードを取得して、詳細なエラー情報を得ることができます。
関数でエラーが発生した場合、WSAGetLastError によって返される拡張エラーコードは、次のいずれかの値になります。
| エラーコード | 意味 |
|---|---|
| Family パラメーターに指定されたアドレスファミリがサポートされていません。このエラーは、指定された Family パラメーターが AF_INET または AF_INET6 でない場合に返されます。 | |
| pszAddrString または pAddrBuf パラメーターが NULL であるか、ユーザーアドレス空間の一部ではありません。 |
解説(Remarks)
InetPton 関数は Windows Vista 以降でサポートされています。
InetPton 関数は、標準的なテキスト表現形式のインターネットネットワークアドレスを数値のバイナリ形式に変換する、プロトコルに依存しない変換を提供します。InetPton 関数は、pszAddrString パラメーターが指すインターネットアドレスのテキスト表現を受け取り、pAddrBuf パラメーターに数値のバイナリ IP アドレスへのポインターを返します。inet_addr 関数が IPv4 アドレス文字列でのみ動作するのに対し、InetPton 関数は IPv4 と IPv6 のどちらのアドレス文字列でも動作します。
この関数の ANSI バージョンは、RFC 2553 で定義されている inet_pton です。詳細については、IETF の Web サイトで入手できる RFC 2553 を参照してください。
InetPton 関数は、IP アドレスを表すテキスト文字列を数値のバイナリ IP アドレスに変換するために、Windows Sockets DLL の読み込みを必要としません。
Family パラメーターに AF_INET を指定した場合、pszAddrString パラメーターは、ドット区切り 10 進表記の IPv4 アドレスのテキスト文字列(ドット区切り 10 進表記の IPv4 アドレスの例である "192.168.16.0" など)を指す必要があります。
Family パラメーターに AF_INET6 を指定した場合、pszAddrString パラメーターは、インターネット標準形式の IPv6 アドレスのテキスト文字列を指す必要があります。基本的な文字列表現は、コロンで区切られた 8 個の 16 進数で構成されます。連続する 0 の並びはダブルコロンで置き換えることができます。IPv6 アドレスの文字列表現中にダブルコロンを含められるのは 1 か所だけです。アドレスが IPv4 互換アドレスである場合、最後の 32 ビットは IPv4 形式のドット区切りオクテット表記で表すことができます。
UNICODE または _UNICODE が定義されている場合、InetPton はこの関数の Unicode バージョンである InetPtonW として定義されます。pszAddrString パラメーターは PCWSTR データ型として定義されます。
UNICODE または _UNICODE が定義されていない場合、InetPton はこの関数の ANSI バージョンである InetPtonA として定義されます。この関数の ANSI バージョンは常に inet_pton として定義されます。pszAddrString パラメーターは PCSTR データ型として定義されます。
IN_ADDR 構造体は Inaddr.h ヘッダーファイルで定義されています。
IN6_ADDR 構造体は In6addr.h ヘッダーファイルで定義されています。
Windows Vista 以降では、RtlIpv4StringToAddress 関数および RtlIpv4StringToAddressEx 関数を使用して、インターネット標準のドット区切り 10 進表記の IPv4 アドレスのテキスト表現を、IN_ADDR 構造体として表される数値のバイナリアドレスに変換できます。Windows Vista 以降では、RtlIpv6StringToAddress 関数および RtlIpv6StringToAddressEx 関数を使用して、IPv6 アドレスの文字列表現を、IN6_ADDR 構造体として表される数値のバイナリ IPv6 アドレスに変換できます。RtlIpv6StringToAddressEx 関数は、標準表記でスコープ ID とポートを含めることができる IPv6 アドレスの文字列表現も数値のバイナリ形式に変換できるため、より柔軟です。
Windows 8.1 および Windows Server 2012 R2: InetPtonW 関数は、Windows 8.1、Windows Server 2012 R2 以降の Windows ストアアプリでサポートされています。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// WS2_32.dll
#include <windows.h>
INT InetPtonW(
INT Family,
LPCWSTR pszAddrString,
void* pAddrBuf
);[DllImport("WS2_32.dll", ExactSpelling = true)]
static extern int InetPtonW(
int Family, // INT
[MarshalAs(UnmanagedType.LPWStr)] string pszAddrString, // LPCWSTR
IntPtr pAddrBuf // void* out
);<DllImport("WS2_32.dll", ExactSpelling:=True)>
Public Shared Function InetPtonW(
Family As Integer, ' INT
<MarshalAs(UnmanagedType.LPWStr)> pszAddrString As String, ' LPCWSTR
pAddrBuf As IntPtr ' void* out
) As Integer
End Function' Family : INT
' pszAddrString : LPCWSTR
' pAddrBuf : void* out
Declare PtrSafe Function InetPtonW Lib "ws2_32" ( _
ByVal Family As Long, _
ByVal pszAddrString As LongPtr, _
ByVal pAddrBuf As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
InetPtonW = ctypes.windll.ws2_32.InetPtonW
InetPtonW.restype = ctypes.c_int
InetPtonW.argtypes = [
ctypes.c_int, # Family : INT
wintypes.LPCWSTR, # pszAddrString : LPCWSTR
ctypes.POINTER(None), # pAddrBuf : void* out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('WS2_32.dll')
InetPtonW = Fiddle::Function.new(
lib['InetPtonW'],
[
Fiddle::TYPE_INT, # Family : INT
Fiddle::TYPE_VOIDP, # pszAddrString : LPCWSTR
Fiddle::TYPE_VOIDP, # pAddrBuf : void* out
],
Fiddle::TYPE_INT)#[link(name = "ws2_32")]
extern "system" {
fn InetPtonW(
Family: i32, // INT
pszAddrString: *const u16, // LPCWSTR
pAddrBuf: *mut () // void* out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("WS2_32.dll")]
public static extern int InetPtonW(int Family, [MarshalAs(UnmanagedType.LPWStr)] string pszAddrString, IntPtr pAddrBuf);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_InetPtonW' -Namespace Win32 -PassThru
# $api::InetPtonW(Family, pszAddrString, pAddrBuf)#uselib "WS2_32.dll"
#func global InetPtonW "InetPtonW" sptr, sptr, sptr
; InetPtonW Family, pszAddrString, pAddrBuf ; 戻り値は stat
; Family : INT -> "sptr"
; pszAddrString : LPCWSTR -> "sptr"
; pAddrBuf : void* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "WS2_32.dll"
#cfunc global InetPtonW "InetPtonW" int, wstr, sptr
; res = InetPtonW(Family, pszAddrString, pAddrBuf)
; Family : INT -> "int"
; pszAddrString : LPCWSTR -> "wstr"
; pAddrBuf : void* out -> "sptr"; INT InetPtonW(INT Family, LPCWSTR pszAddrString, void* pAddrBuf)
#uselib "WS2_32.dll"
#cfunc global InetPtonW "InetPtonW" int, wstr, intptr
; res = InetPtonW(Family, pszAddrString, pAddrBuf)
; Family : INT -> "int"
; pszAddrString : LPCWSTR -> "wstr"
; pAddrBuf : void* out -> "intptr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
procInetPtonW = ws2_32.NewProc("InetPtonW")
)
// Family (INT), pszAddrString (LPCWSTR), pAddrBuf (void* out)
r1, _, err := procInetPtonW.Call(
uintptr(Family),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszAddrString))),
uintptr(pAddrBuf),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // INTfunction InetPtonW(
Family: Integer; // INT
pszAddrString: PWideChar; // LPCWSTR
pAddrBuf: Pointer // void* out
): Integer; stdcall;
external 'WS2_32.dll' name 'InetPtonW';result := DllCall("WS2_32\InetPtonW"
, "Int", Family ; INT
, "WStr", pszAddrString ; LPCWSTR
, "Ptr", pAddrBuf ; void* out
, "Int") ; return: INT●InetPtonW(Family, pszAddrString, pAddrBuf) = DLL("WS2_32.dll", "int InetPtonW(int, char*, void*)")
# 呼び出し: InetPtonW(Family, pszAddrString, pAddrBuf)
# Family : INT -> "int"
# pszAddrString : LPCWSTR -> "char*"
# pAddrBuf : void* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "ws2_32" fn InetPtonW(
Family: i32, // INT
pszAddrString: [*c]const u16, // LPCWSTR
pAddrBuf: ?*anyopaque // void* out
) callconv(std.os.windows.WINAPI) i32;proc InetPtonW(
Family: int32, # INT
pszAddrString: WideCString, # LPCWSTR
pAddrBuf: pointer # void* out
): int32 {.importc: "InetPtonW", stdcall, dynlib: "WS2_32.dll".}pragma(lib, "ws2_32");
extern(Windows)
int InetPtonW(
int Family, // INT
const(wchar)* pszAddrString, // LPCWSTR
void* pAddrBuf // void* out
);ccall((:InetPtonW, "WS2_32.dll"), stdcall, Int32,
(Int32, Cwstring, Ptr{Cvoid}),
Family, pszAddrString, pAddrBuf)
# Family : INT -> Int32
# pszAddrString : LPCWSTR -> Cwstring
# pAddrBuf : void* out -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t InetPtonW(
int32_t Family,
const uint16_t* pszAddrString,
void* pAddrBuf);
]]
local ws2_32 = ffi.load("ws2_32")
-- ws2_32.InetPtonW(Family, pszAddrString, pAddrBuf)
-- Family : INT
-- pszAddrString : LPCWSTR
-- pAddrBuf : void* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('WS2_32.dll');
const InetPtonW = lib.func('__stdcall', 'InetPtonW', 'int32_t', ['int32_t', 'str16', 'void *']);
// InetPtonW(Family, pszAddrString, pAddrBuf)
// Family : INT -> 'int32_t'
// pszAddrString : LPCWSTR -> 'str16'
// pAddrBuf : void* out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("WS2_32.dll", {
InetPtonW: { parameters: ["i32", "buffer", "pointer"], result: "i32" },
});
// lib.symbols.InetPtonW(Family, pszAddrString, pAddrBuf)
// Family : INT -> "i32"
// pszAddrString : LPCWSTR -> "buffer"
// pAddrBuf : void* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t InetPtonW(
int32_t Family,
const uint16_t* pszAddrString,
void* pAddrBuf);
C, "WS2_32.dll");
// $ffi->InetPtonW(Family, pszAddrString, pAddrBuf);
// Family : INT
// pszAddrString : LPCWSTR
// pAddrBuf : void* 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 InetPtonW(
int Family, // INT
WString pszAddrString, // LPCWSTR
Pointer pAddrBuf // void* out
);
}@[Link("ws2_32")]
lib LibWS2_32
fun InetPtonW = InetPtonW(
Family : Int32, # INT
pszAddrString : UInt16*, # LPCWSTR
pAddrBuf : Void* # void* out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef InetPtonWNative = Int32 Function(Int32, Pointer<Utf16>, Pointer<Void>);
typedef InetPtonWDart = int Function(int, Pointer<Utf16>, Pointer<Void>);
final InetPtonW = DynamicLibrary.open('WS2_32.dll')
.lookupFunction<InetPtonWNative, InetPtonWDart>('InetPtonW');
// Family : INT -> Int32
// pszAddrString : LPCWSTR -> Pointer<Utf16>
// pAddrBuf : void* out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function InetPtonW(
Family: Integer; // INT
pszAddrString: PWideChar; // LPCWSTR
pAddrBuf: Pointer // void* out
): Integer; stdcall;
external 'WS2_32.dll' name 'InetPtonW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "InetPtonW"
c_InetPtonW :: Int32 -> CWString -> Ptr () -> IO Int32
-- Family : INT -> Int32
-- pszAddrString : LPCWSTR -> CWString
-- pAddrBuf : void* out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let inetptonw =
foreign "InetPtonW"
(int32_t @-> (ptr uint16_t) @-> (ptr void) @-> returning int32_t)
(* Family : INT -> int32_t *)
(* pszAddrString : LPCWSTR -> (ptr uint16_t) *)
(* pAddrBuf : void* out -> (ptr void) *)
(* 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 ("InetPtonW" inet-pton-w :convention :stdcall) :int32
(family :int32) ; INT
(psz-addr-string (:string :encoding :utf-16le)) ; LPCWSTR
(p-addr-buf :pointer)) ; void* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $InetPtonW = Win32::API::More->new('WS2_32',
'int InetPtonW(int Family, LPCWSTR pszAddrString, LPVOID pAddrBuf)');
# my $ret = $InetPtonW->Call($Family, $pszAddrString, $pAddrBuf);
# Family : INT -> int
# pszAddrString : LPCWSTR -> LPCWSTR
# pAddrBuf : void* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- s IN_ADDR
- f InetNtopW — バイナリ形式のIPアドレスをUnicode文字列形式に変換する。
- f RtlIpv4AddressToStringW — IPv4アドレスをドット区切り文字列へ変換する。
- f RtlIpv4AddressToStringExW — IPv4アドレスとポートを文字列へ変換する。
- f RtlIpv4StringToAddressW — 文字列をIPv4アドレス構造体へ変換する。
- f RtlIpv4StringToAddressExW — 文字列をIPv4アドレスとポートへ変換する。
- f RtlIpv6AddressToStringW — IPv6アドレスを文字列表現へ変換する。
- f RtlIpv6AddressToStringExW — IPv6アドレスとスコープ・ポートを文字列へ変換する。
- f RtlIpv6StringToAddressW — 文字列をIPv6アドレス構造体へ変換する。
- f RtlIpv6StringToAddressExW — 文字列をIPv6アドレスとスコープ・ポートへ変換する。
- f inet_addr — IPアドレス文字列を32ビットアドレスへ変換する。
- f inet_ntoa — IPアドレスをドット区切りの文字列へ変換する。