WSAEnumProtocolsW 函式 (winsock2.h)

WSAEnumProtocols 函式會擷取可用傳輸通訊協定的相關資訊。

語法

int WSAAPI WSAEnumProtocolsW(
  [in]      LPINT               lpiProtocols,
  [out]     LPWSAPROTOCOL_INFOW lpProtocolBuffer,
  [in, out] LPDWORD             lpdwBufferLength
);

參數

[in] lpiProtocols

iProtocol 值的 Null終止陣列。 此參數是選擇性的;如果 lpiProtocolsNull,則會傳回所有可用通訊協定的相關資訊。 否則,只會針對陣列中列出的通訊協定擷取資訊。

[out] lpProtocolBuffer

緩衝區的指標,其填入 WSAPROTOCOL_INFO 結構。

[in, out] lpdwBufferLength

在輸入上,傳遞至WSAEnumProtocols之 lpProtocolBuffer緩衝區中的位元組數目。 在輸出上,可以傳遞至 WSAEnumProtocols 的最小緩衝區大小,以擷取所有要求的資訊。 此常式無法列舉多個呼叫;傳入的緩衝區必須夠大,才能保存所有專案,才能讓常式成功。 這可減少 API 的複雜度,且不應該造成問題,因為電腦上載入的通訊協定數目通常很小。

傳回值

如果沒有發生錯誤, WSAEnumProtocols 會傳回要報告的通訊協定數目。 否則,會傳回SOCKET_ERROR的值,而且可以呼叫 WSAGetLastError來擷取特定的錯誤碼。

錯誤碼 意義
WSANOTINITIALISED
使用此函式之前,必須先進行成功的 WSAStartup 呼叫。
WSAENETDOWN
網路子系統失敗。
WSAEINPROGRESS
封鎖的 Windows Sockets 1.1 呼叫正在進行中。
WSAEINVAL
表示其中一個指定的參數無效。
WSAENOBUFS
緩衝區長度太小,無法接收所有相關 WSAPROTOCOL_INFO 結構和相關資訊。 至少傳入緩衝區,與 lpdwBufferLength中所傳回的值一樣大。
WSAEFAULT
一或多個 lpiProtocolslpProtocolBufferlpdwBufferLength 參數不是使用者位址空間的有效部分。

備註

WSAEnumProtocols 函式可用來探索本機電腦上安裝之傳輸通訊協定集合的相關資訊。 在通訊協定鏈結中安裝時,只有應用程式才能使用分層通訊協定。 除了在 lpProtocolBuffer中以零鏈結長度安裝的 LS) P (LSP 之外,不會傳回分層通訊協定的資訊。

注意 分層服務提供者已被取代。 從 Windows 8 和 Windows Server 2012 開始,請使用 Windows 篩選平台
 
lpiProtocols參數可用來做為篩選準則,以限制提供的資訊量。 通常, lpiProtocols 會指定為 Null 指標,這會導致函式傳回所有可用傳輸通訊協定和通訊協定鏈結的資訊。

WSAEnumProtocols函式與WSCEnumProtocolsWSCEnumProtocols32函式不同,WSAEnumProtocols函式不會傳回所有已安裝通訊協定的WSAPROTOCOL_INFO結構。 WSAEnumProtocols函式會排除服務提供者在 WSAPROTOCOL_INFO結構之 dwProviderFlags成員中以PFL_HIDDEN旗標設定的通訊協定,以向 Ws2_32.dll 指出WSAEnumProtocols函式所產生的結果緩衝區中不應傳回此通訊協定。 此外, WSAEnumProtocols 函 式不會傳回鏈結長度為一或更大 (LSP 提供者 ) 之WSAPROTOCOL_INFO 結構的資料。 WSAEnumProtocols只會傳回基底通訊協定和通訊協定鏈結的資訊,這些通訊協定和通訊協定鏈結缺少PFL_HIDDEN旗標,而且沒有零的通訊協定鏈結長度。

每個要求通訊協定的lpProtocolBuffer所指向的緩衝區中會提供WSAPROTOCOL_INFO結構。 如果指定的緩衝區不夠大, (,如 輸入值 lpdwBufferLength ) 所指示, 則會更新 lpdwBufferLength 所指向的值,以指出所需的緩衝區大小。 然後,應用程式應該會取得足夠的緩衝區,然後再次呼叫 WSAEnumProtocols

緩衝區中出現WSAPROTOCOL_INFO結構的順序會與服務提供者使用 WS2_32.DLL 註冊通訊協定專案的順序一致,或是透過 Windows Sockets 應用程式或為建立預設 TCP/IP 提供者所提供的任何後續重新排序。

Windows Phone 8:Windows Phone 8 和更新版本上的 Windows Phone Store 應用程式支援WSAEnumProtocolsW函式。

Windows 8.1Windows Server 2012 R2:Windows 市集應用程式支援WSAEnumProtocolsW函式,Windows 8.1、Windows Server 2012 R2 及更新版本。

範例

下列範例示範如何使用 WSAEnumProtocols 函式來擷取可用傳輸通訊協定 的WSAPROTOCOL_INFO 結構陣列。

#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 預處理器常數的定義,將 WSAEnumProtocols 定義為別名,自動選取此函式的 ANSI 或 Unicode 版本。 混合使用編碼中性別名與非編碼中性的程式碼,可能會導致編譯或執行時間錯誤不符。 如需詳細資訊,請參閱 函式原型的慣例

規格需求

   
最低支援的用戶端 Windows 8.1、Windows Vista [傳統型應用程式 |UWP 應用程式]
最低支援的伺服器 Windows Server 2003 [傳統型應用程式 |UWP 應用程式]
目標平台 Windows
標頭 winsock2.h
程式庫 Ws2_32.lib
Dll Ws2_32.dll

另請參閱

WSAPROTOCOL_INFO

WSCEnumProtocols

WSCEnumProtocols32

Winsock 函式

Winsock 參考