Introduction
My Ip gives you information about IP addresses, adapter and installed protocols. It gives details about WSADATA structure with comments form MS SDK. No interpretation of the meanings is done this is textually the SDK text. IP addresses are retrieved using the GetNetworkParams function. Another useful feature is IP mask, shown too. This tool could be useful for testing network configurations and for teaching. Beginning programmers could be interesting in the way to detect network parameters. The program is a dialog box including a clock. The WinSock 2.2 DLL must be installed for running My Ip. I have tested My Ip under Windows XP not on others OS. The main WinSock functions used are gethostbyname(...), WSAEnumProtocols(...), GetNumberOfInterfaces(...) and GetNetworkParams(...). I made the tool to detect parameters without beeing connected, and to resolve conflicts between hardware and software. The source code was written with Visual Studio .NET 2002, FRENCH Version. The main work is done with the bool InitNetwork(void)
function.
bool InitNetwork(void)
{
int iResult;
DWORD dwOutBufLen;
char szTmp[256];
iResult = WSAStartup(MAKEWORD(2,2),&WsaData);
if(iResult)
{
wsprintf(szTmp,"WSAStartup failed, returned error code : %d",iResult);
MessageBox(NULL,szTmp,szError,MB_OK|MB_ICONEXCLAMATION);
return (FALSE);
}
memset(szHostName,0,sizeof(szHostName));
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 ;
iResult = WSAEnumProtocols(NULL,lpProtocolBuf,&dwBufLen);
if(WSAENOBUFS != WSAGetLastError())
return (
ShowNetworkError("WSAEnumProtocols failed, returned error code : %d"));
lpProtocolBuf = (WSAPROTOCOL_INFO *) MemoryAlloc(dwBufLen);
if(!lpProtocolBuf)
return (ShowNetworkError("Memory allocation failed"));
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);
}