www.pudn.com > NETINFO.rar > adaplist.cpp
#include "adaplist.h"
int GetAdapterCount()
{
char error[PCAP_ERRBUF_SIZE];
void* adapterList = pcap_lookupdev(error);
DWORD dwVersion=GetVersion(); //get the OS version
DWORD dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
int count = 0;
if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4)// Windows '95
{
//parse the string with the adapters - ASCII version
char *str = (char*)adapterList;
while( strcmp(str, "") != 0 )
{
str += strlen(str)+1;
count++;
}
}
else
{
//parse the string with the adapters - UNICODE version
WCHAR *str = (WCHAR*)adapterList;
while( wcscmp(str, L"") != 0 )
{
str += wcslen(str)+1;
count++;
}
}
return count;
}
void* GetAdapterString(int index)
{
char error[PCAP_ERRBUF_SIZE];
/* pcap_lookupdev(error)
* Return the name of a network interface attached to the system, or NULL
* if none can be found. The interface must be configured up; the
* lowest unit number is preferred; loopback is ignored.
*/
void* adapterList = pcap_lookupdev(error);
DWORD dwVersion=GetVersion(); //get the OS version
DWORD dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
int count = 0;
if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4)// Windows '95
{
//parse the string with the adapters - ASCII version
char *str = (char*)adapterList;
while( strcmp(str, "") != 0 )
{
if( count == index )
return str;
count++;
str += strlen(str)+1;
}
return NULL;
}
else
{
//parse the string with the adapters - UNICODE version
WCHAR *str = (WCHAR*)adapterList;
while( wcscmp(str, L"") != 0 )//compare string
{
if( count == index )
return str;
count++;
str += wcslen(str)+1;
}
return NULL;
}
}
void PrintAdapterString()
{
char error[PCAP_ERRBUF_SIZE];
void* adapterList = pcap_lookupdev(error);
DWORD dwVersion=GetVersion(); //get the OS version
DWORD dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
int count = GetAdapterCount();
for(int i=0; i= 0x80000000 && dwWindowsMajorVersion >= 4)// Windows '95
printf("\n%d - %s", i, GetAdapterString(i) );
else
wprintf(L"\n%d - %s", i, GetAdapterString(i) );
}
printf("\n");
}