65.9K
CodeProject 正在变化。 阅读更多。
Home

获取地址 IP 信息

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.17/5 (9投票s)

2003年2月17日

viewsIcon

111905

downloadIcon

4913

IP、已安装的协议、一些有用的信息

Sample Image - V2.jpg

引言

My Ip 提供了关于 IP 地址、适配器和已安装协议的信息。它提供了带有 MS SDK 注释的 WSADATA 结构体的详细信息。没有对这些含义进行解释,而是直接引用了 SDK 文本。IP 地址使用 GetNetworkParams 函数检索。另一个有用的功能是 IP 掩码,也显示出来了。这个工具对于测试网络配置和教学可能很有用。初学者可能会对检测网络参数的方式感兴趣。该程序是一个包含时钟的对话框。要运行 My Ip,必须安装 WinSock 2.2 DLL。我已经在 Windows XP 下测试过 My Ip,没有在其他操作系统上测试。主要使用的 WinSock 函数是 gethostbyname(...), WSAEnumProtocols(...), GetNumberOfInterfaces(...) 和 GetNetworkParams(...)。我制作这个工具是为了在未连接的情况下检测参数,并解决硬件和软件之间的冲突。源代码是用 Visual Studio .NET 2002(法语版本)编写的。主要工作是在 bool InitNetwork(void) 函数中完成的。

bool InitNetwork(void)
{
    int     iResult;
    DWORD   dwOutBufLen;

    char    szTmp[256];

    iResult = WSAStartup(MAKEWORD(2,2),&WsaData);
    if(iResult)
    {
        // Can't call WSAGetLastError() because WinSock DLL is not loaded !

        wsprintf(szTmp,"WSAStartup failed, returned error code : %d",iResult);
        MessageBox(NULL,szTmp,szError,MB_OK|MB_ICONEXCLAMATION);
        return (FALSE);
    }

    memset(szHostName,0,sizeof(szHostName));




// Getting hostname if(gethostname(szHostName,sizeof(szHostName)) == SOCKET_ERROR)
return (ShowNetworkError("gethostname failed, returned error code : %d")); lpHostEnt = gethostbyname(szHostName); if(!lpHostEnt)
return (
ShowNetworkError("gethostbyname failed, returned error code : %d")); dwBufLen = 0 ; // Setting to 0, to have the real size of the new buffer iResult = WSAEnumProtocols(NULL,lpProtocolBuf,&dwBufLen); if(WSAENOBUFS != WSAGetLastError())
return (
ShowNetworkError("WSAEnumProtocols failed, returned error code : %d")); // The buffer is too small, try to reallocate one with the desired size lpProtocolBuf = (WSAPROTOCOL_INFO *) MemoryAlloc(dwBufLen); if(!lpProtocolBuf)
return (ShowNetworkError("Memory allocation failed"));
// Returns number of protocols installed iNumberOfProtocols = WSAEnumProtocols(NULL, lpProtocolBuf,&dwBufLen) ;
if(iNumberOfProtocols == SOCKET_ERROR) { MemoryFree(lpProtocolBuf); return (
ShowNetworkError("WSAEnumProtocols failed, returned error code : %d")); } lpFixedInfo = (FIXED_INFO *) MemoryAlloc(sizeof(FIXED_INFO)); if(!lpFixedInfo) { MemoryFree(lpProtocolBuf) ; return (ShowNetworkError("Memory allocation failed")) ; } ulOutBufLen = sizeof(FIXED_INFO) ; iResult = (int) GetNetworkParams(lpFixedInfo,&ulOutBufLen); if(iResult == ERROR_BUFFER_OVERFLOW) { MemoryFree(lpFixedInfo) ; lpFixedInfo = (FIXED_INFO *) MemoryAlloc(ulOutBufLen); if(!lpFixedInfo) { MemoryFree(lpProtocolBuf) ; return (ShowNetworkError("Memory allocation failed")); } } else { if(iResult != ERROR_SUCCESS) { MemoryFree(lpFixedInfo) ; MemoryFree(lpProtocolBuf) ; return (
ShowNetworkError("GetNetworkParams failed, returned error code : %d")); } } if(iResult = (int) GetNetworkParams(lpFixedInfo,&ulOutBufLen)) { if(iResult != ERROR_SUCCESS) { MemoryFree(lpFixedInfo) ; MemoryFree(lpProtocolBuf) ; return (
ShowNetworkError("GetNetworkParams failed, returned error code : %d")); } } dwOutBufLen = sizeof(IP_ADAPTER_INFO) ; lpAdapterInfo = (PIP_ADAPTER_INFO) MemoryAlloc(dwOutBufLen); if(!lpAdapterInfo) { MemoryFree(lpFixedInfo); MemoryFree(lpProtocolBuf); return (ShowNetworkError("Memory allocation failed")); } iResult = GetAdaptersInfo(lpAdapterInfo,&dwOutBufLen); if(iResult == ERROR_BUFFER_OVERFLOW) { MemoryFree(lpAdapterInfo) ; lpAdapterInfo = (PIP_ADAPTER_INFO) MemoryAlloc(dwOutBufLen); if(!lpAdapterInfo) { MemoryFree(lpFixedInfo); MemoryFree(lpProtocolBuf); return (ShowNetworkError("Memory allocation failed")); } } else { if(iResult != ERROR_SUCCESS) { MemoryFree(lpFixedInfo); MemoryFree(lpProtocolBuf); MemoryFree(lpAdapterInfo); return (
ShowNetworkError("GetAdaptersInfo failed, returned error code : %d")); } } dwNumInterfaces = 0; GetNumberOfInterfaces(&dwNumInterfaces); return (WSACleanup() != SOCKET_ERROR); }
© . All rights reserved.