// by Doug Lyons for MinGW // To Compile use: gcc GetInterfaceInfo.c -liphlpapi -o gii.exe #include #include #include #include #pragma comment(lib, "iphlpapi.lib") #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) #define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) const int ERROR_BUFFER_OVERFLOW = 111; const int ERROR_INSUFFICIENT_BUFFER = 122; const int ERROR_NO_DATA = 232; const int NO_ERROR = 0; int main() { /* Declare and initialize variables */ PIP_INTERFACE_INFO pInfo; pInfo = (IP_INTERFACE_INFO *) malloc( sizeof(IP_INTERFACE_INFO) ); ULONG ulOutBufLen = 0; DWORD dwRetVal = 0; // Make an initial call to GetInterfaceInfo to get // the necessary size in the ulOutBufLen variable if ( GetInterfaceInfo(pInfo, &ulOutBufLen) == ERROR_INSUFFICIENT_BUFFER) { free(pInfo); pInfo = (IP_INTERFACE_INFO *) malloc (ulOutBufLen); } // Make a second call to GetInterfaceInfo to get // the actual data we need if ((dwRetVal = GetInterfaceInfo(pInfo, &ulOutBufLen)) == NO_ERROR ) { printf("\tAdapter Name: %ws\n", pInfo->Adapter[0].Name); printf("\tAdapter Index: %ld\n", pInfo->Adapter[0].Index); printf("\tNum Adapters: %ld\n", pInfo->NumAdapters); // free memory allocated free(pInfo); pInfo = NULL; } else if (dwRetVal == ERROR_NO_DATA) { printf("There are no network adapters with IPv4 enabled on the local system\n"); } else { printf("GetInterfaceInfo 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 ); } return 0; }