Network Connectivity APIs
This section provides details on the Windows APIs that allow applications to interact with and manage network connectivity. Understand how to detect network status, manage connections, and handle network-related events.
InternetGetConnectedStateEx
Determines whether the system is connected to the Internet. This function can also return information about the type of connection and the proxy settings.
Syntax
BOOL InternetGetConnectedStateEx(
LPDWORD lpdwFlags,
LPSTR lpszConnectionName,
DWORD dwNameLen,
DWORD dwReserved
);
Parameters
lpdwFlags: A pointer to a DWORD that receives a bitmask of flags indicating the status of the connection.
lpszConnectionName: A pointer to a buffer that receives the name of the connection, if available.
dwNameLen: The size, in characters, of the lpszConnectionName buffer.
dwReserved: Reserved. Must be zero.
Return Value
Returns TRUE if the system is connected to the Internet, and FALSE otherwise.
Remarks
- This function provides a higher-level view of network connectivity compared to
InternetGetConnectedState. - It can distinguish between different types of network connections, such as dial-up, LAN, and wireless.
See Also
Network List Manager (NLM) APIs
The Network List Manager provides a set of COM interfaces that enable applications to discover network connections, retrieve network properties, and receive notifications about network status changes.
Key Interfaces
INetworkListManager: The primary interface for querying network information and registering for notifications.INetwork: Represents a single network connection.INetworkConnection: Represents a connection to a network.
Common Operations
- Enumerating all available networks.
- Getting the network category (Public, Private, DomainAuthenticated).
- Checking the connectivity status of a network.
- Registering for notifications when network status changes.
Example Usage (Conceptual)
// Get the Network List Manager
INetworkListManager* pNlm;
CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, IID_INetworkListManager, (void**)&pNlm);
// Get all networks
IEnumNetworks* pEnumNetworks;
pNlm->EnumNetworks(NLM_ENUM_NETWORK_CONNECTED, &pEnumNetworks);
// Iterate through networks and get properties...
// ...
// Release COM objects
pNlm->Release();
pEnumNetworks->Release();
See Also
Windows.Networking Namespace (UWP)
For Universal Windows Platform (UWP) applications, the Windows.Networking namespace provides modern APIs for network operations, including connectivity management.
Key Classes
ConnectionProfile: Represents a network connection profile, providing information about the current connection.NetworkInformation: A static class providing access to network connectivity status and properties.
Common Operations
- Getting the current connection profile.
- Checking the network connectivity level (e.g., InternetAccess, LocalAccess).
- Subscribing to connectivity change events.
Example Usage (C# for UWP)
using Windows.Networking.Connectivity;
// Get the current connection profile
ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
if (profile != null)
{
// Get connectivity level
NetworkConnectivityLevel level = profile.GetNetworkConnectivityLevel();
if (level == NetworkConnectivityLevel.InternetAccess)
{
// Connected to the internet
}
}
// Subscribe to connectivity change events
NetworkInformation.NetworkStatusChanged += NetworkStatusChangedHandler;
void NetworkStatusChangedHandler(object sender)
{
// Handle network status changes
}