Bagikan melalui


Fungsi EnumProtocolsW (nspapi.h)

Fungsi EnumProtocols mengambil informasi tentang sekumpulan protokol jaringan tertentu yang aktif pada host lokal.

Note Fungsi EnumProtocols adalah ekstensi khusus Microsoft untuk spesifikasi Windows Sockets 1.1. Fungsi ini usang. Untuk kenyamanan pengembang Windows Sockets 1.1, materi referensi disertakan. Fungsi WSAEnumProtocols menyediakan fungsionalitas yang setara di Windows Sockets 2.
 

Sintaksis

INT EnumProtocolsW(
  [in, optional] LPINT   lpiProtocols,
  [out]          LPVOID  lpProtocolBuffer,
  [in, out]      LPDWORD lpdwBufferLength
);

Parameter

[in, optional] lpiProtocols

Pointer ke nullarray pengidentifikasi protokol yang dihentikan. Fungsi EnumProtocols mengambil informasi tentang protokol yang ditentukan oleh array ini.

Jika lpiProtocolsNULL, fungsi mengambil informasi tentang semua protokol yang tersedia.

Nilai pengidentifikasi protokol berikut ditentukan.

Nilai Arti
IPPROTO_TCP
Protokol Kontrol Transmisi (TCP), protokol aliran berorientasi koneksi.
IPPROTO_UDP
Protokol Datagram Pengguna (UDP), protokol datagram tanpa koneksi.
ISOPROTO_TP4
Protokol transportasi berorientasi koneksi ISO.
NSPROTO_IPX
Protokol Pertukaran Paket Internet (IPX), protokol datagram tanpa koneksi.
NSPROTO_SPX
Protokol Pertukaran Paket Berurutan (SPX), protokol aliran berorientasi koneksi.
NSPROTO_SPXII
Protokol Pertukaran Paket Berurutan (SPX) versi 2, protokol aliran berorientasi koneksi.

[out] lpProtocolBuffer

Penunjuk ke buffer yang diisi fungsi dengan array struktur data PROTOCOL_INFO.

[in, out] lpdwBufferLength

Penunjuk ke variabel yang, pada input, menentukan ukuran, dalam byte, dari buffer yang ditunjukkan oleh lpProtocolBuffer.

Pada output, fungsi mengatur variabel ini ke ukuran buffer minimum yang diperlukan untuk mengambil semua informasi yang diminta. Agar fungsi berhasil, buffer harus setidaknya ukuran ini.

Mengembalikan nilai

Jika fungsi berhasil, nilai pengembalian adalah jumlah struktur data PROTOCOL_INFO yang ditulis ke buffer yang ditujukkan oleh lpProtocolBuffer.

Jika fungsi gagal, nilai yang dikembalikan adalah SOCKET_ERROR(–1). Untuk mendapatkan informasi kesalahan yang diperluas, panggil GetLastError, yang mengembalikan kode kesalahan yang diperluas berikut.

Kode kesalahan Arti
ERROR_INSUFFICIENT_BUFFER
Buffer yang ditujukkan oleh lpProtocolBuffer terlalu kecil untuk menerima semua struktur PROTOCOL_INFO yang relevan. Panggil fungsi dengan buffer setidaknya sebesar nilai yang dikembalikan dalam *lpdwBufferLength.

Komentar

Dalam kode sampel berikut, fungsi EnumProtocols mengambil informasi tentang semua protokol yang tersedia pada sistem. Kode kemudian memeriksa masing-masing protokol secara lebih rinci.

#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;
}

Nota

Header nspapi.h mendefinisikan EnumProtocols sebagai alias yang secara otomatis memilih versi ANSI atau Unicode dari fungsi ini berdasarkan definisi konstanta praproscesor UNICODE. Mencampur penggunaan alias encoding-netral dengan kode yang tidak mengodekan-netral dapat menyebabkan ketidakcocokan yang mengakibatkan kesalahan kompilasi atau runtime. Untuk informasi selengkapnya, lihat Konvensi untuk Prototipe Fungsi.

Persyaratan

Syarat Nilai
klien minimum yang didukung Windows 2000 Professional [hanya aplikasi desktop]
server minimum yang didukung Windows 2000 Server [hanya aplikasi desktop]
Platform Target Windows
Header nspapi.h
Pustaka Mswsock.lib
DLL Mswsock.dll

Lihat juga

GetAddressByName

PROTOCOL_INFO

Winsock Functions

Referensi Winsock