EnumProtocolsW
関数シグネチャ
// MSWSOCK.dll (Unicode / -W)
#include <windows.h>
INT EnumProtocolsW(
INT* lpiProtocols, // optional
void* lpProtocolBuffer,
DWORD* lpdwBufferLength
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpiProtocols | INT* | inoptional | プロトコル識別子の null 終端配列へのポインターです。 EnumProtocols 関数は、この配列で指定されたプロトコルに関する情報を取得します。 lpiProtocols が NULL の場合、関数は利用可能なすべてのプロトコルに関する情報を取得します。 次のプロトコル識別子の値が定義されています。 |
| lpProtocolBuffer | void* | out | 関数が PROTOCOL_INFO データ構造体の配列を書き込むバッファーへのポインターです。 |
| lpdwBufferLength | DWORD* | inout | 入力時には、lpProtocolBuffer が指すバッファーのサイズ (バイト単位) を指定する変数へのポインターです。 出力時には、関数はこの変数に、要求されたすべての情報を取得するために必要な最小バッファーサイズを設定します。関数が成功するには、バッファーが少なくともこのサイズである必要があります。 |
戻り値の型: INT
公式ドキュメント
EnumProtocols 関数は、ローカルホスト上でアクティブな、指定された一連のネットワークプロトコルに関する情報を取得します。(Unicode)
戻り値
関数が成功した場合、戻り値は lpProtocolBuffer が指すバッファーに書き込まれた PROTOCOL_INFO データ構造体の数です。
関数が失敗した場合、戻り値は SOCKET_ERROR(–1) です。拡張エラー情報を取得するには GetLastError を呼び出します。この関数は次の拡張エラーコードを返します。
| エラーコード | 意味 |
|---|---|
| lpProtocolBuffer が指すバッファーが小さすぎて、関連するすべての PROTOCOL_INFO 構造体を受け取れませんでした。*lpdwBufferLength で返された値以上のサイズのバッファーを指定して、関数を呼び出してください。 |
解説(Remarks)
次のサンプルコードでは、 EnumProtocols 関数がシステム上で利用可能なすべてのプロトコルに関する情報を取得します。続いてコードは、各プロトコルをより詳細に調べます。
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <Nspapi.h>
#include <stdlib.h>
#include <stdio.h>
// Need to link with Ws2_32.lib and Mswsock.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
int FindProtocol(BOOL Reliable,
BOOL MessageOriented, BOOL StreamOriented,
BOOL Connectionless, DWORD *ProtocolUsed);
int __cdecl main(int argc, char **argv)
{
WSADATA wsaData;
int ProtocolError = SOCKET_ERROR;
int iResult;
BOOLEAN bReliable = FALSE;
BOOLEAN bMessageOriented = FALSE;
BOOLEAN bStreamOriented = TRUE;
BOOLEAN bConnectionless = FALSE;
DWORD *pProtocols = NULL;
// Validate the parameters
if (argc != 2) {
printf("usage: %s servicename\n", argv[0]);
return 1;
}
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ProtocolError = FindProtocol( bReliable, bMessageOriented,
bStreamOriented, bConnectionless, pProtocols);
if (ProtocolError == SOCKET_ERROR) {
printf("Unable to find a protocol to support the parameters requested\n");
return 1;
}
// Connect to the servicename ...
return 0;
}
#define MAX_PROTOCOLS 1024
int FindProtocol (
BOOL Reliable,
BOOL MessageOriented,
BOOL StreamOriented,
BOOL Connectionless,
DWORD *ProtocolUsed
)
{
// local variables
INT protocols[MAX_PROTOCOLS+1];
BYTE buffer[2048];
DWORD bytesRequired;
INT err;
PPROTOCOL_INFO protocolInfo;
INT protocolCount;
INT i;
DWORD protocolIndex;
// PCSADDR_INFO csaddrInfo;
// INT addressCount;
// SOCKET s;
// First look up the protocols installed on this computer.
//
bytesRequired = sizeof(buffer);
err = EnumProtocols( NULL, buffer, &bytesRequired );
if ( err <= 0 )
return SOCKET_ERROR;
// Walk through the available protocols and pick out the ones which
// support the desired characteristics.
//
protocolCount = err;
protocolInfo = (PPROTOCOL_INFO)buffer;
for ( i = 0, protocolIndex = 0;
i < protocolCount && protocolIndex < MAX_PROTOCOLS;
i++, protocolInfo++ ) {
// If connection-oriented support is requested, then check if
// supported by this protocol. We assume here that connection-
// oriented support implies fully reliable service.
//
if ( Reliable ) {
// Check to see if the protocol is reliable. It must
// guarantee both delivery of all data and the order in
// which the data arrives.
//
if ( (protocolInfo->dwServiceFlags &
XP_GUARANTEED_DELIVERY) == 0
||
(protocolInfo->dwServiceFlags &
XP_GUARANTEED_ORDER) == 0 ) {
continue;
}
// Check to see that the protocol matches the stream/message
// characteristics requested.
//
if ( StreamOriented &&
(protocolInfo->dwServiceFlags & XP_MESSAGE_ORIENTED)
!= 0 &&
(protocolInfo->dwServiceFlags & XP_PSEUDO_STREAM)
== 0 ) {
continue;
}
if ( MessageOriented &&
(protocolInfo->dwServiceFlags & XP_MESSAGE_ORIENTED)
== 0 ) {
continue;
}
}
else if ( Connectionless ) {
// Make sure that this is a connectionless protocol.
//
if ( (protocolInfo->dwServiceFlags & XP_CONNECTIONLESS)
!= 0 )
continue;
}
// This protocol fits all the criteria. Add it to the list of
// protocols in which we're interested.
//
protocols[protocolIndex++] = protocolInfo->iProtocol;
}
*ProtocolUsed = (INT) protocolIndex;
return 0;
}
nspapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして EnumProtocols を定義します。エンコーディング中立のエイリアスの使用を、エンコーディング中立でないコードと混在させると、不一致が生じてコンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// MSWSOCK.dll (Unicode / -W)
#include <windows.h>
INT EnumProtocolsW(
INT* lpiProtocols, // optional
void* lpProtocolBuffer,
DWORD* lpdwBufferLength
);[DllImport("MSWSOCK.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern int EnumProtocolsW(
IntPtr lpiProtocols, // INT* optional
IntPtr lpProtocolBuffer, // void* out
ref uint lpdwBufferLength // DWORD* in/out
);<DllImport("MSWSOCK.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function EnumProtocolsW(
lpiProtocols As IntPtr, ' INT* optional
lpProtocolBuffer As IntPtr, ' void* out
ByRef lpdwBufferLength As UInteger ' DWORD* in/out
) As Integer
End Function' lpiProtocols : INT* optional
' lpProtocolBuffer : void* out
' lpdwBufferLength : DWORD* in/out
Declare PtrSafe Function EnumProtocolsW Lib "mswsock" ( _
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
EnumProtocolsW = ctypes.windll.mswsock.EnumProtocolsW
EnumProtocolsW.restype = ctypes.c_int
EnumProtocolsW.argtypes = [
ctypes.POINTER(ctypes.c_int), # lpiProtocols : INT* optional
ctypes.POINTER(None), # lpProtocolBuffer : void* 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('MSWSOCK.dll')
EnumProtocolsW = Fiddle::Function.new(
lib['EnumProtocolsW'],
[
Fiddle::TYPE_VOIDP, # lpiProtocols : INT* optional
Fiddle::TYPE_VOIDP, # lpProtocolBuffer : void* out
Fiddle::TYPE_VOIDP, # lpdwBufferLength : DWORD* in/out
],
Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "mswsock")]
extern "system" {
fn EnumProtocolsW(
lpiProtocols: *mut i32, // INT* optional
lpProtocolBuffer: *mut (), // void* out
lpdwBufferLength: *mut u32 // DWORD* in/out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("MSWSOCK.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int EnumProtocolsW(IntPtr lpiProtocols, IntPtr lpProtocolBuffer, ref uint lpdwBufferLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'MSWSOCK_EnumProtocolsW' -Namespace Win32 -PassThru
# $api::EnumProtocolsW(lpiProtocols, lpProtocolBuffer, lpdwBufferLength)#uselib "MSWSOCK.dll"
#func global EnumProtocolsW "EnumProtocolsW" wptr, wptr, wptr
; EnumProtocolsW varptr(lpiProtocols), lpProtocolBuffer, varptr(lpdwBufferLength) ; 戻り値は stat
; lpiProtocols : INT* optional -> "wptr"
; lpProtocolBuffer : void* out -> "wptr"
; lpdwBufferLength : DWORD* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "MSWSOCK.dll" #cfunc global EnumProtocolsW "EnumProtocolsW" var, sptr, var ; res = EnumProtocolsW(lpiProtocols, lpProtocolBuffer, lpdwBufferLength) ; lpiProtocols : INT* optional -> "var" ; lpProtocolBuffer : void* out -> "sptr" ; lpdwBufferLength : DWORD* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "MSWSOCK.dll" #cfunc global EnumProtocolsW "EnumProtocolsW" sptr, sptr, sptr ; res = EnumProtocolsW(varptr(lpiProtocols), lpProtocolBuffer, varptr(lpdwBufferLength)) ; lpiProtocols : INT* optional -> "sptr" ; lpProtocolBuffer : void* out -> "sptr" ; lpdwBufferLength : DWORD* in/out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; INT EnumProtocolsW(INT* lpiProtocols, void* lpProtocolBuffer, DWORD* lpdwBufferLength) #uselib "MSWSOCK.dll" #cfunc global EnumProtocolsW "EnumProtocolsW" var, intptr, var ; res = EnumProtocolsW(lpiProtocols, lpProtocolBuffer, lpdwBufferLength) ; lpiProtocols : INT* optional -> "var" ; lpProtocolBuffer : void* out -> "intptr" ; lpdwBufferLength : DWORD* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; INT EnumProtocolsW(INT* lpiProtocols, void* lpProtocolBuffer, DWORD* lpdwBufferLength) #uselib "MSWSOCK.dll" #cfunc global EnumProtocolsW "EnumProtocolsW" intptr, intptr, intptr ; res = EnumProtocolsW(varptr(lpiProtocols), lpProtocolBuffer, varptr(lpdwBufferLength)) ; lpiProtocols : INT* optional -> "intptr" ; lpProtocolBuffer : void* out -> "intptr" ; lpdwBufferLength : DWORD* in/out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
mswsock = windows.NewLazySystemDLL("MSWSOCK.dll")
procEnumProtocolsW = mswsock.NewProc("EnumProtocolsW")
)
// lpiProtocols (INT* optional), lpProtocolBuffer (void* out), lpdwBufferLength (DWORD* in/out)
r1, _, err := procEnumProtocolsW.Call(
uintptr(lpiProtocols),
uintptr(lpProtocolBuffer),
uintptr(lpdwBufferLength),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // INTfunction EnumProtocolsW(
lpiProtocols: Pointer; // INT* optional
lpProtocolBuffer: Pointer; // void* out
lpdwBufferLength: Pointer // DWORD* in/out
): Integer; stdcall;
external 'MSWSOCK.dll' name 'EnumProtocolsW';result := DllCall("MSWSOCK\EnumProtocolsW"
, "Ptr", lpiProtocols ; INT* optional
, "Ptr", lpProtocolBuffer ; void* out
, "Ptr", lpdwBufferLength ; DWORD* in/out
, "Int") ; return: INT●EnumProtocolsW(lpiProtocols, lpProtocolBuffer, lpdwBufferLength) = DLL("MSWSOCK.dll", "int EnumProtocolsW(void*, void*, void*)")
# 呼び出し: EnumProtocolsW(lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
# lpiProtocols : INT* optional -> "void*"
# lpProtocolBuffer : void* 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 "mswsock" fn EnumProtocolsW(
lpiProtocols: [*c]i32, // INT* optional
lpProtocolBuffer: ?*anyopaque, // void* out
lpdwBufferLength: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc EnumProtocolsW(
lpiProtocols: ptr int32, # INT* optional
lpProtocolBuffer: pointer, # void* out
lpdwBufferLength: ptr uint32 # DWORD* in/out
): int32 {.importc: "EnumProtocolsW", stdcall, dynlib: "MSWSOCK.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "mswsock");
extern(Windows)
int EnumProtocolsW(
int* lpiProtocols, // INT* optional
void* lpProtocolBuffer, // void* out
uint* lpdwBufferLength // DWORD* in/out
);ccall((:EnumProtocolsW, "MSWSOCK.dll"), stdcall, Int32,
(Ptr{Int32}, Ptr{Cvoid}, Ptr{UInt32}),
lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
# lpiProtocols : INT* optional -> Ptr{Int32}
# lpProtocolBuffer : void* out -> Ptr{Cvoid}
# lpdwBufferLength : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
int32_t EnumProtocolsW(
int32_t* lpiProtocols,
void* lpProtocolBuffer,
uint32_t* lpdwBufferLength);
]]
local mswsock = ffi.load("mswsock")
-- mswsock.EnumProtocolsW(lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
-- lpiProtocols : INT* optional
-- lpProtocolBuffer : void* 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('MSWSOCK.dll');
const EnumProtocolsW = lib.func('__stdcall', 'EnumProtocolsW', 'int32_t', ['int32_t *', 'void *', 'uint32_t *']);
// EnumProtocolsW(lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
// lpiProtocols : INT* optional -> 'int32_t *'
// lpProtocolBuffer : void* out -> 'void *'
// lpdwBufferLength : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("MSWSOCK.dll", {
EnumProtocolsW: { parameters: ["pointer", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.EnumProtocolsW(lpiProtocols, lpProtocolBuffer, lpdwBufferLength)
// lpiProtocols : INT* optional -> "pointer"
// lpProtocolBuffer : void* 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 EnumProtocolsW(
int32_t* lpiProtocols,
void* lpProtocolBuffer,
uint32_t* lpdwBufferLength);
C, "MSWSOCK.dll");
// $ffi->EnumProtocolsW(lpiProtocols, lpProtocolBuffer, lpdwBufferLength);
// lpiProtocols : INT* optional
// lpProtocolBuffer : void* 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 Mswsock extends StdCallLibrary {
Mswsock INSTANCE = Native.load("mswsock", Mswsock.class, W32APIOptions.UNICODE_OPTIONS);
int EnumProtocolsW(
IntByReference lpiProtocols, // INT* optional
Pointer lpProtocolBuffer, // void* out
IntByReference lpdwBufferLength // DWORD* in/out
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("mswsock")]
lib LibMSWSOCK
fun EnumProtocolsW = EnumProtocolsW(
lpiProtocols : Int32*, # INT* optional
lpProtocolBuffer : Void*, # void* 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 EnumProtocolsWNative = Int32 Function(Pointer<Int32>, Pointer<Void>, Pointer<Uint32>);
typedef EnumProtocolsWDart = int Function(Pointer<Int32>, Pointer<Void>, Pointer<Uint32>);
final EnumProtocolsW = DynamicLibrary.open('MSWSOCK.dll')
.lookupFunction<EnumProtocolsWNative, EnumProtocolsWDart>('EnumProtocolsW');
// lpiProtocols : INT* optional -> Pointer<Int32>
// lpProtocolBuffer : void* out -> Pointer<Void>
// lpdwBufferLength : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function EnumProtocolsW(
lpiProtocols: Pointer; // INT* optional
lpProtocolBuffer: Pointer; // void* out
lpdwBufferLength: Pointer // DWORD* in/out
): Integer; stdcall;
external 'MSWSOCK.dll' name 'EnumProtocolsW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "EnumProtocolsW"
c_EnumProtocolsW :: Ptr Int32 -> Ptr () -> Ptr Word32 -> IO Int32
-- lpiProtocols : INT* optional -> Ptr Int32
-- lpProtocolBuffer : void* out -> Ptr ()
-- lpdwBufferLength : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let enumprotocolsw =
foreign "EnumProtocolsW"
((ptr int32_t) @-> (ptr void) @-> (ptr uint32_t) @-> returning int32_t)
(* lpiProtocols : INT* optional -> (ptr int32_t) *)
(* lpProtocolBuffer : void* out -> (ptr void) *)
(* lpdwBufferLength : DWORD* in/out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library mswsock (t "MSWSOCK.dll"))
(cffi:use-foreign-library mswsock)
(cffi:defcfun ("EnumProtocolsW" enum-protocols-w :convention :stdcall) :int32
(lpi-protocols :pointer) ; INT* optional
(lp-protocol-buffer :pointer) ; void* out
(lpdw-buffer-length :pointer)) ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $EnumProtocolsW = Win32::API::More->new('MSWSOCK',
'int EnumProtocolsW(LPVOID lpiProtocols, LPVOID lpProtocolBuffer, LPVOID lpdwBufferLength)');
# my $ret = $EnumProtocolsW->Call($lpiProtocols, $lpProtocolBuffer, $lpdwBufferLength);
# lpiProtocols : INT* optional -> LPVOID
# lpProtocolBuffer : void* out -> LPVOID
# lpdwBufferLength : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f EnumProtocolsA (ANSI版) — システムが提供するプロトコル情報を列挙する。
- f GetAddressByNameW — サービス名から対応するアドレス情報を取得する。
- s PROTOCOL_INFOW