ホーム › Networking.WinSock › inet_pton
inet_pton
関数IPv4/IPv6のANSI文字列形式アドレスをバイナリ形式に変換する。
シグネチャ
// WS2_32.dll
#include <windows.h>
INT inet_pton(
INT Family,
LPCSTR pszAddrString,
void* pAddrBuf
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| Family | INT | in |
| pszAddrString | LPCSTR | in |
| pAddrBuf | void* | out |
戻り値の型: INT
各言語での呼び出し定義
// WS2_32.dll
#include <windows.h>
INT inet_pton(
INT Family,
LPCSTR pszAddrString,
void* pAddrBuf
);[DllImport("WS2_32.dll", SetLastError = true, ExactSpelling = true)]
static extern int inet_pton(
int Family, // INT
[MarshalAs(UnmanagedType.LPStr)] string pszAddrString, // LPCSTR
IntPtr pAddrBuf // void* out
);<DllImport("WS2_32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function inet_pton(
Family As Integer, ' INT
<MarshalAs(UnmanagedType.LPStr)> pszAddrString As String, ' LPCSTR
pAddrBuf As IntPtr ' void* out
) As Integer
End Function' Family : INT
' pszAddrString : LPCSTR
' pAddrBuf : void* out
Declare PtrSafe Function inet_pton Lib "ws2_32" ( _
ByVal Family As Long, _
ByVal pszAddrString As String, _
ByVal pAddrBuf As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
inet_pton = ctypes.windll.ws2_32.inet_pton
inet_pton.restype = ctypes.c_int
inet_pton.argtypes = [
ctypes.c_int, # Family : INT
wintypes.LPCSTR, # pszAddrString : LPCSTR
ctypes.POINTER(None), # pAddrBuf : void* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('WS2_32.dll')
inet_pton = Fiddle::Function.new(
lib['inet_pton'],
[
Fiddle::TYPE_INT, # Family : INT
Fiddle::TYPE_VOIDP, # pszAddrString : LPCSTR
Fiddle::TYPE_VOIDP, # pAddrBuf : void* out
],
Fiddle::TYPE_INT)#[link(name = "ws2_32")]
extern "system" {
fn inet_pton(
Family: i32, // INT
pszAddrString: *const u8, // LPCSTR
pAddrBuf: *mut () // void* out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("WS2_32.dll", SetLastError = true)]
public static extern int inet_pton(int Family, [MarshalAs(UnmanagedType.LPStr)] string pszAddrString, IntPtr pAddrBuf);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_inet_pton' -Namespace Win32 -PassThru
# $api::inet_pton(Family, pszAddrString, pAddrBuf)#uselib "WS2_32.dll"
#func global inet_pton "inet_pton" sptr, sptr, sptr
; inet_pton Family, pszAddrString, pAddrBuf ; 戻り値は stat
; Family : INT -> "sptr"
; pszAddrString : LPCSTR -> "sptr"
; pAddrBuf : void* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "WS2_32.dll"
#cfunc global inet_pton "inet_pton" int, str, sptr
; res = inet_pton(Family, pszAddrString, pAddrBuf)
; Family : INT -> "int"
; pszAddrString : LPCSTR -> "str"
; pAddrBuf : void* out -> "sptr"; INT inet_pton(INT Family, LPCSTR pszAddrString, void* pAddrBuf)
#uselib "WS2_32.dll"
#cfunc global inet_pton "inet_pton" int, str, intptr
; res = inet_pton(Family, pszAddrString, pAddrBuf)
; Family : INT -> "int"
; pszAddrString : LPCSTR -> "str"
; pAddrBuf : void* out -> "intptr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
procinet_pton = ws2_32.NewProc("inet_pton")
)
// Family (INT), pszAddrString (LPCSTR), pAddrBuf (void* out)
r1, _, err := procinet_pton.Call(
uintptr(Family),
uintptr(unsafe.Pointer(windows.BytePtrFromString(pszAddrString))),
uintptr(pAddrBuf),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // INTfunction inet_pton(
Family: Integer; // INT
pszAddrString: PAnsiChar; // LPCSTR
pAddrBuf: Pointer // void* out
): Integer; stdcall;
external 'WS2_32.dll' name 'inet_pton';result := DllCall("WS2_32\inet_pton"
, "Int", Family ; INT
, "AStr", pszAddrString ; LPCSTR
, "Ptr", pAddrBuf ; void* out
, "Int") ; return: INT●inet_pton(Family, pszAddrString, pAddrBuf) = DLL("WS2_32.dll", "int inet_pton(int, char*, void*)")
# 呼び出し: inet_pton(Family, pszAddrString, pAddrBuf)
# Family : INT -> "int"
# pszAddrString : LPCSTR -> "char*"
# pAddrBuf : void* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。