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

WSAEnumProtocolsA

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

シグネチャ

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

INT WSAEnumProtocolsA(
    INT* lpiProtocols,   // optional
    WSAPROTOCOL_INFOA* lpProtocolBuffer,   // optional
    DWORD* lpdwBufferLength
);

パラメーター

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

戻り値の型: INT

公式ドキュメント

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

戻り値

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

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

解説(Remarks)

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

階層化サービスプロバイダーは非推奨です。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  (ANSI / -A)
#include <windows.h>

INT WSAEnumProtocolsA(
    INT* lpiProtocols,   // optional
    WSAPROTOCOL_INFOA* lpProtocolBuffer,   // optional
    DWORD* lpdwBufferLength
);
[DllImport("WS2_32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern int WSAEnumProtocolsA(
    IntPtr lpiProtocols,   // INT* optional
    IntPtr lpProtocolBuffer,   // WSAPROTOCOL_INFOA* optional, out
    ref uint lpdwBufferLength   // DWORD* in/out
);
<DllImport("WS2_32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WSAEnumProtocolsA(
    lpiProtocols As IntPtr,   ' INT* optional
    lpProtocolBuffer As IntPtr,   ' WSAPROTOCOL_INFOA* optional, out
    ByRef lpdwBufferLength As UInteger   ' DWORD* in/out
) As Integer
End Function
' lpiProtocols : INT* optional
' lpProtocolBuffer : WSAPROTOCOL_INFOA* optional, out
' lpdwBufferLength : DWORD* in/out
Declare PtrSafe Function WSAEnumProtocolsA Lib "ws2_32" ( _
    ByVal lpiProtocols As LongPtr, _
    ByVal lpProtocolBuffer As LongPtr, _
    ByRef lpdwBufferLength As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

WSAEnumProtocolsA = ctypes.windll.ws2_32.WSAEnumProtocolsA
WSAEnumProtocolsA.restype = ctypes.c_int
WSAEnumProtocolsA.argtypes = [
    ctypes.POINTER(ctypes.c_int),  # lpiProtocols : INT* optional
    ctypes.c_void_p,  # lpProtocolBuffer : WSAPROTOCOL_INFOA* 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')
WSAEnumProtocolsA = Fiddle::Function.new(
  lib['WSAEnumProtocolsA'],
  [
    Fiddle::TYPE_VOIDP,  # lpiProtocols : INT* optional
    Fiddle::TYPE_VOIDP,  # lpProtocolBuffer : WSAPROTOCOL_INFOA* optional, out
    Fiddle::TYPE_VOIDP,  # lpdwBufferLength : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "ws2_32")]
extern "system" {
    fn WSAEnumProtocolsA(
        lpiProtocols: *mut i32,  // INT* optional
        lpProtocolBuffer: *mut WSAPROTOCOL_INFOA,  // WSAPROTOCOL_INFOA* 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.Ansi, SetLastError = true)]
public static extern int WSAEnumProtocolsA(IntPtr lpiProtocols, IntPtr lpProtocolBuffer, ref uint lpdwBufferLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WS2_32_WSAEnumProtocolsA' -Namespace Win32 -PassThru
# $api::WSAEnumProtocolsA(lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
#uselib "WS2_32.dll"
#func global WSAEnumProtocolsA "WSAEnumProtocolsA" sptr, sptr, sptr
; WSAEnumProtocolsA varptr(lpiProtocols), varptr(lpProtocolBuffer), varptr(lpdwBufferLength)   ; 戻り値は stat
; lpiProtocols : INT* optional -> "sptr"
; lpProtocolBuffer : WSAPROTOCOL_INFOA* optional, out -> "sptr"
; lpdwBufferLength : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "WS2_32.dll"
#cfunc global WSAEnumProtocolsA "WSAEnumProtocolsA" var, var, var
; res = WSAEnumProtocolsA(lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
; lpiProtocols : INT* optional -> "var"
; lpProtocolBuffer : WSAPROTOCOL_INFOA* optional, out -> "var"
; lpdwBufferLength : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; INT WSAEnumProtocolsA(INT* lpiProtocols, WSAPROTOCOL_INFOA* lpProtocolBuffer, DWORD* lpdwBufferLength)
#uselib "WS2_32.dll"
#cfunc global WSAEnumProtocolsA "WSAEnumProtocolsA" var, var, var
; res = WSAEnumProtocolsA(lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
; lpiProtocols : INT* optional -> "var"
; lpProtocolBuffer : WSAPROTOCOL_INFOA* 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")
	procWSAEnumProtocolsA = ws2_32.NewProc("WSAEnumProtocolsA")
)

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

extern "ws2_32" fn WSAEnumProtocolsA(
    lpiProtocols: [*c]i32, // INT* optional
    lpProtocolBuffer: [*c]WSAPROTOCOL_INFOA, // WSAPROTOCOL_INFOA* optional, out
    lpdwBufferLength: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;
proc WSAEnumProtocolsA(
    lpiProtocols: ptr int32,  # INT* optional
    lpProtocolBuffer: ptr WSAPROTOCOL_INFOA,  # WSAPROTOCOL_INFOA* optional, out
    lpdwBufferLength: ptr uint32  # DWORD* in/out
): int32 {.importc: "WSAEnumProtocolsA", stdcall, dynlib: "WS2_32.dll".}
pragma(lib, "ws2_32");
extern(Windows)
int WSAEnumProtocolsA(
    int* lpiProtocols,   // INT* optional
    WSAPROTOCOL_INFOA* lpProtocolBuffer,   // WSAPROTOCOL_INFOA* optional, out
    uint* lpdwBufferLength   // DWORD* in/out
);
ccall((:WSAEnumProtocolsA, "WS2_32.dll"), stdcall, Int32,
      (Ptr{Int32}, Ptr{WSAPROTOCOL_INFOA}, Ptr{UInt32}),
      lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
# lpiProtocols : INT* optional -> Ptr{Int32}
# lpProtocolBuffer : WSAPROTOCOL_INFOA* optional, out -> Ptr{WSAPROTOCOL_INFOA}
# lpdwBufferLength : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t WSAEnumProtocolsA(
    int32_t* lpiProtocols,
    void* lpProtocolBuffer,
    uint32_t* lpdwBufferLength);
]]
local ws2_32 = ffi.load("ws2_32")
-- ws2_32.WSAEnumProtocolsA(lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
-- lpiProtocols : INT* optional
-- lpProtocolBuffer : WSAPROTOCOL_INFOA* optional, out
-- lpdwBufferLength : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('WS2_32.dll');
const WSAEnumProtocolsA = lib.func('__stdcall', 'WSAEnumProtocolsA', 'int32_t', ['int32_t *', 'void *', 'uint32_t *']);
// WSAEnumProtocolsA(lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
// lpiProtocols : INT* optional -> 'int32_t *'
// lpProtocolBuffer : WSAPROTOCOL_INFOA* optional, out -> 'void *'
// lpdwBufferLength : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WS2_32.dll", {
  WSAEnumProtocolsA: { parameters: ["pointer", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.WSAEnumProtocolsA(lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
// lpiProtocols : INT* optional -> "pointer"
// lpProtocolBuffer : WSAPROTOCOL_INFOA* optional, out -> "pointer"
// lpdwBufferLength : DWORD* in/out -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t WSAEnumProtocolsA(
    int32_t* lpiProtocols,
    void* lpProtocolBuffer,
    uint32_t* lpdwBufferLength);
C, "WS2_32.dll");
// $ffi->WSAEnumProtocolsA(lpiProtocols, lpProtocolBuffer, lpdwBufferLength);
// lpiProtocols : INT* optional
// lpProtocolBuffer : WSAPROTOCOL_INFOA* 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.ASCII_OPTIONS);
    int WSAEnumProtocolsA(
        IntByReference lpiProtocols,   // INT* optional
        Pointer lpProtocolBuffer,   // WSAPROTOCOL_INFOA* optional, out
        IntByReference lpdwBufferLength   // DWORD* in/out
    );
}
@[Link("ws2_32")]
lib LibWS2_32
  fun WSAEnumProtocolsA = WSAEnumProtocolsA(
    lpiProtocols : Int32*,   # INT* optional
    lpProtocolBuffer : WSAPROTOCOL_INFOA*,   # WSAPROTOCOL_INFOA* 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 WSAEnumProtocolsANative = Int32 Function(Pointer<Int32>, Pointer<Void>, Pointer<Uint32>);
typedef WSAEnumProtocolsADart = int Function(Pointer<Int32>, Pointer<Void>, Pointer<Uint32>);
final WSAEnumProtocolsA = DynamicLibrary.open('WS2_32.dll')
    .lookupFunction<WSAEnumProtocolsANative, WSAEnumProtocolsADart>('WSAEnumProtocolsA');
// lpiProtocols : INT* optional -> Pointer<Int32>
// lpProtocolBuffer : WSAPROTOCOL_INFOA* optional, out -> Pointer<Void>
// lpdwBufferLength : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function WSAEnumProtocolsA(
  lpiProtocols: Pointer;   // INT* optional
  lpProtocolBuffer: Pointer;   // WSAPROTOCOL_INFOA* optional, out
  lpdwBufferLength: Pointer   // DWORD* in/out
): Integer; stdcall;
  external 'WS2_32.dll' name 'WSAEnumProtocolsA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let wsaenumprotocolsa =
  foreign "WSAEnumProtocolsA"
    ((ptr int32_t) @-> (ptr void) @-> (ptr uint32_t) @-> returning int32_t)
(* lpiProtocols : INT* optional -> (ptr int32_t) *)
(* lpProtocolBuffer : WSAPROTOCOL_INFOA* 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 ("WSAEnumProtocolsA" wsaenum-protocols-a :convention :stdcall) :int32
  (lpi-protocols :pointer)   ; INT* optional
  (lp-protocol-buffer :pointer)   ; WSAPROTOCOL_INFOA* 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 $WSAEnumProtocolsA = Win32::API::More->new('WS2_32',
    'int WSAEnumProtocolsA(LPVOID lpiProtocols, LPVOID lpProtocolBuffer, LPVOID lpdwBufferLength)');
# my $ret = $WSAEnumProtocolsA->Call($lpiProtocols, $lpProtocolBuffer, $lpdwBufferLength);
# lpiProtocols : INT* optional -> LPVOID
# lpProtocolBuffer : WSAPROTOCOL_INFOA* optional, out -> LPVOID
# lpdwBufferLength : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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