it seems that wszName is not returned. the following code is build with VC.NET. it return NULL string #include <stdio.h> #include <tchar.h>#include <windows.h> #include <Iphlpapi.h> #pragma comment(lib,"Iphlpapi.lib") #include <comutil.h> #pragma comment(lib,"atl") #pragma comment(lib,"comsupp.lib") int _tmain(int argc, _TCHAR* argv[]) { PIP_ADAPTER_INFO pAdapterInfo = NULL; PIP_ADAPTER_INFO pOriginalPtr; ULONG ulSizeAdapterInfo = 0; DWORD dwStatus; // Find out how big our buffer needs to be to hold the data dwStatus = GetAdaptersInfo(pAdapterInfo, &ulSizeAdapterInfo); if (dwStatus == ERROR_BUFFER_OVERFLOW) { // Allocate a buffer of the appropriate size if (!(pAdapterInfo = (PIP_ADAPTER_INFO)malloc(ulSizeAdapterInfo))) { printf("\n Insufficient Memory "); return(1); } // Obtain the Adapter Info dwStatus = GetAdaptersInfo(pAdapterInfo, &ulSizeAdapterInfo); } if (dwStatus != ERROR_SUCCESS) { return(1); } pOriginalPtr = pAdapterInfo; if (pAdapterInfo == NULL) printf("\n No Interfaces Present.\n"); else { // Step through the adapter list while (pAdapterInfo != NULL) { MIB_IFROW* pIfRow = (MIB_IFROW*)malloc(sizeof(MIB_IFROW)); ZeroMemory(pIfRow,sizeof(MIB_IFROW)); pIfRow->dwIndex = pAdapterInfo->Index; if( NO_ERROR==GetIfEntry(pIfRow)) { _bstr_t bstrname(pIfRow->wszName); printf("index:%d,name:%s\n",pAdapterInfo->Index,(LPCTSTR)bstrname); //MessageBoxW(NULL,pIfRow->wszName,NULL,MB_OK); printf("desc:%s\n",pIfRow->bDescr); } pAdapterInfo = pAdapterInfo->Next; } } return 0; }
我还想确认一下,这个wszName是连接的名字吗?就是那个"本地连接"或"local area connection",是吗?我还能用什么其他的方法得到这个名字吗?
GetAdaptersInfo(NULL, &dwSize);
IP_ADAPTER_INFO* pInfo = (IP_ADAPTER_INFO*)malloc(dwSize);
GetAdaptersInfo(pInfo,&dwSize);MIB_IFROW* pIfRow = (MIB_IFROW*)malloc(sizeof(MIB_IFROW));
pIfRow->dwIndex = pInfo->Index;
GetIfEntry(pIfRow);就这些,详见msdn (ip helper)
dwIfIndex是interface的索引值,pIfRow的其他成员都能正确取值,只有wszName不对
#include <stdio.h>
#include <tchar.h>#include <windows.h>
#include <Iphlpapi.h>
#pragma comment(lib,"Iphlpapi.lib")
#include <comutil.h>
#pragma comment(lib,"atl")
#pragma comment(lib,"comsupp.lib")
int _tmain(int argc, _TCHAR* argv[])
{
PIP_ADAPTER_INFO pAdapterInfo = NULL;
PIP_ADAPTER_INFO pOriginalPtr;
ULONG ulSizeAdapterInfo = 0;
DWORD dwStatus; // Find out how big our buffer needs to be to hold the data
dwStatus = GetAdaptersInfo(pAdapterInfo, &ulSizeAdapterInfo);
if (dwStatus == ERROR_BUFFER_OVERFLOW) {
// Allocate a buffer of the appropriate size
if (!(pAdapterInfo = (PIP_ADAPTER_INFO)malloc(ulSizeAdapterInfo))) {
printf("\n Insufficient Memory ");
return(1);
}
// Obtain the Adapter Info
dwStatus = GetAdaptersInfo(pAdapterInfo, &ulSizeAdapterInfo);
} if (dwStatus != ERROR_SUCCESS) {
return(1);
}
pOriginalPtr = pAdapterInfo; if (pAdapterInfo == NULL)
printf("\n No Interfaces Present.\n");
else
{
// Step through the adapter list
while (pAdapterInfo != NULL)
{
MIB_IFROW* pIfRow = (MIB_IFROW*)malloc(sizeof(MIB_IFROW));
ZeroMemory(pIfRow,sizeof(MIB_IFROW));
pIfRow->dwIndex = pAdapterInfo->Index;
if( NO_ERROR==GetIfEntry(pIfRow))
{
_bstr_t bstrname(pIfRow->wszName);
printf("index:%d,name:%s\n",pAdapterInfo->Index,(LPCTSTR)bstrname);
//MessageBoxW(NULL,pIfRow->wszName,NULL,MB_OK);
printf("desc:%s\n",pIfRow->bDescr);
} pAdapterInfo = pAdapterInfo->Next;
}
}
return 0;
}