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

InetPtonW

関数
IPv4/IPv6のUnicode文字列形式アドレスをバイナリ形式に変換する。
DLLWS2_32.dll呼出規約winapi対応OSWindows 8.1 以降

シグネチャ

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

INT InetPtonW(
    INT Family,
    LPCWSTR pszAddrString,
    void* pAddrBuf
);

パラメーター

名前方向
FamilyINTin
pszAddrStringLPCWSTRin
pAddrBufvoid*out

戻り値の型: INT

各言語での呼び出し定義

// 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   // INT
function 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)。