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

WSAEnumProtocolsW

関数
利用可能なトランスポートプロトコル情報を列挙する。
DLLWS2_32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 8.1 以降

シグネチャ

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

INT WSAEnumProtocolsW(
    INT* lpiProtocols,   // optional
    WSAPROTOCOL_INFOW* lpProtocolBuffer,   // optional
    DWORD* lpdwBufferLength
);

パラメーター

名前方向説明
lpiProtocolsINT*inoptionaliProtocol 値の NULL 終端配列です。このパラメーターは省略可能です。lpiProtocolsNULL の場合、利用可能なすべてのプロトコルに関する情報が返されます。それ以外の場合は、配列に列挙されたプロトコルについてのみ情報が取得されます。
lpProtocolBufferWSAPROTOCOL_INFOW*outoptionalWSAPROTOCOL_INFO 構造体で満たされるバッファーへのポインターです。
lpdwBufferLengthDWORD*inout入力時には、 WSAEnumProtocols に渡される lpProtocolBuffer バッファーのバイト数を指定します。出力時には、要求されたすべての情報を取得するために WSAEnumProtocols に渡すことができる最小バッファーサイズが格納されます。このルーチンは複数回の呼び出しにわたって列挙する機能を持たないため、ルーチンが成功するには、渡されるバッファーがすべてのエントリを格納できる十分な大きさである必要があります。これにより API の複雑さが軽減されます。コンピューターに読み込まれているプロトコルの数は通常少ないため、これが問題になることはありません。

戻り値の型: INT

公式ドキュメント

WSAEnumProtocols 関数は、利用可能なトランスポートプロトコルに関する情報を取得します。(Unicode)

戻り値

エラーが発生しなかった場合、 WSAEnumProtocols は報告対象となるプロトコルの数を返します。それ以外の場合は SOCKET_ERROR が返され、 WSAGetLastError を呼び出すことで特定のエラーコードを取得できます。

エラーコード 意味
WSANOTINITIALISED
この関数を使用する前に、 WSAStartup の呼び出しが成功している必要があります。
WSAENETDOWN
ネットワークサブシステムで障害が発生しました。
WSAEINPROGRESS
ブロッキング型の Windows Sockets 1.1 の呼び出しが進行中です。
WSAEINVAL
指定されたパラメーターのいずれかが無効であることを示します。
WSAENOBUFS
バッファー長が小さすぎて、関連するすべての WSAPROTOCOL_INFO 構造体と関連情報を受け取ることができませんでした。lpdwBufferLength に返された値以上の大きさのバッファーを渡してください。
WSAEFAULT
lpiProtocolslpProtocolBufferlpdwBufferLength のいずれか 1 つ以上のパラメーターが、ユーザーアドレス空間の有効な部分ではありません。

解説(Remarks)

WSAEnumProtocols 関数は、ローカルコンピューターにインストールされているトランスポートプロトコルの集合に関する情報を取得するために使用されます。階層化プロトコル(layered protocol)は、プロトコルチェーンにインストールされている場合にのみアプリケーションから使用できます。階層化プロトコルに関する情報は、lpProtocolBuffer 内にチェーン長 0 でインストールされたダミーの階層化サービスプロバイダー(LSP)を除き、返されません。

注意 階層化サービスプロバイダー(Layered Service Provider)は非推奨です。Windows 8 および Windows Server 2012 以降では、Windows Filtering Platform を使用してください。
lpiProtocols パラメーターは、提供される情報量を制限するためのフィルターとして使用できます。多くの場合、lpiProtocols には NULL ポインターが指定され、これにより、利用可能なすべてのトランスポートプロトコルおよびプロトコルチェーンに関する情報が返されます。

WSAEnumProtocols 関数は、WSCEnumProtocols 関数および WSCEnumProtocols32 関数とは異なり、 インストールされているすべてのプロトコルについて WSAPROTOCOL_INFO 構造体を返すわけではありません。WSAEnumProtocols 関数は、サービスプロバイダーが WSAPROTOCOL_INFO 構造体の dwProviderFlags メンバーに PFL_HIDDEN フラグを設定したプロトコルを除外します。このフラグは、当該プロトコルを WSAEnumProtocols 関数が生成する結果バッファーに返すべきではないことを Ws2_32.dll に示すものです。さらに、WSAEnumProtocols 関数は、チェーン長が 1 以上の WSAPROTOCOL_INFO 構造体(LSP プロバイダー)についてはデータを返しません。WSAEnumProtocols は、PFL_HIDDEN フラグを持たず、かつプロトコルチェーン長が 0 でない、ベースプロトコルおよびプロトコルチェーンに関する情報のみを返します。

要求された各プロトコルについて、lpProtocolBuffer が指すバッファー内に WSAPROTOCOL_INFO 構造体が格納されます。指定されたバッファーが十分な大きさでない場合(lpdwBufferLength の入力値で示されます)、lpdwBufferLength が指す値が更新され、必要なバッファーサイズが示されます。その場合、アプリケーションは十分な大きさのバッファーを確保したうえで、再度 WSAEnumProtocols を呼び出す必要があります。

バッファー内に WSAPROTOCOL_INFO 構造体が現れる順序は、サービスプロバイダーが WS2_32.DLL を使用してプロトコルエントリを登録した順序、または既定の TCP/IP プロバイダーを設定するために提供される Windows Sockets アプリケーションや DLL によって後から行われた並べ替えの結果と一致します。

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

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

使用例

次の例は、利用可能なトランスポートプロトコルの WSAPROTOCOL_INFO 構造体の配列を取得するために WSAEnumProtocols 関数を使用する方法を示しています。

#ifndef UNICODE
#define UNICODE 1
#endif

#include <winsock2.h>
#include <ws2tcpip.h>
#include <objbase.h>
#include <stdio.h>

// Link with ws2_32.lib and ole32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "ole32.lib")

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
// Note: could also use malloc() and free()

int wmain()
{

    //-----------------------------------------
    // Declare and initialize variables
    WSADATA wsaData;
    int iResult = 0;

    int iError = 0;
    INT iNuminfo = 0;

    int i;

    // Allocate a 16K buffer to retrieve all the protocol providers
    DWORD dwBufferLen = 16384;

    LPWSAPROTOCOL_INFO lpProtocolInfo = NULL;

    // variables needed for converting provider GUID to a string
    int iRet = 0;
    WCHAR GuidString[40] = { 0 };

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        wprintf(L"WSAStartup failed: %d\n", iResult);
        return 1;
    }

    lpProtocolInfo = (LPWSAPROTOCOL_INFO) MALLOC(dwBufferLen);
    if (lpProtocolInfo == NULL) {
        wprintf(L"Memory allocation for providers buffer failed\n");
        WSACleanup();
        return 1;
    }

    iNuminfo = WSAEnumProtocols(NULL, lpProtocolInfo, &dwBufferLen);
    if (iNuminfo == SOCKET_ERROR) {
        iError = WSAGetLastError();
        if (iError != WSAENOBUFS) {
            wprintf(L"WSAEnumProtocols failed with error: %d\n", iError);
            if (lpProtocolInfo) {
                FREE(lpProtocolInfo);
                lpProtocolInfo = NULL;
            }
            WSACleanup();
            return 1;
        } else {
            wprintf(L"WSAEnumProtocols failed with error: WSAENOBUFS (%d)\n",
                    iError);
            wprintf(L"  Increasing buffer size to %d\n\n", dwBufferLen);
            if (lpProtocolInfo) {
                FREE(lpProtocolInfo);
                lpProtocolInfo = NULL;
            }
            lpProtocolInfo = (LPWSAPROTOCOL_INFO) MALLOC(dwBufferLen);
            if (lpProtocolInfo == NULL) {
                wprintf(L"Memory allocation increase for buffer failed\n");
                WSACleanup();
                return 1;
            }
            iNuminfo = WSAEnumProtocols(NULL, lpProtocolInfo, &dwBufferLen);
            if (iNuminfo == SOCKET_ERROR) {
                iError = WSAGetLastError();
                wprintf(L"WSAEnumProtocols failed with error: %d\n", iError);
                if (lpProtocolInfo) {
                    FREE(lpProtocolInfo);
                    lpProtocolInfo = NULL;
                }
                WSACleanup();
                return 1;
            }

        }
    }

    wprintf(L"WSAEnumProtocols succeeded with protocol count = %d\n\n",
            iNuminfo);
    for (i = 0; i < iNuminfo; i++) {
        wprintf(L"Winsock Catalog Provider Entry #%d\n", i);
        wprintf
            (L"----------------------------------------------------------\n");
        wprintf(L"Entry type:\t\t\t ");
        if (lpProtocolInfo[i].ProtocolChain.ChainLen == 1)
            wprintf(L"Base Service Provider\n");
        else
            wprintf(L"Layered Chain Entry\n");

        wprintf(L"Protocol:\t\t\t %ws\n", lpProtocolInfo[i].szProtocol);

        iRet =
            StringFromGUID2(lpProtocolInfo[i].ProviderId,
                            (LPOLESTR) & GuidString, 39);
        if (iRet == 0)
            wprintf(L"StringFromGUID2 failed\n");
        else
            wprintf(L"Provider ID:\t\t\t %ws\n", GuidString);

        wprintf(L"Catalog Entry ID:\t\t %u\n",
                lpProtocolInfo[i].dwCatalogEntryId);

        wprintf(L"Version:\t\t\t %d\n", lpProtocolInfo[i].iVersion);

        wprintf(L"Address Family:\t\t\t %d\n",
                lpProtocolInfo[i].iAddressFamily);
        wprintf(L"Max Socket Address Length:\t %d\n",
                lpProtocolInfo[i].iMaxSockAddr);
        wprintf(L"Min Socket Address Length:\t %d\n",
                lpProtocolInfo[i].iMinSockAddr);

        wprintf(L"Socket Type:\t\t\t %d\n", lpProtocolInfo[i].iSocketType);
        wprintf(L"Socket Protocol:\t\t %d\n", lpProtocolInfo[i].iProtocol);
        wprintf(L"Socket Protocol Max Offset:\t %d\n",
                lpProtocolInfo[i].iProtocolMaxOffset);

        wprintf(L"Network Byte Order:\t\t %d\n",
                lpProtocolInfo[i].iNetworkByteOrder);
        wprintf(L"Security Scheme:\t\t %d\n",
                lpProtocolInfo[i].iSecurityScheme);
        wprintf(L"Max Message Size:\t\t %u\n", lpProtocolInfo[i].dwMessageSize);

        wprintf(L"ServiceFlags1:\t\t\t 0x%x\n",
                lpProtocolInfo[i].dwServiceFlags1);
        wprintf(L"ServiceFlags2:\t\t\t 0x%x\n",
                lpProtocolInfo[i].dwServiceFlags2);
        wprintf(L"ServiceFlags3:\t\t\t 0x%x\n",
                lpProtocolInfo[i].dwServiceFlags3);
        wprintf(L"ServiceFlags4:\t\t\t 0x%x\n",
                lpProtocolInfo[i].dwServiceFlags4);
        wprintf(L"ProviderFlags:\t\t\t 0x%x\n",
                lpProtocolInfo[i].dwProviderFlags);

        wprintf(L"Protocol Chain length:\t\t %d\n",
                lpProtocolInfo[i].ProtocolChain.ChainLen);

        wprintf(L"\n");
    }

    if (lpProtocolInfo) {
        FREE(lpProtocolInfo);
        lpProtocolInfo = NULL;
    }
    WSACleanup();

    return 0;
}

メモ

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

出典・ライセンス: 上記「公式ドキュメント」の内容は 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 WSAEnumProtocolsW(
    INT* lpiProtocols,   // optional
    WSAPROTOCOL_INFOW* lpProtocolBuffer,   // optional
    DWORD* lpdwBufferLength
);
[DllImport("WS2_32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern int WSAEnumProtocolsW(
    IntPtr lpiProtocols,   // INT* optional
    IntPtr lpProtocolBuffer,   // WSAPROTOCOL_INFOW* optional, out
    ref uint lpdwBufferLength   // DWORD* in/out
);
<DllImport("WS2_32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WSAEnumProtocolsW(
    lpiProtocols As IntPtr,   ' INT* optional
    lpProtocolBuffer As IntPtr,   ' WSAPROTOCOL_INFOW* optional, out
    ByRef lpdwBufferLength As UInteger   ' DWORD* in/out
) As Integer
End Function
' lpiProtocols : INT* optional
' lpProtocolBuffer : WSAPROTOCOL_INFOW* optional, out
' lpdwBufferLength : DWORD* in/out
Declare PtrSafe Function WSAEnumProtocolsW Lib "ws2_32" ( _
    ByVal lpiProtocols As LongPtr, _
    ByVal lpProtocolBuffer As LongPtr, _
    ByRef lpdwBufferLength 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

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

var (
	ws2_32 = windows.NewLazySystemDLL("WS2_32.dll")
	procWSAEnumProtocolsW = ws2_32.NewProc("WSAEnumProtocolsW")
)

// lpiProtocols (INT* optional), lpProtocolBuffer (WSAPROTOCOL_INFOW* optional, out), lpdwBufferLength (DWORD* in/out)
r1, _, err := procWSAEnumProtocolsW.Call(
	uintptr(lpiProtocols),
	uintptr(lpProtocolBuffer),
	uintptr(lpdwBufferLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function WSAEnumProtocolsW(
  lpiProtocols: Pointer;   // INT* optional
  lpProtocolBuffer: Pointer;   // WSAPROTOCOL_INFOW* optional, out
  lpdwBufferLength: Pointer   // DWORD* in/out
): Integer; stdcall;
  external 'WS2_32.dll' name 'WSAEnumProtocolsW';
result := DllCall("WS2_32\WSAEnumProtocolsW"
    , "Ptr", lpiProtocols   ; INT* optional
    , "Ptr", lpProtocolBuffer   ; WSAPROTOCOL_INFOW* optional, out
    , "Ptr", lpdwBufferLength   ; DWORD* in/out
    , "Int")   ; return: INT
●WSAEnumProtocolsW(lpiProtocols, lpProtocolBuffer, lpdwBufferLength) = DLL("WS2_32.dll", "int WSAEnumProtocolsW(void*, void*, void*)")
# 呼び出し: WSAEnumProtocolsW(lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
# lpiProtocols : INT* optional -> "void*"
# lpProtocolBuffer : WSAPROTOCOL_INFOW* optional, out -> "void*"
# lpdwBufferLength : 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 WSAEnumProtocolsW(
    lpiProtocols: [*c]i32, // INT* optional
    lpProtocolBuffer: [*c]WSAPROTOCOL_INFOW, // WSAPROTOCOL_INFOW* optional, out
    lpdwBufferLength: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc WSAEnumProtocolsW(
    lpiProtocols: ptr int32,  # INT* optional
    lpProtocolBuffer: ptr WSAPROTOCOL_INFOW,  # WSAPROTOCOL_INFOW* optional, out
    lpdwBufferLength: ptr uint32  # DWORD* in/out
): int32 {.importc: "WSAEnumProtocolsW", stdcall, dynlib: "WS2_32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "ws2_32");
extern(Windows)
int WSAEnumProtocolsW(
    int* lpiProtocols,   // INT* optional
    WSAPROTOCOL_INFOW* lpProtocolBuffer,   // WSAPROTOCOL_INFOW* optional, out
    uint* lpdwBufferLength   // DWORD* in/out
);
ccall((:WSAEnumProtocolsW, "WS2_32.dll"), stdcall, Int32,
      (Ptr{Int32}, Ptr{WSAPROTOCOL_INFOW}, Ptr{UInt32}),
      lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
# lpiProtocols : INT* optional -> Ptr{Int32}
# lpProtocolBuffer : WSAPROTOCOL_INFOW* optional, out -> Ptr{WSAPROTOCOL_INFOW}
# lpdwBufferLength : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int32_t WSAEnumProtocolsW(
    int32_t* lpiProtocols,
    void* lpProtocolBuffer,
    uint32_t* lpdwBufferLength);
]]
local ws2_32 = ffi.load("ws2_32")
-- ws2_32.WSAEnumProtocolsW(lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
-- lpiProtocols : INT* optional
-- lpProtocolBuffer : WSAPROTOCOL_INFOW* optional, out
-- lpdwBufferLength : 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 WSAEnumProtocolsW = lib.func('__stdcall', 'WSAEnumProtocolsW', 'int32_t', ['int32_t *', 'void *', 'uint32_t *']);
// WSAEnumProtocolsW(lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
// lpiProtocols : INT* optional -> 'int32_t *'
// lpProtocolBuffer : WSAPROTOCOL_INFOW* optional, out -> 'void *'
// lpdwBufferLength : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WS2_32.dll", {
  WSAEnumProtocolsW: { parameters: ["pointer", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.WSAEnumProtocolsW(lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
// lpiProtocols : INT* optional -> "pointer"
// lpProtocolBuffer : WSAPROTOCOL_INFOW* optional, out -> "pointer"
// lpdwBufferLength : DWORD* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t WSAEnumProtocolsW(
    int32_t* lpiProtocols,
    void* lpProtocolBuffer,
    uint32_t* lpdwBufferLength);
C, "WS2_32.dll");
// $ffi->WSAEnumProtocolsW(lpiProtocols, lpProtocolBuffer, lpdwBufferLength);
// lpiProtocols : INT* optional
// lpProtocolBuffer : WSAPROTOCOL_INFOW* optional, out
// lpdwBufferLength : 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 WSAEnumProtocolsW(
        IntByReference lpiProtocols,   // INT* optional
        Pointer lpProtocolBuffer,   // WSAPROTOCOL_INFOW* optional, out
        IntByReference lpdwBufferLength   // DWORD* in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("ws2_32")]
lib LibWS2_32
  fun WSAEnumProtocolsW = WSAEnumProtocolsW(
    lpiProtocols : Int32*,   # INT* optional
    lpProtocolBuffer : WSAPROTOCOL_INFOW*,   # WSAPROTOCOL_INFOW* optional, out
    lpdwBufferLength : 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 WSAEnumProtocolsWNative = Int32 Function(Pointer<Int32>, Pointer<Void>, Pointer<Uint32>);
typedef WSAEnumProtocolsWDart = int Function(Pointer<Int32>, Pointer<Void>, Pointer<Uint32>);
final WSAEnumProtocolsW = DynamicLibrary.open('WS2_32.dll')
    .lookupFunction<WSAEnumProtocolsWNative, WSAEnumProtocolsWDart>('WSAEnumProtocolsW');
// lpiProtocols : INT* optional -> Pointer<Int32>
// lpProtocolBuffer : WSAPROTOCOL_INFOW* optional, out -> Pointer<Void>
// lpdwBufferLength : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WSAEnumProtocolsW(
  lpiProtocols: Pointer;   // INT* optional
  lpProtocolBuffer: Pointer;   // WSAPROTOCOL_INFOW* optional, out
  lpdwBufferLength: Pointer   // DWORD* in/out
): Integer; stdcall;
  external 'WS2_32.dll' name 'WSAEnumProtocolsW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "WSAEnumProtocolsW"
  c_WSAEnumProtocolsW :: Ptr Int32 -> Ptr () -> Ptr Word32 -> IO Int32
-- lpiProtocols : INT* optional -> Ptr Int32
-- lpProtocolBuffer : WSAPROTOCOL_INFOW* optional, out -> Ptr ()
-- lpdwBufferLength : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let wsaenumprotocolsw =
  foreign "WSAEnumProtocolsW"
    ((ptr int32_t) @-> (ptr void) @-> (ptr uint32_t) @-> returning int32_t)
(* lpiProtocols : INT* optional -> (ptr int32_t) *)
(* lpProtocolBuffer : WSAPROTOCOL_INFOW* optional, out -> (ptr void) *)
(* lpdwBufferLength : 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 ("WSAEnumProtocolsW" wsaenum-protocols-w :convention :stdcall) :int32
  (lpi-protocols :pointer)   ; INT* optional
  (lp-protocol-buffer :pointer)   ; WSAPROTOCOL_INFOW* optional, out
  (lpdw-buffer-length :pointer))   ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $WSAEnumProtocolsW = Win32::API::More->new('WS2_32',
    'int WSAEnumProtocolsW(LPVOID lpiProtocols, LPVOID lpProtocolBuffer, LPVOID lpdwBufferLength)');
# my $ret = $WSAEnumProtocolsW->Call($lpiProtocols, $lpProtocolBuffer, $lpdwBufferLength);
# lpiProtocols : INT* optional -> LPVOID
# lpProtocolBuffer : WSAPROTOCOL_INFOW* optional, out -> LPVOID
# lpdwBufferLength : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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