Search code examples
winapinetwork-programmingadapterdefault

How to determine the default network adapter through WinApi?


Is there a way through Windows API to determine which is the primary/default network adapter?

For example, if I have a PC with two network cards, I need to know which one is used by the system to access internet, similarly if I have a network adapter and a virtual adapter.

I tried with GetAdaptersAddresses but it doesn't show which is the favourite one, maybe with GetBestInterface?


Solution

  • This SO Answer explains how to determine the local IP address used to connect to the Internet (like Google's DNS servers), you can then compare this local IP address with the list returned by GetAdaptersAddresses to determine which network card was used for Internet Access.