15
votes

I would like to know when a interface has been disabled.

If I go into the windows manager and disable one of the 2 enabled connections, GetIfTable() only returns status about 1 interface, it no longer sees the disconnected one. (Returns 1 table)

How can I get something to return that the disabled interface still exists but is currently disabled?

Thanks.

http://msdn.microsoft.com/en-us/library/aa365943%28VS.85%29.aspx

6

6 Answers

9
votes

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
7
votes

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 disable
bool 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; 
}
6
votes

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

4
votes

The IP_ADAPTER_ADDRESSES structure hold an OperStatus member. See MSDN documentation

I think it can be used to detect disabled NICs. I didn't try.

Here is a test code:

ULONG nFlags= 0;
if (WINVER>=0x0600)                              // flag supported in Vista and later
    nFlags= 0x0100;                              // GAA_FLAG_INCLUDE_ALL_INTERFACES

// during system initialization, GetAdaptersAddresses may return ERROR_BUFFER_OVERFLOW and supply nLen,
// but in a subsequent call it may return ERROR_BUFFER_OVERFLOW and supply greater nLen !
ULONG nLen= sizeof (IP_ADAPTER_ADDRESSES);
BYTE* pBuf= NULL;
DWORD nErr= 0   ;
do
{
    delete[] pBuf;
    pBuf= new BYTE[nLen];
    nErr= ::GetAdaptersAddresses(AF_INET, nFlags, NULL, (IP_ADAPTER_ADDRESSES*&)pBuf, &nLen);
}
while (ERROR_BUFFER_OVERFLOW == nErr);

if (NO_ERROR != nErr)
{
    delete[] pBuf;

    TCHAR czErr[300]= _T("GetAdaptersAddresses failed. ");
    REPORT(REP_ERROR, _T("GetAdapterInfo"), GetSysErrStr(nErr, czErr, 300));
    return false;
}

const IP_ADAPTER_ADDRESSES* pAdaptersAddresses= (IP_ADAPTER_ADDRESSES*&)pBuf;

while (pAdaptersAddresses) // for each adapter
{
    TCHAR czAdapterName [500]; str_cpy(czAdapterName , 500, pAdaptersAddresses->AdapterName );
    TCHAR czDesc        [500]; str_cpy(czDesc        , 500, pAdaptersAddresses->Description );
    TCHAR czFriendlyName[500]; str_cpy(czFriendlyName, 500, pAdaptersAddresses->FriendlyName);

    const IF_OPER_STATUS& Stat= pAdaptersAddresses->OperStatus; // 1:up, 2:down...

    ...

    pAdaptersAddresses= pAdaptersAddresses->Next;
}
3
votes

According to this CodeGuru forum message, you can query WMI for this information (A C# code is provided there).

To query WMI using C++, see these two links:

3
votes

command-line:

wmic NIC where(ConfigManagerErrorCode=22)get Description,Index,NetConnectionID,PNPDeviceID

Output:

Description                            Index  NetConnectionID                            PNPDeviceID
Broadcom 802.11g Network Adapter       8      WiFi                                       PCI\VEN_14E4&DEV_4320&SUBSYS_041814E4&REV_03\4&31B6CD7&0&00F0
1394 Net Adapter                       13     1394                                       V1394\NIC1394\1B9E0F31E8C00
TAP-Win32 Adapter V9                   14     Steganos Internet Anonym 2012 VPN Adapter  ROOT\NET\0000
VirtualBox Host-Only Ethernet Adapter  24     VirtualBox Host-Only Network               ROOT\NET\0001