CSocketWrench::GetVpnAdapter Method  
 
BOOL GetVpnAdapter(
  LPTSTR lpszAdapterName,  
  INT nMaxLength,  
  INT nAddressFamily,  
  LPINTERNET_ADDRESS lpAddress  
);
BOOL GetVpnAdapter(
  CString &strAdapterName,  
  INT nAddressFamily,  
  LPINTERNET_ADDRESS lpAddress  
);

The GetVpnAdapter method returns the name of the active VPN adapter.

Parameters

lpszAdapterName
A null-terminated string buffer which will contain the name of the active VPN adapter if one is found. If a VPN was not detected, the buffer will contain an empty string when the method returns. This parameter may be NULL if the adapter name is not required. An alternate version of this method accepts a reference to a CString object which will contain the adapter name when the method returns.
nMaxLength
The maximum number of characters which can be copied into the string buffer, including the terminating null character. If this value is zero, the lpszAdapterName parameter is ignored.
nAddressFamily
An optional integer value which identifies the type of IP address to return. It may be one of the following values:
Value Description
INET_ADDRESS_ANY Return the IP address for the adapter in either IPv4 or IPv6 format, depending on how the host name can be resolved. By default, a preference will be given for returning an IPv4 address. However, if the adapter only has an IPv6 address, that value will be returned.
INET_ADDRESS_IPV4 Specifies that the adapter address should be returned in IPv4 format. The first four bytes of the ipNumber member of the INTERNET_ADDRESS structure are significant and contains the IP address. The remaining bytes are not significant and an application should not depend on them having any particular value, including zero.
INET_ADDRESS_IPV6 Specifies that the adapter address should be returned in IPv6 format. All bytes in the ipNumber member of the INTERNET_ADDRESS structure are significant. Note that it is possible for an IPv6 address to actually represent an IPv4 address. This is indicated by the first 10 bytes of the address being zero.
lpAddress
A pointer to an INTERNET_ADDRESS structure that will contain the IP address assigned to the VPN adapter when the function returns. This parameter may be NULL or omitted if this information is not required. If this parameter is NULL, the value of the nAddressFamily parameter is ignored.

Return Value

If the method succeeds, the return value is non-zero if an active VPN adapter was found. If no VPN adapter was detected, this method will return zero.

Remarks

The GetVpnAdapter method provides a best-effort method to detect if the system is currently using a Virtual Private Network (VPN) connection. The detection logic is based on identifying active network adapters with names or descriptions that match known VPN software or common VPN adapter naming conventions, while explicitly excluding adapters associated with virtualization platforms (such as Hyper-V, VMware, and VirtualBox), loopback interfaces, and other non-VPN virtual adapters.

Because there is no industry standard for how VPN clients are identified at the network adapter level, this method uses a heuristic approach and cannot guarantee detection in every scenario. Some VPN clients may use custom adapter names or attempt to obscure their presence, and new VPN software may not be recognized until the internal detection list is updated. There is also a small chance that non-VPN software using similar naming conventions could result in a false positive.

The result of this method should be treated as a hint for diagnostic or troubleshooting purposes. It should not be used to restrict access to your application or deny service to users based solely on the possible presence of a VPN connection. If you specify an address family and the lpAddress parameter is not NULL, it will contain the first IP address bound to the adapter interface. It is possible for VPN adapters to be bound to multiple IP addresses. The FormatAddress method can be used to convert the INTERNET_ADDRESS value to a printable string.

The adapter name returned by this method is the description of the detected VPN adapter, encoded as a UTF-8 string in the ANSI version of this function. If you require more detailed network information, consider using the EnumNetworkAddresses method. You can use the GetExternalAddress method to obtain the public-facing IP address allocated for the client.

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cswsock12.h
Import Library: cswskv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also