// win32console.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
// Module Name: Enum.c
//
// Description:
// Enumerate the protocol entries and print their characteristics.
//
// Compile:
// cl -o enum enum.c ws2_32.lib
//
// Command Line Options:
// NONE
//
//#include <windows.h>
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
//#define _WIN32_WINNT
#include "af_irda.h"
#include <ws2atm.h>
#include <wsipx.h>
#include "atalkwsh.h"
#pragma comment (lib,"Ws2_32.lib")
// Function: EnumearteProtocols
//
// Description:
// Perform the actual enumeration of protocol entries stored by
// Winsock.
//
BOOL EnumerateProtocols(WSAPROTOCOL_INFO **wsapi, DWORD *dwCount)
{
DWORD dwErr,
dwRet,
dwBufLen=0;
int i;
*dwCount = 0;
*wsapi = NULL;
if (SOCKET_ERROR != WSAEnumProtocols(NULL, *wsapi,
&dwBufLen))
{
// This should never happen as there is a NULL buffer
//
printf("WSAEnumProtocols failed!\n");
return FALSE;
}
else if (WSAENOBUFS != (dwErr = WSAGetLastError()))
{
// We failed for some reason not relating to buffer size -
// also odd
//
printf("WSAEnumProtocols failed: %d\n", dwErr);
return FALSE;
}
// Allocate the correct buffer size for WSAEnumProtocols as
// well as the buffer to return
//
*wsapi = (WSAPROTOCOL_INFO *) GlobalAlloc(GMEM_FIXED, dwBufLen);
if (*wsapi == NULL)
{
printf("GlobalAlloc failed: %d\n", GetLastError());
return FALSE;
}
dwRet = WSAEnumProtocols(NULL, *wsapi, &dwBufLen);
if (dwRet == SOCKET_ERROR)
{
printf("WSAEnumProtocols failed: %d\n", WSAGetLastError());
GlobalFree(*wsapi);
return FALSE;
}
*dwCount = dwRet;
return TRUE;
}
//
// Function: main
//
// Description:
// Load the Winsock library and call the protocol enumeration and
// printing routines. Then unload and clean up.
//
int main(int argch, char **argv)
{
WSAPROTOCOL_INFO *wsapi=NULL;
WSADATA wsd;
DWORD dwCount;
int i;
if ( WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
{
printf("Unable to load Winsock2 DLL!\n");
return 1;
}
if (EnumerateProtocols(&wsapi, &dwCount) == FALSE)
{
printf("Failed to enumerate protocols!\n");
return 1;
}
WSACleanup();
system("pause");
return 0;
}
//
#include "stdafx.h"
// Module Name: Enum.c
//
// Description:
// Enumerate the protocol entries and print their characteristics.
//
// Compile:
// cl -o enum enum.c ws2_32.lib
//
// Command Line Options:
// NONE
//
//#include <windows.h>
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
//#define _WIN32_WINNT
#include "af_irda.h"
#include <ws2atm.h>
#include <wsipx.h>
#include "atalkwsh.h"
#pragma comment (lib,"Ws2_32.lib")
// Function: EnumearteProtocols
//
// Description:
// Perform the actual enumeration of protocol entries stored by
// Winsock.
//
BOOL EnumerateProtocols(WSAPROTOCOL_INFO **wsapi, DWORD *dwCount)
{
DWORD dwErr,
dwRet,
dwBufLen=0;
int i;
*dwCount = 0;
*wsapi = NULL;
if (SOCKET_ERROR != WSAEnumProtocols(NULL, *wsapi,
&dwBufLen))
{
// This should never happen as there is a NULL buffer
//
printf("WSAEnumProtocols failed!\n");
return FALSE;
}
else if (WSAENOBUFS != (dwErr = WSAGetLastError()))
{
// We failed for some reason not relating to buffer size -
// also odd
//
printf("WSAEnumProtocols failed: %d\n", dwErr);
return FALSE;
}
// Allocate the correct buffer size for WSAEnumProtocols as
// well as the buffer to return
//
*wsapi = (WSAPROTOCOL_INFO *) GlobalAlloc(GMEM_FIXED, dwBufLen);
if (*wsapi == NULL)
{
printf("GlobalAlloc failed: %d\n", GetLastError());
return FALSE;
}
dwRet = WSAEnumProtocols(NULL, *wsapi, &dwBufLen);
if (dwRet == SOCKET_ERROR)
{
printf("WSAEnumProtocols failed: %d\n", WSAGetLastError());
GlobalFree(*wsapi);
return FALSE;
}
*dwCount = dwRet;
return TRUE;
}
//
// Function: main
//
// Description:
// Load the Winsock library and call the protocol enumeration and
// printing routines. Then unload and clean up.
//
int main(int argch, char **argv)
{
WSAPROTOCOL_INFO *wsapi=NULL;
WSADATA wsd;
DWORD dwCount;
int i;
if ( WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
{
printf("Unable to load Winsock2 DLL!\n");
return 1;
}
if (EnumerateProtocols(&wsapi, &dwCount) == FALSE)
{
printf("Failed to enumerate protocols!\n");
return 1;
}
WSACleanup();
system("pause");
return 0;
}
1.WSADATA
大致说来,在W S A D ATA结构中,返回的唯一有用的信息是w Ve r s i o n和w H i g h Ve r s i o n。
属于最大套接字和最大U D P长度的条目应该从自己正在使用的特定协议目录条目中获取。
上一小节曾谈到这一点。
2.WSAPROTOCOL_INFO
最常用的字段是dwServiceFlags1可以查看该协议的属性.