#include <Winsock2.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
void main(void)
{
WSADATA wsaData;
WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
LPWSAPROTOCOL_INFO info = NULL;
DWORD dwLen;
//因为info还未分配内存,第一次调用必定会失败,但能够得到dwLen的值
if (SOCKET_ERROR == WSAEnumProtocols(NULL, info, &dwLen))
{
int iErrorCode = GetLastError();
if (WSAENOBUFS != iErrorCode)
{
WSACleanup();
return;
}
}
if (dwLen > 0)
{
//如果没有分配内存这一步,将会出错,只传递 &inof是没有用的,注意指针的用法
info = (LPWSAPROTOCOL_INFO)GlobalAlloc(GPTR, dwLen);
if (info == NULL)
{
printf("Allocate memory failed! ErrorCode is %d", GetLastError());
WSACleanup();
return;
}
//第二次调用
int iProtocolCounts = WSAEnumProtocols(NULL, info, &dwLen);
if (SOCKET_ERROR == iProtocolCounts)
{
printf("WSCEnumProtocols Error: %d", GetLastError());
GlobalFree(info);
WSACleanup();
return;
}
//打印本机安装的协议信息
for (int i=0; i<iProtocolCounts; i++)
{
printf("\ni=%d\nProtocolDescription:%s\n", i, info[i].szProtocol);
}
GlobalFree(info);
}
WSACleanup();
}
WSAEnumProtocols的用法
最新推荐文章于 2012-05-11 17:08:23 发布