0

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

Community
  • 1
  • 1
8bitcartridge
  • 1,629
  • 6
  • 25
  • 38

2 Answers2

3

GetAdaptersInfo: retrieves adapter information for the local computer.

DWORD GetAdaptersInfo(
  __out    PIP_ADAPTER_INFO pAdapterInfo,
  __inout  PULONG pOutBufLen
);

MSDN for GetAdaptersInfo.

Eric Z
  • 14,327
  • 7
  • 45
  • 69
  • I linked to the GetAdaptersAddresses MSDN article from the one you linked here and tested that code. Seems helpful and I'm looking into it more now. Thanks! – 8bitcartridge Jul 28 '11 at 07:55
0

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
}
Jeeva
  • 4,585
  • 2
  • 32
  • 56