程序如下:
#include"..\common\initsock.h"
#include<windows.h>
#include<stdio.h>
LPWSAPROTOCOL_INFO GetProvider(LPINT lpnTotalProtocols)
{
DWORD dwSize=0;
LPWSAPROTOCOL_INFO pProtoInfo=NULL;
//取得需要的缓冲区长度
if(::WSAEnumProtocols(NULL,pProtoInfo,&dwSize)==SOCKET_ERROR)
{
if(::WSAGetLastError()!=WSAENOBUFS)
return NULL;
}
//申请缓冲区,再次调用WSAEnumEnuProtocols函数
pProtoInfo=(LPWSAPROTOCOL_INFO)::GlobalAlloc(GPTR,dwSize);
*lpnTotalProtocols=::WSAEnumProtocols(NULL,pProtoInfo,&dwSize);
//return pProtoInfo;
}
void FreeProvider(LPWSAPROTOCOL_INFO pProtoInfo)
{
::GlobalFree(pProtoInfo);
}
CInitSock theSock;
void main()
{
int nTotalProtocols;
LPWSAPROTOCOL_INFO pProtoInfo=GetProvider(&nTotalProtocols);
if(pProtoInfo!=NULL)
{
//打印出各个提供者的协议信息
for(int i=0;i<nTotalProtocols;i++)
{
printf(" Protocol: %s\n",pProtoInfo[i].szProtocol);
printf("CatalogEntryId: %d ChainLen: %d\n\n",
pProtoInfo[i].dwCatalogEntryId,pProtoInfo[i].ProtocolChain.ChainLen);
}
}
}
运行程序出现这样的提示:
WSAEnumProtocols.exe已停止工作
Windows可以联机检查该问题的解决方案
联机检查解决方案并关闭程序
关闭程序
调试程序
即如下:
原因是:该LPWSAPROTOCOL_INFO GetProvider(LPINT lpnTotalProtocols)函数没有返回值,仔细看上面,你会发现 return语句被我注释掉了的
解决:将LPWSAPROTOCOL_INFO GetProvider(LPINT lpnTotalProtocols)函数中语句//return pProtoInfo;改为return pProtoInfo;