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

WSAAddressToStringW

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

シグネチャ

// WS2_32.dll  (Unicode / -W)
#include <windows.h>

INT WSAAddressToStringW(
    SOCKADDR* lpsaAddress,
    DWORD dwAddressLength,
    WSAPROTOCOL_INFOW* lpProtocolInfo,   // optional
    LPWSTR lpszAddressString,
    DWORD* lpdwAddressStringLength
);

パラメーター

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

戻り値の型: INT

公式ドキュメント

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

戻り値

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

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

解説(Remarks)

WSAAddressToString 関数は、プロトコルに依存しないアドレスから文字列への変換を提供します。 WSAAddressToString 関数は、lpsaAddress パラメーターが指すソケットアドレス構造体を受け取り、そのソケットアドレスを表す NULL 終端文字列へのポインターを lpszAddressString パラメーターに返します。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 with 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  (Unicode / -W)
#include <windows.h>

INT WSAAddressToStringW(
    SOCKADDR* lpsaAddress,
    DWORD dwAddressLength,
    WSAPROTOCOL_INFOW* lpProtocolInfo,   // optional
    LPWSTR lpszAddressString,
    DWORD* lpdwAddressStringLength
);
[DllImport("WS2_32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern int WSAAddressToStringW(
    IntPtr lpsaAddress,   // SOCKADDR*
    uint dwAddressLength,   // DWORD
    IntPtr lpProtocolInfo,   // WSAPROTOCOL_INFOW* optional
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpszAddressString,   // LPWSTR out
    ref uint lpdwAddressStringLength   // DWORD* in/out
);
<DllImport("WS2_32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WSAAddressToStringW(
    lpsaAddress As IntPtr,   ' SOCKADDR*
    dwAddressLength As UInteger,   ' DWORD
    lpProtocolInfo As IntPtr,   ' WSAPROTOCOL_INFOW* optional
    <MarshalAs(UnmanagedType.LPWStr)> lpszAddressString As System.Text.StringBuilder,   ' LPWSTR out
    ByRef lpdwAddressStringLength As UInteger   ' DWORD* in/out
) As Integer
End Function
' lpsaAddress : SOCKADDR*
' dwAddressLength : DWORD
' lpProtocolInfo : WSAPROTOCOL_INFOW* optional
' lpszAddressString : LPWSTR out
' lpdwAddressStringLength : DWORD* in/out
Declare PtrSafe Function WSAAddressToStringW Lib "ws2_32" ( _
    ByVal lpsaAddress As LongPtr, _
    ByVal dwAddressLength As Long, _
    ByVal lpProtocolInfo As LongPtr, _
    ByVal lpszAddressString As LongPtr, _
    ByRef lpdwAddressStringLength As Long) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WSAAddressToStringW = ctypes.windll.ws2_32.WSAAddressToStringW
WSAAddressToStringW.restype = ctypes.c_int
WSAAddressToStringW.argtypes = [
    ctypes.c_void_p,  # lpsaAddress : SOCKADDR*
    wintypes.DWORD,  # dwAddressLength : DWORD
    ctypes.c_void_p,  # lpProtocolInfo : WSAPROTOCOL_INFOW* optional
    wintypes.LPWSTR,  # lpszAddressString : LPWSTR 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')
WSAAddressToStringW = Fiddle::Function.new(
  lib['WSAAddressToStringW'],
  [
    Fiddle::TYPE_VOIDP,  # lpsaAddress : SOCKADDR*
    -Fiddle::TYPE_INT,  # dwAddressLength : DWORD
    Fiddle::TYPE_VOIDP,  # lpProtocolInfo : WSAPROTOCOL_INFOW* optional
    Fiddle::TYPE_VOIDP,  # lpszAddressString : LPWSTR out
    Fiddle::TYPE_VOIDP,  # lpdwAddressStringLength : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "ws2_32")]
extern "system" {
    fn WSAAddressToStringW(
        lpsaAddress: *mut SOCKADDR,  // SOCKADDR*
        dwAddressLength: u32,  // DWORD
        lpProtocolInfo: *mut WSAPROTOCOL_INFOW,  // WSAPROTOCOL_INFOW* optional
        lpszAddressString: *mut u16,  // LPWSTR 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.Unicode, SetLastError = true)]
public static extern int WSAAddressToStringW(IntPtr lpsaAddress, uint dwAddressLength, IntPtr lpProtocolInfo, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpszAddressString, ref uint lpdwAddressStringLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_WSAAddressToStringW' -Namespace Win32 -PassThru
# $api::WSAAddressToStringW(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength)
#uselib "WS2_32.dll"
#func global WSAAddressToStringW "WSAAddressToStringW" wptr, wptr, wptr, wptr, wptr
; WSAAddressToStringW varptr(lpsaAddress), dwAddressLength, varptr(lpProtocolInfo), varptr(lpszAddressString), varptr(lpdwAddressStringLength)   ; 戻り値は stat
; lpsaAddress : SOCKADDR* -> "wptr"
; dwAddressLength : DWORD -> "wptr"
; lpProtocolInfo : WSAPROTOCOL_INFOW* optional -> "wptr"
; lpszAddressString : LPWSTR out -> "wptr"
; lpdwAddressStringLength : DWORD* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "WS2_32.dll"
#cfunc global WSAAddressToStringW "WSAAddressToStringW" var, int, var, var, var
; res = WSAAddressToStringW(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength)
; lpsaAddress : SOCKADDR* -> "var"
; dwAddressLength : DWORD -> "int"
; lpProtocolInfo : WSAPROTOCOL_INFOW* optional -> "var"
; lpszAddressString : LPWSTR out -> "var"
; lpdwAddressStringLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT WSAAddressToStringW(SOCKADDR* lpsaAddress, DWORD dwAddressLength, WSAPROTOCOL_INFOW* lpProtocolInfo, LPWSTR lpszAddressString, DWORD* lpdwAddressStringLength)
#uselib "WS2_32.dll"
#cfunc global WSAAddressToStringW "WSAAddressToStringW" var, int, var, var, var
; res = WSAAddressToStringW(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength)
; lpsaAddress : SOCKADDR* -> "var"
; dwAddressLength : DWORD -> "int"
; lpProtocolInfo : WSAPROTOCOL_INFOW* optional -> "var"
; lpszAddressString : LPWSTR out -> "var"
; lpdwAddressStringLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
	procWSAAddressToStringW = ws2_32.NewProc("WSAAddressToStringW")
)

// lpsaAddress (SOCKADDR*), dwAddressLength (DWORD), lpProtocolInfo (WSAPROTOCOL_INFOW* optional), lpszAddressString (LPWSTR out), lpdwAddressStringLength (DWORD* in/out)
r1, _, err := procWSAAddressToStringW.Call(
	uintptr(lpsaAddress),
	uintptr(dwAddressLength),
	uintptr(lpProtocolInfo),
	uintptr(lpszAddressString),
	uintptr(lpdwAddressStringLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function WSAAddressToStringW(
  lpsaAddress: Pointer;   // SOCKADDR*
  dwAddressLength: DWORD;   // DWORD
  lpProtocolInfo: Pointer;   // WSAPROTOCOL_INFOW* optional
  lpszAddressString: PWideChar;   // LPWSTR out
  lpdwAddressStringLength: Pointer   // DWORD* in/out
): Integer; stdcall;
  external 'WS2_32.dll' name 'WSAAddressToStringW';
result := DllCall("WS2_32\WSAAddressToStringW"
    , "Ptr", lpsaAddress   ; SOCKADDR*
    , "UInt", dwAddressLength   ; DWORD
    , "Ptr", lpProtocolInfo   ; WSAPROTOCOL_INFOW* optional
    , "Ptr", lpszAddressString   ; LPWSTR out
    , "Ptr", lpdwAddressStringLength   ; DWORD* in/out
    , "Int")   ; return: INT
●WSAAddressToStringW(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength) = DLL("WS2_32.dll", "int WSAAddressToStringW(void*, dword, void*, char*, void*)")
# 呼び出し: WSAAddressToStringW(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength)
# lpsaAddress : SOCKADDR* -> "void*"
# dwAddressLength : DWORD -> "dword"
# lpProtocolInfo : WSAPROTOCOL_INFOW* optional -> "void*"
# lpszAddressString : LPWSTR out -> "char*"
# lpdwAddressStringLength : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "ws2_32" fn WSAAddressToStringW(
    lpsaAddress: [*c]SOCKADDR, // SOCKADDR*
    dwAddressLength: u32, // DWORD
    lpProtocolInfo: [*c]WSAPROTOCOL_INFOW, // WSAPROTOCOL_INFOW* optional
    lpszAddressString: [*c]u16, // LPWSTR out
    lpdwAddressStringLength: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc WSAAddressToStringW(
    lpsaAddress: ptr SOCKADDR,  # SOCKADDR*
    dwAddressLength: uint32,  # DWORD
    lpProtocolInfo: ptr WSAPROTOCOL_INFOW,  # WSAPROTOCOL_INFOW* optional
    lpszAddressString: ptr uint16,  # LPWSTR out
    lpdwAddressStringLength: ptr uint32  # DWORD* in/out
): int32 {.importc: "WSAAddressToStringW", stdcall, dynlib: "WS2_32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "ws2_32");
extern(Windows)
int WSAAddressToStringW(
    SOCKADDR* lpsaAddress,   // SOCKADDR*
    uint dwAddressLength,   // DWORD
    WSAPROTOCOL_INFOW* lpProtocolInfo,   // WSAPROTOCOL_INFOW* optional
    wchar* lpszAddressString,   // LPWSTR out
    uint* lpdwAddressStringLength   // DWORD* in/out
);
ccall((:WSAAddressToStringW, "WS2_32.dll"), stdcall, Int32,
      (Ptr{SOCKADDR}, UInt32, Ptr{WSAPROTOCOL_INFOW}, Ptr{UInt16}, Ptr{UInt32}),
      lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength)
# lpsaAddress : SOCKADDR* -> Ptr{SOCKADDR}
# dwAddressLength : DWORD -> UInt32
# lpProtocolInfo : WSAPROTOCOL_INFOW* optional -> Ptr{WSAPROTOCOL_INFOW}
# lpszAddressString : LPWSTR out -> Ptr{UInt16}
# lpdwAddressStringLength : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t WSAAddressToStringW(
    void* lpsaAddress,
    uint32_t dwAddressLength,
    void* lpProtocolInfo,
    uint16_t* lpszAddressString,
    uint32_t* lpdwAddressStringLength);
]]
local ws2_32 = ffi.load("ws2_32")
-- ws2_32.WSAAddressToStringW(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength)
-- lpsaAddress : SOCKADDR*
-- dwAddressLength : DWORD
-- lpProtocolInfo : WSAPROTOCOL_INFOW* optional
-- lpszAddressString : LPWSTR out
-- lpdwAddressStringLength : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('WS2_32.dll');
const WSAAddressToStringW = lib.func('__stdcall', 'WSAAddressToStringW', 'int32_t', ['void *', 'uint32_t', 'void *', 'uint16_t *', 'uint32_t *']);
// WSAAddressToStringW(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength)
// lpsaAddress : SOCKADDR* -> 'void *'
// dwAddressLength : DWORD -> 'uint32_t'
// lpProtocolInfo : WSAPROTOCOL_INFOW* optional -> 'void *'
// lpszAddressString : LPWSTR out -> 'uint16_t *'
// lpdwAddressStringLength : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WS2_32.dll", {
  WSAAddressToStringW: { parameters: ["pointer", "u32", "pointer", "buffer", "pointer"], result: "i32" },
});
// lib.symbols.WSAAddressToStringW(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength)
// lpsaAddress : SOCKADDR* -> "pointer"
// dwAddressLength : DWORD -> "u32"
// lpProtocolInfo : WSAPROTOCOL_INFOW* optional -> "pointer"
// lpszAddressString : LPWSTR out -> "buffer"
// lpdwAddressStringLength : DWORD* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t WSAAddressToStringW(
    void* lpsaAddress,
    uint32_t dwAddressLength,
    void* lpProtocolInfo,
    uint16_t* lpszAddressString,
    uint32_t* lpdwAddressStringLength);
C, "WS2_32.dll");
// $ffi->WSAAddressToStringW(lpsaAddress, dwAddressLength, lpProtocolInfo, lpszAddressString, lpdwAddressStringLength);
// lpsaAddress : SOCKADDR*
// dwAddressLength : DWORD
// lpProtocolInfo : WSAPROTOCOL_INFOW* optional
// lpszAddressString : LPWSTR 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.UNICODE_OPTIONS);
    int WSAAddressToStringW(
        Pointer lpsaAddress,   // SOCKADDR*
        int dwAddressLength,   // DWORD
        Pointer lpProtocolInfo,   // WSAPROTOCOL_INFOW* optional
        char[] lpszAddressString,   // LPWSTR out
        IntByReference lpdwAddressStringLength   // DWORD* in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("ws2_32")]
lib LibWS2_32
  fun WSAAddressToStringW = WSAAddressToStringW(
    lpsaAddress : SOCKADDR*,   # SOCKADDR*
    dwAddressLength : UInt32,   # DWORD
    lpProtocolInfo : WSAPROTOCOL_INFOW*,   # WSAPROTOCOL_INFOW* optional
    lpszAddressString : UInt16*,   # LPWSTR 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 WSAAddressToStringWNative = Int32 Function(Pointer<Void>, Uint32, Pointer<Void>, Pointer<Utf16>, Pointer<Uint32>);
typedef WSAAddressToStringWDart = int Function(Pointer<Void>, int, Pointer<Void>, Pointer<Utf16>, Pointer<Uint32>);
final WSAAddressToStringW = DynamicLibrary.open('WS2_32.dll')
    .lookupFunction<WSAAddressToStringWNative, WSAAddressToStringWDart>('WSAAddressToStringW');
// lpsaAddress : SOCKADDR* -> Pointer<Void>
// dwAddressLength : DWORD -> Uint32
// lpProtocolInfo : WSAPROTOCOL_INFOW* optional -> Pointer<Void>
// lpszAddressString : LPWSTR out -> Pointer<Utf16>
// lpdwAddressStringLength : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WSAAddressToStringW(
  lpsaAddress: Pointer;   // SOCKADDR*
  dwAddressLength: DWORD;   // DWORD
  lpProtocolInfo: Pointer;   // WSAPROTOCOL_INFOW* optional
  lpszAddressString: PWideChar;   // LPWSTR out
  lpdwAddressStringLength: Pointer   // DWORD* in/out
): Integer; stdcall;
  external 'WS2_32.dll' name 'WSAAddressToStringW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let wsaaddresstostringw =
  foreign "WSAAddressToStringW"
    ((ptr void) @-> uint32_t @-> (ptr void) @-> (ptr uint16_t) @-> (ptr uint32_t) @-> returning int32_t)
(* lpsaAddress : SOCKADDR* -> (ptr void) *)
(* dwAddressLength : DWORD -> uint32_t *)
(* lpProtocolInfo : WSAPROTOCOL_INFOW* optional -> (ptr void) *)
(* lpszAddressString : LPWSTR out -> (ptr uint16_t) *)
(* 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 ("WSAAddressToStringW" wsaaddress-to-string-w :convention :stdcall) :int32
  (lpsa-address :pointer)   ; SOCKADDR*
  (dw-address-length :uint32)   ; DWORD
  (lp-protocol-info :pointer)   ; WSAPROTOCOL_INFOW* optional
  (lpsz-address-string :pointer)   ; LPWSTR 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 $WSAAddressToStringW = Win32::API::More->new('WS2_32',
    'int WSAAddressToStringW(LPVOID lpsaAddress, DWORD dwAddressLength, LPVOID lpProtocolInfo, LPWSTR lpszAddressString, LPVOID lpdwAddressStringLength)');
# my $ret = $WSAAddressToStringW->Call($lpsaAddress, $dwAddressLength, $lpProtocolInfo, $lpszAddressString, $lpdwAddressStringLength);
# lpsaAddress : SOCKADDR* -> LPVOID
# dwAddressLength : DWORD -> DWORD
# lpProtocolInfo : WSAPROTOCOL_INFOW* optional -> LPVOID
# lpszAddressString : LPWSTR out -> LPWSTR
# lpdwAddressStringLength : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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