Winsock Functions


EnumProtocols Function

The EnumProtocols function retrieves information about a specified set of network protocols that are active on a local host.

Note  The EnumProtocols function is a Microsoft-specific extension to the Windows Sockets 1.1 specification. This function is obsolete. For the convenience of Windows Sockets 1.1 developers, the reference material is included. The WSAEnumProtocols function provides equivalent functionality in Windows Sockets 2.

Syntax

C++
INT EnumProtocols(
    LPINT lpiProtocols,
    LPVOID lpProtocolBuffer,
    LPDWORD lpdwBufferLength
);

Parameters

lpiProtocols

A pointer to a null-terminated array of protocol identifiers. The EnumProtocols function retrieves information about the protocols specified by this array.

If lpiProtocols is NULL, the function retrieves information about all available protocols.

The following protocol identifier values are defined.

ValueMeaning
IPPROTO_TCP

The Transmission Control Protocol (TCP), a connection-oriented stream protocol.

IPPROTO_UDP

The User Datagram Protocol (UDP), a connectionless datagram protocol.

ISOPROTO_TP4

The ISO connection-oriented transport protocol.

NSPROTO_IPX

The Internet Packet Exchange (IPX) protocol, a connectionless datagram protocol.

NSPROTO_SPX

The Sequenced Packet Exchange (SPX) protocol, a connection-oriented stream protocol.

NSPROTO_SPXII

The Sequenced Packet Exchange (SPX) protocol version 2, a connection-oriented stream protocol.

 

lpProtocolBuffer

A pointer to a buffer that the function fills with an array of PROTOCOL_INFO data structures.

lpdwBufferLength

A pointer to a variable that, on input, specifies the size, in bytes, of the buffer pointed to by lpProtocolBuffer.

On output, the function sets this variable to the minimum buffer size needed to retrieve all of the requested information. For the function to succeed, the buffer must be at least this size.

Return Value

If the function succeeds, the return value is the number of PROTOCOL_INFO data structures written to the buffer pointed to by lpProtocolBuffer.

If the function fails, the return value is SOCKET_ERROR(–1). To get extended error information, call GetLastError, which returns the following extended error code.

Error codeMeaning
ERROR_INSUFFICIENT_BUFFER

The buffer pointed to by lpProtocolBuffer was too small to receive all of the relevant PROTOCOL_INFO structures. Call the function with a buffer at least as large as the value returned in *lpdwBufferLength.

 

Remarks

In the following sample code, the EnumProtocols function retrieves information about all protocols that are available on a system. The code then examines each of the protocols in greater detail.

#include <windows.h>

SOCKET OpenConnection ( 
    PTSTR ServiceName, 
    PGUID ServiceType, 
    BOOL Reliable, 
    BOOL MessageOriented, 
    BOOL StreamOriented, 
    BOOL Connectionless, 
    PINT ProtocolUsed 
    ) 
{ 
    // local variables 
    INT protocols[MAX_PROTOCOLS+1]; 
    BYTE buffer[2048]; 
    DWORD bytesRequired; 
    INT err; 
    PPROTOCOL_INFO protocolInfo; 
    PCSADDR_INFO csaddrInfo; 
    INT protocolCount; 
    INT addressCount; 
    INT i; 
    DWORD protocolIndex; 
    SOCKET s; 
 
    // First look up the protocols installed on this computer. 
    // 
    bytesRequired = sizeof(buffer); 
    err = EnumProtocols( NULL, buffer, &bytesRequired ); 
    if ( err <= 0 ) 
        return INVALID_SOCKET; 
 
    // 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; 
     }
}

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderNspapi.h
LibraryMswsock.lib
DLLMswsock.dll
Unicode and ANSI namesEnumProtocolsW (Unicode) and EnumProtocolsA (ANSI)

See Also

Winsock Reference
Winsock Functions
GetAddressByName
PROTOCOL_INFO

Send comments about this topic to Microsoft

Build date: 11/12/2009

Tags :


Page view tracker