四、添加代码

五、添加对象/库模块

1、 于“GetNetProtocolsDlg.cpp”文件内添加包含语句

  1. #include "stdafx.h"  
  2. #include "GetNetProtocols.h"  
  3. #include "GetNetProtocolsDlg.h"  
  4. #include <winsock2.h> 

2、添加获取网络协议的函数代码

  1. void CGetNetProtocolsDlg::OnGetnetprotocols()   
  2. {  
  3. // TODO: Add your control notification handler code here  
  4. WSADATA WSAData;  
  5.     int i, nRet;  
  6.     DWORD dwErr;  
  7.     WSAPROTOCOL_INFO *lpProtocolBuf = NULL;  
  8.     DWORD dwBufLen = 0;  
  9.  
  10. CString strTemp;  
  11.  
  12.     if (WSAStartup(MAKEWORD(2,2), &WSAData))  
  13. {  
  14.         strTemp.Format("WSAStartup %d", WSAGetLastError());  
  15.    m_ListProtocols.AddString(strTemp);  
  16. }  
  17.     else//第一层  
  18.     {  
  19.    //方法:WSAEnumProtocols获得计算机安装的协议  
  20.         // First, have WSAEnumProtocols tell you how big a buffer you need.  
  21.         nRet = WSAEnumProtocols(NULL, lpProtocolBuf, &dwBufLen);  
  22.         if (SOCKET_ERROR != nRet)  
  23.    {  
  24.     strTemp.Format("WSAEnumProtocols: should not have succeeded\r\n");  
  25.     m_ListProtocols.AddString(strTemp);  
  26.    }  
  27.         else if (WSAENOBUFS != (dwErr = WSAGetLastError()))  
  28.    {  
  29.             // WSAEnumProtocols failed for some reason not relating to buffer size - also odd.  
  30.             strTemp.Format("WSAEnumProtocols(1): %d\r\n", WSAGetLastError());  
  31.     m_ListProtocols.AddString(strTemp);  
  32.    }  
  33.         else//第二层  
  34.    {  
  35.             // WSAEnumProtocols failed for the "expected" reason.  
  36.             // Now you need to allocate a buffer that is the right size.  
  37.             lpProtocolBuf = (WSAPROTOCOL_INFO *)malloc(dwBufLen);  
  38.             if (lpProtocolBuf)  
  39.     {  
  40.                 // Now you can call WSAEnumProtocols again with the  
  41.                 // expectation that it will succeed  
  42.                 // because you have allocated a big enough buffer.  
  43.                 nRet = WSAEnumProtocols(NULL, lpProtocolBuf, &dwBufLen);  
  44.                 if (SOCKET_ERROR == nRet)  
  45.      {                      
  46.       strTemp.Format("WSAEnumProtocols(3): %d\r\n", WSAGetLastError());  
  47.       m_ListProtocols.AddString(strTemp);  
  48.      }  
  49.                 else 
  50.                 {  
  51.       // Enumerate the protocols.  
  52.       strTemp.Format("该计算机安装的网络协议有:");  
  53.       m_ListProtocols.AddString(strTemp);  
  54.       for (i=0; i<nRet; i++)  
  55.       {  
  56.        strTemp.Format(" 协议%d:<%s>\r",i+1, lpProtocolBuf[i].szProtocol);  
  57.        m_ListProtocols.AddString(strTemp);  
  58.       }  
  59.                 }  
  60.                 free(lpProtocolBuf);  
  61.     }//if (lpProtocolBuf)结束  
  62.    }//第二层else结束  
  63.     }//第一层else结束  
  64. //调用WSACleanup函数进行WinSock的清理工作,以便释放其占用的资源  
  65.     WSACleanup();   

点“工程”,点“设置”,点选“连接”,添加“Ws2_32.lib ”模块。

获取网络协议的步骤和代码

六、编译

七、运行

获取网络协议的步骤和代码


相关内容

    暂无相关文章