We can see some routines that use winsocket. In fact, we all know that ipconfig can meet our requirements. The IP assistant API provides similar functions. getadaptersinfo is used in this article. You can check msdn for the specific parameter meanings.
Example:
// You need to connect to lplpapi. Lib
# Include <iostream>
# Include <atlstr. h>
# Include <iphlpapi. h>
Void gethostip (cstring & address)
{
Ulong Len = 0; address. Empty ();
Getadaptersinfo (null, & Len );
Pip_adapter_info P = static_cast <pip_adapter_info> (malloc (LEN ));
Getadaptersinfo (p, & Len );
For (pip_adapter_info q = P; Q! = NULL; q = Q-> next)
{
Address + = Q-> ipaddresslist. IPaddress. String;
Address + = '/N ';
}
Free (P );
}
Int _ tmain (INT argc, _ tchar * argv [])
{
Cstring address;
Gethostip (Address );
STD: cout <address;
Return 0;
}
The first call, Len returns the required buffer length; the second call gets information. Because there may be more than one network card, each IP address is separated by a line break.