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

gethostbyaddr

関数
IPアドレスから対応するホスト情報を取得する。
DLLWS2_32.dll呼出規約winapiSetLastErrorあり対応OSWindows 8.1 以降

シグネチャ

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

HOSTENT* gethostbyaddr(
    LPCSTR addr,
    INT len,
    INT type
);

パラメーター

名前方向
addrLPCSTRin
lenINTin
typeINTin

戻り値の型: HOSTENT*

各言語での呼び出し定義

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

HOSTENT* gethostbyaddr(
    LPCSTR addr,
    INT len,
    INT type
);
[DllImport("WS2_32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr gethostbyaddr(
    [MarshalAs(UnmanagedType.LPStr)] string addr,   // LPCSTR
    int len,   // INT
    int type   // INT
);
<DllImport("WS2_32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function gethostbyaddr(
    <MarshalAs(UnmanagedType.LPStr)> addr As String,   ' LPCSTR
    len As Integer,   ' INT
    type As Integer   ' INT
) As IntPtr
End Function
' addr : LPCSTR
' len : INT
' type : INT
Declare PtrSafe Function gethostbyaddr Lib "ws2_32" ( _
    ByVal addr As String, _
    ByVal len As Long, _
    ByVal type As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

gethostbyaddr = ctypes.windll.ws2_32.gethostbyaddr
gethostbyaddr.restype = ctypes.c_void_p
gethostbyaddr.argtypes = [
    wintypes.LPCSTR,  # addr : LPCSTR
    ctypes.c_int,  # len : INT
    ctypes.c_int,  # type : INT
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WS2_32.dll')
gethostbyaddr = Fiddle::Function.new(
  lib['gethostbyaddr'],
  [
    Fiddle::TYPE_VOIDP,  # addr : LPCSTR
    Fiddle::TYPE_INT,  # len : INT
    Fiddle::TYPE_INT,  # type : INT
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "ws2_32")]
extern "system" {
    fn gethostbyaddr(
        addr: *const u8,  // LPCSTR
        len: i32,  // INT
        type: i32  // INT
    ) -> *mut HOSTENT;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("WS2_32.dll", SetLastError = true)]
public static extern IntPtr gethostbyaddr([MarshalAs(UnmanagedType.LPStr)] string addr, int len, int type);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_gethostbyaddr' -Namespace Win32 -PassThru
# $api::gethostbyaddr(addr, len, type)
#uselib "WS2_32.dll"
#func global gethostbyaddr "gethostbyaddr" sptr, sptr, sptr
; gethostbyaddr addr, len, type   ; 戻り値は stat
; addr : LPCSTR -> "sptr"
; len : INT -> "sptr"
; type : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "WS2_32.dll"
#cfunc global gethostbyaddr "gethostbyaddr" str, int, int
; res = gethostbyaddr(addr, len, type)
; addr : LPCSTR -> "str"
; len : INT -> "int"
; type : INT -> "int"
; HOSTENT* gethostbyaddr(LPCSTR addr, INT len, INT type)
#uselib "WS2_32.dll"
#cfunc global gethostbyaddr "gethostbyaddr" str, int, int
; res = gethostbyaddr(addr, len, type)
; addr : LPCSTR -> "str"
; len : INT -> "int"
; type : INT -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
	procgethostbyaddr = ws2_32.NewProc("gethostbyaddr")
)

// addr (LPCSTR), len (INT), type (INT)
r1, _, err := procgethostbyaddr.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(addr))),
	uintptr(len),
	uintptr(type),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HOSTENT*
function gethostbyaddr(
  addr: PAnsiChar;   // LPCSTR
  len: Integer;   // INT
  type: Integer   // INT
): Pointer; stdcall;
  external 'WS2_32.dll' name 'gethostbyaddr';
result := DllCall("WS2_32\gethostbyaddr"
    , "AStr", addr   ; LPCSTR
    , "Int", len   ; INT
    , "Int", type   ; INT
    , "Ptr")   ; return: HOSTENT*
●gethostbyaddr(addr, len, type) = DLL("WS2_32.dll", "void* gethostbyaddr(char*, int, int)")
# 呼び出し: gethostbyaddr(addr, len, type)
# addr : LPCSTR -> "char*"
# len : INT -> "int"
# type : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。