How do I detect a disabled Network Interface Connection from a Windows application? How do I detect a disabled Network Interface Connection from a Windows application? windows windows

How do I detect a disabled Network Interface Connection from a Windows application?


I think you would just need to read the registry.

For example, this is a snippet found on the web of what things should look like:

[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{1E6AF554-25FF-40FC-9CEE-EB899472C5A3}\Connection]"PnpInstanceID"="PCI\\VEN_14E4&DEV_1696&SUBSYS_12BC103C&REV_03\\4&3A321F38&0&10F0""MediaSubType"=dword:00000001"Name"="Lan Name""ShowIcon"=dword:00000000"IpCheckingEnabled"=dword:00000001[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{1E6AF554-25FF-40FC-9CEE-EB899472C5A3}\Connection]"PnpInstanceID"="PCI\\VEN_14E4&DEV_1696&SUBSYS_12BC103C&REV_03\\4&3A321F38&0&10F0""MediaSubType"=dword:00000001"Name"="Lan Name""ShowIcon"=dword:00000000"IpCheckingEnabled"=dword:00000001


How about using the interfaces from netcon.h as illustrated in this example? The code in that example enables and disables the interface programmatically, but I've made some modifications so that you could query the status instead:

#include <netcon.h>// wszName is the name of the connection as appears in Network Connections folder// set bEnable to true to enable and to false to disablebool GetConnectionStatus(LPCWSTR wszName, bool *status){    bool result = false;    if (!status)        return false;    typedef void (__stdcall * LPNcFreeNetconProperties)(NETCON_PROPERTIES* pProps);    HMODULE hmod = LoadLibrary("netshell.dll");    if (!hmod)         return false;     LPNcFreeNetconProperties NcFreeNetconProperties =         (LPNcFreeNetconProperties)GetProcAddress(hmod, "NcFreeNetconProperties");     if (!NcFreeNetconProperties )         return false;     INetConnectionManager * pMan = 0;     HRESULT hres = CoCreateInstance(CLSID_ConnectionManager,                                     0,                                     CLSCTX_ALL,                                     __uuidof(INetConnectionManager),                                     (void**)&pMan);     if (SUCCEEDED(hres))     {         IEnumNetConnection * pEnum = 0;          hres = pMan->EnumConnections(NCME_DEFAULT, &pEnum);         if (SUCCEEDED(hres))         {             INetConnection * pCon = 0;             ULONG count;             while (pEnum->Next(1, &pCon, &count) == S_OK && !done)             {                 NETCON_PROPERTIES * pProps = 0;                 hres = pCon->GetProperties(&pProps);                 if (SUCCEEDED(hres))                 {                     if (wcscmp(pProps->pszwName,wszName) == 0)                     {                         *status = pProps->Status == NCS_CONNECTED;                    }                     NcFreeNetconProperties(pProps);                 }                 pCon->Release();             }             pEnum->Release();         }         pMan->Release();     }     FreeLibrary(hmod);     return result; }


Another option is use the Win32_NetworkAdapter WMI Class , check the NetConnectionStatus and NetEnabled properties.