Answer the question
In order to leave comments, you need to log in
How to get default gateway?
I needed a function that returns the ip of the default gateway.
After a lot of googling this came up:
#include <QtNetwork>
#include "Iphlpapi.h"
............
QString getDefaultGateway(void)
{
union unIPAddr {
struct {
unsigned char p1;
unsigned char p2;
unsigned char p3;
unsigned char p4;
} bIPAddr;
DWORD dwIPAddr;
};
MIB_IPFORWARDROW BestRoute;
DWORD dwResult = GetBestRoute(inet_addr("0.0.0.0"), 0, &BestRoute);
unIPAddr IPAddrF;
IPAddrF.dwIPAddr = BestRoute.dwForwardNextHop;
QString qsDGWIp = QString::number(IPAddrF.bIPAddr.p1) + "." + QString::number(IPAddrF.bIPAddr.p2) + "." + QString::number(IPAddrF.bIPAddr.p3) + "." + QString::number(IPAddrF.bIPAddr.p4) ;
return qsDGWIp;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question