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

WSAAddressToStringA

関数
ソケットアドレス構造体を文字列表現へ変換する。
DLLWS2_32.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows 8.1 以降

シグネチャ

// WS2_32.dll  (ANSI / -A)
#include <windows.h>

INT WSAAddressToStringA(
    SOCKADDR* lpsaAddress,
    DWORD dwAddressLength,
    WSAPROTOCOL_INFOA* lpProtocolInfo,   // optional
    LPSTR lpszAddressString,
    DWORD* lpdwAddressStringLength
);

パラメーター

名前方向説明
lpsaAddressSOCKADDR*in文字列に変換する sockaddr 構造体へのポインターです。
dwAddressLengthDWORDinlpsaAddress パラメーターが指す sockaddr 構造体内のアドレスの長さ(バイト単位)です。dwAddressLength パラメーターのサイズはプロトコルによって異なる場合があります。
lpProtocolInfoWSAPROTOCOL_INFOA*inoptional特定のプロバイダーの WSAPROTOCOL_INFO 構造体へのポインターです。このパラメーターが NULL の場合、呼び出しは lpsaAddress パラメーターで示されるアドレスファミリーをサポートする最初のプロトコルのプロバイダーにルーティングされます。
lpszAddressStringLPSTRout人間が読めるアドレス文字列を受け取るバッファーへのポインターです。
lpdwAddressStringLengthDWORD*inout入力時には、このパラメーターは lpszAddressString パラメーターが指すバッファーの長さを指定します。長さは ANSI 文字列ではバイト単位、Unicode 文字列では WCHAR 単位で表されます。出力時には、このパラメーターは lpszAddressString パラメーターが指すバッファーに実際にコピーされた文字列の長さ(NULL 終端文字を含む)を返します。指定したバッファーが十分に大きくない場合、関数は WSAEFAULT という特定のエラーで失敗し、このパラメーターには必要なサイズが設定されます。

戻り値の型: INT

公式ドキュメント

sockaddr 構造体のすべての要素を、アドレスの人間が読める文字列表現に変換します。(ANSI)

戻り値

エラーが発生しない場合、 WSAAddressToString は 0 を返します。それ以外の場合は SOCKET_ERROR が返され、 WSAGetLastError を呼び出すことで特定のエラー番号を取得できます。

エラーコード 意味
WSAEFAULT
指定された lpcsAddresslpProtocolInfolpszAddressString パラメーターが、プロセスのアドレス空間にすべて含まれていないメモリを指しているか、または lpszAddressString パラメーターが指すバッファーが小さすぎます。より大きなバッファーを渡してください。
WSAEINVAL
無効なパラメーターが渡されました。このエラーは、lpsaAddressdwAddressLength、または lpdwAddressStringLength パラメーターが NULL の場合に返されます。また、指定したアドレスが有効なソケットアドレスでない場合や、示されたアドレスファミリーをサポートするトランスポートプロバイダーが存在しない場合にも返されます。
WSAENOBUFS
利用可能なバッファー領域がありません。
WSANOTINITIALISED
Winsock 2 DLL が初期化されていません。アプリケーションは、Windows Sockets 関数を呼び出す前に、まず WSAStartup を呼び出す必要があります。

解説(Remarks)

WSAAddressToString 関数は、プロトコルに依存しないアドレスから文字列への変換を提供します。 WSAAddressToString 関数は、lpsaAddress パラメーターが指すソケットアドレス構造体を受け取り、lpszAddressString パラメーターでそのソケットアドレスを表す NULL 終端文字列へのポインターを返します。inet_ntoa 関数が IPv4 アドレスのみを扱うのに対し、WSAAddressToString 関数は、IPv6 アドレスを含め、ローカルコンピューター上の Winsock プロバイダーがサポートする任意のソケットアドレスを扱えます。

lpsaAddress パラメーターが IPv4 ソケットアドレス(アドレスファミリーが AF_INET)を指している場合、lpszAddressString パラメーターが指すバッファーに返されるアドレス文字列は、ドット区切り 10 進表記(例えば "192.168.16.0" はドット区切り 10 進表記による IPv4 アドレスの一例です)になります。

lpsaAddress パラメーターが IPv6 ソケットアドレス(アドレスファミリーが AF_INET6)を指している場合、lpszAddressString パラメーターが指すバッファーに返されるアドレス文字列は、インターネット標準形式になります。基本的な文字列表現は、コロンで区切られた 8 つの 16 進数で構成されます。連続するゼロの並びはダブルコロンで置き換えられます。IPv6 アドレスの文字列表現中にダブルコロンを含められるのは 1 か所だけです。

lpszAddressString パラメーターが指すバッファーの長さが、ソケットアドレスの文字列表現を受け取るのに十分でない場合、WSAAddressToStringWSAEFAULT を返します。

WSAAddressToString 関数による IPv6 アドレスのサポートは、Windows XP Service Pack 1 (SP1) 以降で追加されました。WSAAddressToString 関数が IPv6 アドレスをサポートするには、ローカルコンピューターに IPv6 もインストールされている必要があります。

Windows Phone 8: WSAAddressToStringW 関数は、Windows Phone 8 以降の Windows Phone ストアアプリでサポートされています。

Windows 8.1 および Windows Server 2012 R2: WSAAddressToStringW 関数は、Windows 8.1、Windows Server 2012 R2 以降の Windows ストアアプリでサポートされています。

メモ

winsock2.h ヘッダーは、WSAAddressToString を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義しています。エンコーディング中立のエイリアスの使用と、エンコーディング中立でないコードを混在させると、不一致が生じ、コンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、関数プロトタイプの規則を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// WS2_32.dll  (ANSI / -A)
#include <windows.h>

INT WSAAddressToStringA(
    SOCKADDR* lpsaAddress,
    DWORD dwAddressLength,
    WSAPROTOCOL_INFOA* lpProtocolInfo,   // optional
    LPSTR lpszAddressString,
    DWORD* lpdwAddressStringLength
);
[DllImport("WS2_32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern int WSAAddressToStringA(
    IntPtr lpsaAddress,   // SOCKADDR*
    uint dwAddressLength,   // DWORD
    IntPtr lpProtocolInfo,   // WSAPROTOCOL_INFOA* optional
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpszAddressString,   // LPSTR out
    ref uint lpdwAddressStringLength   // DWORD* in/out
);
<DllImport("WS2_32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WSAAddressToStringA(
    lpsaAddress As IntPtr,   ' SOCKADDR*
    dwAddressLength As UInteger,   ' DWORD
    lpProtocolInfo As IntPtr,   ' WSAPROTOCOL_INFOA* optional
    <MarshalAs(UnmanagedType.LPStr)> lpszAddressString As System.Text.StringBuilder,   ' LPSTR out
    ByRef lpdwAddressStringLength As UInteger   ' DWORD* in/out
) As Integer
End Function
' lpsaAddress : SOCKADDR*
' dwAddressLength : DWORD
' lpProtocolInfo : WSAPROTOCOL_INFOA* optional
' lpszAddressString : LPSTR out
' lpdwAddressStringLength : DWORD* in/out
Declare PtrSafe Function WSAAddressToStringA Lib "ws2_32" ( _
    ByVal lpsaAddress As LongPtr, _
    ByVal dwAddressLength As Long, _
    ByVal lpProtocolInfo As LongPtr, _
    ByVal lpszAddressString As String, _
    ByRef lpdwAddressStringLength As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WSAAddressToStringA = ctypes.windll.ws2_32.WSAAddressToStringA
WSAAddressToStringA.restype = ctypes.c_int
WSAAddressToStringA.argtypes = [
    ctypes.c_void_p,  # lpsaAddress : SOCKADDR*
    wintypes.DWORD,  # dwAddressLength : DWORD
    ctypes.c_void_p,  # lpProtocolInfo : WSAPROTOCOL_INFOA* optional
    wintypes.LPSTR,  # lpszAddressString : LPSTR out
    ctypes.POINTER(wintypes.DWORD),  # lpdwAddressStringLength : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WS2_32.dll')
WSAAddressToStringA = Fiddle::Function.new(
  lib['WSAAddressToStringA'],
  [
    Fiddle::TYPE_VOIDP,  # lpsaAddress : SOCKADDR*
    -Fiddle::TYPE_INT,  # dwAddressLength : DWORD
    Fiddle::TYPE_VOIDP,  # lpProtocolInfo : WSAPROTOCOL_INFOA* optional
    Fiddle::TYPE_VOIDP,  # lpszAddressString : LPSTR out
    Fiddle::TYPE_VOIDP,  # lpdwAddressStringLength : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "ws2_32")]
extern "system" {
    fn WSAAddressToStringA(
        lpsaAddress: *mut SOCKADDR,  // SOCKADDR*
        dwAddressLength: u32,  // DWORD
        lpProtocolInfo: *mut WSAPROTOCOL_INFOA,  // WSAPROTOCOL_INFOA* optional
        lpszAddressString: *mut u8,  // LPSTR out
        lpdwAddressStringLength: *mut u32  // DWORD* in/out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("WS2_32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern int WSAAddressToStringA(IntPtr lpsaAddress, uint dwAddressLength, IntPtr lpProtocolInfo, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpszAddressString, ref uint lpdwAddressStringLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_WSAAddressToStringA' -Namespace Win32 -PassThru
# $api::WSAAddressToStringA(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength)
#uselib "WS2_32.dll"
#func global WSAAddressToStringA "WSAAddressToStringA" sptr, sptr, sptr, sptr, sptr
; WSAAddressToStringA varptr(lpsaAddress), dwAddressLength, varptr(lpProtocolInfo), varptr(lpszAddressString), varptr(lpdwAddressStringLength)   ; 戻り値は stat
; lpsaAddress : SOCKADDR* -> "sptr"
; dwAddressLength : DWORD -> "sptr"
; lpProtocolInfo : WSAPROTOCOL_INFOA* optional -> "sptr"
; lpszAddressString : LPSTR out -> "sptr"
; lpdwAddressStringLength : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "WS2_32.dll"
#cfunc global WSAAddressToStringA "WSAAddressToStringA" var, int, var, var, var
; res = WSAAddressToStringA(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength)
; lpsaAddress : SOCKADDR* -> "var"
; dwAddressLength : DWORD -> "int"
; lpProtocolInfo : WSAPROTOCOL_INFOA* optional -> "var"
; lpszAddressString : LPSTR out -> "var"
; lpdwAddressStringLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT WSAAddressToStringA(SOCKADDR* lpsaAddress, DWORD dwAddressLength, WSAPROTOCOL_INFOA* lpProtocolInfo, LPSTR lpszAddressString, DWORD* lpdwAddressStringLength)
#uselib "WS2_32.dll"
#cfunc global WSAAddressToStringA "WSAAddressToStringA" var, int, var, var, var
; res = WSAAddressToStringA(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength)
; lpsaAddress : SOCKADDR* -> "var"
; dwAddressLength : DWORD -> "int"
; lpProtocolInfo : WSAPROTOCOL_INFOA* optional -> "var"
; lpszAddressString : LPSTR out -> "var"
; lpdwAddressStringLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
	procWSAAddressToStringA = ws2_32.NewProc("WSAAddressToStringA")
)

// lpsaAddress (SOCKADDR*), dwAddressLength (DWORD), lpProtocolInfo (WSAPROTOCOL_INFOA* optional), lpszAddressString (LPSTR out), lpdwAddressStringLength (DWORD* in/out)
r1, _, err := procWSAAddressToStringA.Call(
	uintptr(lpsaAddress),
	uintptr(dwAddressLength),
	uintptr(lpProtocolInfo),
	uintptr(lpszAddressString),
	uintptr(lpdwAddressStringLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function WSAAddressToStringA(
  lpsaAddress: Pointer;   // SOCKADDR*
  dwAddressLength: DWORD;   // DWORD
  lpProtocolInfo: Pointer;   // WSAPROTOCOL_INFOA* optional
  lpszAddressString: PAnsiChar;   // LPSTR out
  lpdwAddressStringLength: Pointer   // DWORD* in/out
): Integer; stdcall;
  external 'WS2_32.dll' name 'WSAAddressToStringA';
result := DllCall("WS2_32\WSAAddressToStringA"
    , "Ptr", lpsaAddress   ; SOCKADDR*
    , "UInt", dwAddressLength   ; DWORD
    , "Ptr", lpProtocolInfo   ; WSAPROTOCOL_INFOA* optional
    , "Ptr", lpszAddressString   ; LPSTR out
    , "Ptr", lpdwAddressStringLength   ; DWORD* in/out
    , "Int")   ; return: INT
●WSAAddressToStringA(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength) = DLL("WS2_32.dll", "int WSAAddressToStringA(void*, dword, void*, char*, void*)")
# 呼び出し: WSAAddressToStringA(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength)
# lpsaAddress : SOCKADDR* -> "void*"
# dwAddressLength : DWORD -> "dword"
# lpProtocolInfo : WSAPROTOCOL_INFOA* optional -> "void*"
# lpszAddressString : LPSTR out -> "char*"
# lpdwAddressStringLength : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "ws2_32" fn WSAAddressToStringA(
    lpsaAddress: [*c]SOCKADDR, // SOCKADDR*
    dwAddressLength: u32, // DWORD
    lpProtocolInfo: [*c]WSAPROTOCOL_INFOA, // WSAPROTOCOL_INFOA* optional
    lpszAddressString: [*c]u8, // LPSTR out
    lpdwAddressStringLength: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;
proc WSAAddressToStringA(
    lpsaAddress: ptr SOCKADDR,  # SOCKADDR*
    dwAddressLength: uint32,  # DWORD
    lpProtocolInfo: ptr WSAPROTOCOL_INFOA,  # WSAPROTOCOL_INFOA* optional
    lpszAddressString: ptr char,  # LPSTR out
    lpdwAddressStringLength: ptr uint32  # DWORD* in/out
): int32 {.importc: "WSAAddressToStringA", stdcall, dynlib: "WS2_32.dll".}
pragma(lib, "ws2_32");
extern(Windows)
int WSAAddressToStringA(
    SOCKADDR* lpsaAddress,   // SOCKADDR*
    uint dwAddressLength,   // DWORD
    WSAPROTOCOL_INFOA* lpProtocolInfo,   // WSAPROTOCOL_INFOA* optional
    char* lpszAddressString,   // LPSTR out
    uint* lpdwAddressStringLength   // DWORD* in/out
);
ccall((:WSAAddressToStringA, "WS2_32.dll"), stdcall, Int32,
      (Ptr{SOCKADDR}, UInt32, Ptr{WSAPROTOCOL_INFOA}, Ptr{UInt8}, Ptr{UInt32}),
      lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength)
# lpsaAddress : SOCKADDR* -> Ptr{SOCKADDR}
# dwAddressLength : DWORD -> UInt32
# lpProtocolInfo : WSAPROTOCOL_INFOA* optional -> Ptr{WSAPROTOCOL_INFOA}
# lpszAddressString : LPSTR out -> Ptr{UInt8}
# lpdwAddressStringLength : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t WSAAddressToStringA(
    void* lpsaAddress,
    uint32_t dwAddressLength,
    void* lpProtocolInfo,
    char* lpszAddressString,
    uint32_t* lpdwAddressStringLength);
]]
local ws2_32 = ffi.load("ws2_32")
-- ws2_32.WSAAddressToStringA(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength)
-- lpsaAddress : SOCKADDR*
-- dwAddressLength : DWORD
-- lpProtocolInfo : WSAPROTOCOL_INFOA* optional
-- lpszAddressString : LPSTR out
-- lpdwAddressStringLength : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('WS2_32.dll');
const WSAAddressToStringA = lib.func('__stdcall', 'WSAAddressToStringA', 'int32_t', ['void *', 'uint32_t', 'void *', 'char *', 'uint32_t *']);
// WSAAddressToStringA(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength)
// lpsaAddress : SOCKADDR* -> 'void *'
// dwAddressLength : DWORD -> 'uint32_t'
// lpProtocolInfo : WSAPROTOCOL_INFOA* optional -> 'void *'
// lpszAddressString : LPSTR out -> 'char *'
// lpdwAddressStringLength : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WS2_32.dll", {
  WSAAddressToStringA: { parameters: ["pointer", "u32", "pointer", "buffer", "pointer"], result: "i32" },
});
// lib.symbols.WSAAddressToStringA(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength)
// lpsaAddress : SOCKADDR* -> "pointer"
// dwAddressLength : DWORD -> "u32"
// lpProtocolInfo : WSAPROTOCOL_INFOA* optional -> "pointer"
// lpszAddressString : LPSTR out -> "buffer"
// lpdwAddressStringLength : DWORD* in/out -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t WSAAddressToStringA(
    void* lpsaAddress,
    uint32_t dwAddressLength,
    void* lpProtocolInfo,
    char* lpszAddressString,
    uint32_t* lpdwAddressStringLength);
C, "WS2_32.dll");
// $ffi->WSAAddressToStringA(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength);
// lpsaAddress : SOCKADDR*
// dwAddressLength : DWORD
// lpProtocolInfo : WSAPROTOCOL_INFOA* optional
// lpszAddressString : LPSTR out
// lpdwAddressStringLength : DWORD* in/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, W32APIOptions.ASCII_OPTIONS);
    int WSAAddressToStringA(
        Pointer lpsaAddress,   // SOCKADDR*
        int dwAddressLength,   // DWORD
        Pointer lpProtocolInfo,   // WSAPROTOCOL_INFOA* optional
        byte[] lpszAddressString,   // LPSTR out
        IntByReference lpdwAddressStringLength   // DWORD* in/out
    );
}
@[Link("ws2_32")]
lib LibWS2_32
  fun WSAAddressToStringA = WSAAddressToStringA(
    lpsaAddress : SOCKADDR*,   # SOCKADDR*
    dwAddressLength : UInt32,   # DWORD
    lpProtocolInfo : WSAPROTOCOL_INFOA*,   # WSAPROTOCOL_INFOA* optional
    lpszAddressString : UInt8*,   # LPSTR out
    lpdwAddressStringLength : UInt32*   # DWORD* in/out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef WSAAddressToStringANative = Int32 Function(Pointer<Void>, Uint32, Pointer<Void>, Pointer<Utf8>, Pointer<Uint32>);
typedef WSAAddressToStringADart = int Function(Pointer<Void>, int, Pointer<Void>, Pointer<Utf8>, Pointer<Uint32>);
final WSAAddressToStringA = DynamicLibrary.open('WS2_32.dll')
    .lookupFunction<WSAAddressToStringANative, WSAAddressToStringADart>('WSAAddressToStringA');
// lpsaAddress : SOCKADDR* -> Pointer<Void>
// dwAddressLength : DWORD -> Uint32
// lpProtocolInfo : WSAPROTOCOL_INFOA* optional -> Pointer<Void>
// lpszAddressString : LPSTR out -> Pointer<Utf8>
// lpdwAddressStringLength : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WSAAddressToStringA(
  lpsaAddress: Pointer;   // SOCKADDR*
  dwAddressLength: DWORD;   // DWORD
  lpProtocolInfo: Pointer;   // WSAPROTOCOL_INFOA* optional
  lpszAddressString: PAnsiChar;   // LPSTR out
  lpdwAddressStringLength: Pointer   // DWORD* in/out
): Integer; stdcall;
  external 'WS2_32.dll' name 'WSAAddressToStringA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WSAAddressToStringA"
  c_WSAAddressToStringA :: Ptr () -> Word32 -> Ptr () -> CString -> Ptr Word32 -> IO Int32
-- lpsaAddress : SOCKADDR* -> Ptr ()
-- dwAddressLength : DWORD -> Word32
-- lpProtocolInfo : WSAPROTOCOL_INFOA* optional -> Ptr ()
-- lpszAddressString : LPSTR out -> CString
-- lpdwAddressStringLength : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let wsaaddresstostringa =
  foreign "WSAAddressToStringA"
    ((ptr void) @-> uint32_t @-> (ptr void) @-> string @-> (ptr uint32_t) @-> returning int32_t)
(* lpsaAddress : SOCKADDR* -> (ptr void) *)
(* dwAddressLength : DWORD -> uint32_t *)
(* lpProtocolInfo : WSAPROTOCOL_INFOA* optional -> (ptr void) *)
(* lpszAddressString : LPSTR out -> string *)
(* lpdwAddressStringLength : DWORD* in/out -> (ptr uint32_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 ("WSAAddressToStringA" wsaaddress-to-string-a :convention :stdcall) :int32
  (lpsa-address :pointer)   ; SOCKADDR*
  (dw-address-length :uint32)   ; DWORD
  (lp-protocol-info :pointer)   ; WSAPROTOCOL_INFOA* optional
  (lpsz-address-string :pointer)   ; LPSTR out
  (lpdw-address-string-length :pointer))   ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WSAAddressToStringA = Win32::API::More->new('WS2_32',
    'int WSAAddressToStringA(LPVOID lpsaAddress, DWORD dwAddressLength, LPVOID lpProtocolInfo, LPSTR lpszAddressString, LPVOID lpdwAddressStringLength)');
# my $ret = $WSAAddressToStringA->Call($lpsaAddress, $dwAddressLength, $lpProtocolInfo, $lpszAddressString, $lpdwAddressStringLength);
# lpsaAddress : SOCKADDR* -> LPVOID
# dwAddressLength : DWORD -> DWORD
# lpProtocolInfo : WSAPROTOCOL_INFOA* optional -> LPVOID
# lpszAddressString : LPSTR out -> LPSTR
# lpdwAddressStringLength : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い
公式の関連項目
使用する型