Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Please test my GetDNSAddresses() function (MS Windows)

4 views
Skip to first unread message

Rob Pitt

unread,
Jan 26, 2001, 9:50:57 AM1/26/01
to
Could you folks spare 3 minutes to test my GetDNSAddress() function - see
below. The code is completely self contained - just paste it into a file and
compile.

It works ok on Win2000 but would like to know if it works correct on...
1) Windows 95
2) Windows 98
2) Windows NT 4
4) Machines using DHCP, manual configuration plus any other exotic config.

Thanks,

Rob


//GetDNSAddresses() function by Robert Pitt 2001
#include <windows.h>

//FROM <IpTypes.h> header file
#ifndef IP_TYPES_INCLUDED
#define MAX_HOSTNAME_LEN 128
#define MAX_DOMAIN_NAME_LEN 128
#define MAX_SCOPE_ID_LEN 256

typedef struct
{
char String[4 * 4];
} IP_ADDRESS_STRING, IP_MASK_STRING;

typedef struct _IP_ADDR_STRING
{
struct _IP_ADDR_STRING* Next;
IP_ADDRESS_STRING IpAddress;
IP_MASK_STRING IpMask;
DWORD Context;
} IP_ADDR_STRING, *PIP_ADDR_STRING;

typedef struct
{
char HostName[MAX_HOSTNAME_LEN + 4] ;
char DomainName[MAX_DOMAIN_NAME_LEN + 4];
PIP_ADDR_STRING CurrentDnsServer;
IP_ADDR_STRING DnsServerList;
UINT NodeType;
char ScopeId[MAX_SCOPE_ID_LEN + 4];
UINT EnableRouting;
UINT EnableProxy;
UINT EnableDns;
} FIXED_INFO, *PFIXED_INFO;
#endif//<IpTypes.h>


// GetDNSAddresses
// On success returns non-zero and sets bufferOut to a
// "space" delimited list of one or more IP addresses
// e.g. 111.112.113.114 222.223.224.225

int WINAPI GetDNSAddresses(LPSTR bufferOut)
{
LPCSTR szKey95="System\\CurrentControlSet\\Services\\VxD\\MSTCP";
LPCSTR szKeyNT="System\\CurrentControlSet\\Services\\Tcpip\\Parameters";
DWORD (WINAPI *GetNetworkParams)(PFIXED_INFO,PULONG);
FIXED_INFO *pFixedInfo;
IP_ADDR_STRING *pIpAddr;
HINSTANCE hDll;
OSVERSIONINFO osver;
DWORD dwSize,dwType;
HKEY hKey;
char buf[256];
int i;

bufferOut[0]=0;
buf[0]=0;

//PLAN A : Use GetNetworkParams() (if available - Win98 / 2000)
hDll=LoadLibrary("Iphlpapi.dll");
if (hDll)
{
*(FARPROC*)&GetNetworkParams=
GetProcAddress(hDll,"GetNetworkParams");

if (GetNetworkParams)
{
dwSize=0;
GetNetworkParams(NULL,&dwSize);
pFixedInfo=(FIXED_INFO*)GlobalAlloc(GPTR,dwSize);
if (GetNetworkParams(pFixedInfo,&dwSize)==0)
{
pIpAddr=&pFixedInfo->DnsServerList;
for (i=0;pIpAddr;pIpAddr=pIpAddr->Next)
{
i+=wsprintf(buf+i,"%s ",pIpAddr->IpAddress.String);
}
if (i) buf[i-1]=0;//Remove trailing space
}
GlobalFree(pFixedInfo);
}
FreeLibrary(hDll);
if (buf[0])
{
lstrcpy(bufferOut,buf);
return TRUE;
}
}

//PLAN B : Get setting from registry directly
ZeroMemory(&osver,sizeof(osver));
osver.dwOSVersionInfoSize=sizeof(osver);
GetVersionEx(&osver);

if (osver.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
{
//Win 95
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,szKey95,0,KEY_READ,&hKey)==0)
{
buf[0]=0;
dwSize=sizeof(buf);
RegQueryValueEx(hKey,"NameServer",NULL,&dwType,
(LPBYTE)buf,&dwSize);
RegCloseKey(hKey);

if (buf[0])
{
//Convert comma seperated string to space seperated
for (i=0;buf[i];i++)
{
if (buf[i]==',') buf[i]=' ';
}

lstrcpy(bufferOut,buf);
return TRUE;
}
}
}
else if (osver.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
{
//Win NT3/4
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,szKeyNT,0,KEY_READ,&hKey)==0)
{
buf[0]=0;
dwSize=sizeof(buf);
RegQueryValueEx(hKey,"NameServer",NULL,&dwType,
(LPBYTE)buf,&dwSize);
RegCloseKey(hKey);
if (buf[0])
{
lstrcpy(bufferOut,buf);
return TRUE;
}
}
}

//Oh eck its all gone wrong!
return FALSE;
}


//************************

#ifndef _AFXDLL //Test program if not building dll

#include <stdio.h>

void main()
{
char buf[256];
int i;

i=GetDNSAddresses(buf);
printf("GetDNSAddresses returned %d\n",i);
printf("%s\n",buf);
}

#endif


Rob Pitt

unread,
Jan 26, 2001, 1:42:57 PM1/26/01
to
Oh 'eck, I just thought - what on happens with multiple network cards?

Hmmm...

Rob

"Rob Pitt" <Rob...@REMOVEME.cyberdude.com> wrote in message
news:t733maj...@corp.supernews.com...

0 new messages