-
Gateway Address
Hi,
I am writing a newtowrk application that needs to obtain the local machines IP address, Gateway address and subnet mask.
I can get the locl IP using gethostbyname(), but I need help, with examples if possible.
1) How can I get the gateway address?
2) How can I get the Subnet Mask?
I have used netsh in the past, If this is the answer then how do I get the addresses into variables that I can use to display the addresses in controls.
Many Thanks
-
You can use GetAdaptersInfo. Here is a sample, error checking is omitted.
Code:
#include <windows.h>
#include <Iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "Iphlpapi.lib")
int main(void)
{
ULONG cbBuf = 0;
PIP_ADAPTER_INFO pAdapter = NULL;
PIP_ADAPTER_INFO pMemory = NULL;
DWORD dwResult = 0;
dwResult = GetAdaptersInfo(NULL, &cbBuf);
pMemory = pAdapter = (PIP_ADAPTER_INFO) malloc(cbBuf);
dwResult = GetAdaptersInfo(pAdapter, &cbBuf);
while (pAdapter)
{
printf("Name: %s\nDescription: %s\nGateway: %s\nSubnet Mask: %s\n",
pAdapter->AdapterName,
pAdapter->Description,
pAdapter->GatewayList.IpAddress.String,
pAdapter->IpAddressList.IpMask.String);
pAdapter = pAdapter->Next;
}
free(pMemory);
getchar();
return 0;
}
-
Many Thanks, but
I am using MS Visual C++ 6.0
The function GetAdaptersInfo is not included nor is the header file Iphlpapi.h
Is there a VC++ way or where can I get the required Iphlpapi.h file?
-
You need to download the latest Platform SDK.
http://www.microsoft.com/msdownload/...sdk/sdkupdate/
Click on "Core SDK" on the left then "Install this SDK" on the right.
Once installed, you'll need to add new "Include files" and "Library files" directories in VC++ 6.0: Tools -> Options -> Directories tab
gg