IP助手函数 GetAdaptersAddressesExample Code This example retrieves the addresses of the adapters associated with the system, and prints the friendly name and description for each adapter. PIP_ADAPTER_ADDRESSES pAddresses; pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(sizeof(IP_ADAPTER_ADDRESSES)); ULONG outBufLen = 0; DWORD dwRetVal = 0;// Make an initial call to GetAdaptersAddresses to get the // size needed into the outBufLen variable if (GetAdaptersAddresses(AF_INET, 0, NULL, pAddresses, &outBufLen) == ERROR_BUFFER_OVERFLOW) { GlobalFree(pAddresses); pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(outBufLen); }// Make a second call to GetAdapters Addresses to get the // actual data we want if ((dwRetVal = GetAdaptersAddresses(AF_INET, 0, NULL, pAddresses, &outBufLen)) == NO_ERROR) { // If successful, output some information from the data we received while (pAddresses) { printf("\tFriendly name: %S\n", pAddresses->FriendlyName); printf("\tDescription: %S\n", pAddresses->Description); pAddresses = pAddresses->Next; } } else { printf("Call to GetAdaptersAddresses failed.\n"); LPVOID lpMsgBuf; if (FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL )) { printf("\tError: %s", lpMsgBuf); } LocalFree( lpMsgBuf ); }
GetAdaptersAddressesExample Code
This example retrieves the addresses of the adapters associated with the system, and prints the friendly name and description for each adapter.
PIP_ADAPTER_ADDRESSES pAddresses;
pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(sizeof(IP_ADAPTER_ADDRESSES));
ULONG outBufLen = 0;
DWORD dwRetVal = 0;// Make an initial call to GetAdaptersAddresses to get the
// size needed into the outBufLen variable
if (GetAdaptersAddresses(AF_INET,
0,
NULL,
pAddresses,
&outBufLen) == ERROR_BUFFER_OVERFLOW) {
GlobalFree(pAddresses);
pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(outBufLen);
}// Make a second call to GetAdapters Addresses to get the
// actual data we want
if ((dwRetVal = GetAdaptersAddresses(AF_INET,
0,
NULL,
pAddresses,
&outBufLen)) == NO_ERROR) {
// If successful, output some information from the data we received
while (pAddresses) {
printf("\tFriendly name: %S\n", pAddresses->FriendlyName);
printf("\tDescription: %S\n", pAddresses->Description);
pAddresses = pAddresses->Next;
}
}
else {
printf("Call to GetAdaptersAddresses failed.\n");
LPVOID lpMsgBuf;
if (FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwRetVal,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL )) {
printf("\tError: %s", lpMsgBuf);
}
LocalFree( lpMsgBuf );
}
struct hostent * pHost; //定义结构体 hostent
int i; //定义变量i
LVITEM lvi;
lvi.mask=LVIF_IMAGE|LVIF_TEXT;
lvi.iItem=0;
lvi.iSubItem=0;
lvi.iImage=0; if(gethostname(szHostName,128)==0) //如果本机的名称查到,则将其名称送入List控件
{
m_ListCtrl.InsertItem(lvi.iSubItem, szHostName,lvi.iImage);
pHost = gethostbyname(szHostName);
for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
{
LPCSTR IPAddress=inet_ntoa (*(struct in_addr *)pHost->h_addr_list[i]);
m_ListCtrl.SetItemText(0,1,IPAddress);
}
}
}