Using the Windows API to detect IP addresses for network cards and ethernet devices
I would like to write an application to detect the IP addresses of all network connections on a Windows computer system, as well as the IP of all ethernet devices connected to that system. Is there an easy way to do this programmatically with the Windows API?
I'm looking for the C++ equivalent to the answer to this question:
How do I check for a network connection? as well as how to enumerate ethernet devices connected.I am aware of Beej's networking guide and I'm actually (slowly) making my way through it, but I thought there if there is a quicker, easier way, that would be fantastic. I searched all over StackOverflow without too 开发者_开发知识库much luck.
If anyone has any advice, or could point me to a useful guide, that would be great!
Thanks!
--R
GetAdaptersInfo: retrieves adapter information for the local computer.
DWORD GetAdaptersInfo(
__out PIP_ADAPTER_INFO pAdapterInfo,
__inout PULONG pOutBufLen
);
MSDN for GetAdaptersInfo.
To get all the IP address of your computer use the following code
char chHostName[MAX_PATH];
if (gethostname(chHostName, sizeof(chHostName)) == SOCKET_ERROR)
{
return "0.0.0.0";
}
struct addrinfo hints;
struct addrinfo *result = NULL;
// Setup the hints address info structure
// which is passed to the getaddrinfo() function
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
dwRetval = getaddrinfo(chHostName, NULL, &hints, &result);
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next)
{
//Parse the address
}
精彩评论