15

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 Answers 6

8

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
4
  • Which field here is the dis/enabled status?
    – T.T.T.
    May 6, 2011 at 22:13
  • 1
    I don't know from memory, and I'm not near any windows PC's. I'm hoping you can find some more info around this pointer... sry about that for now
    – sehe
    May 6, 2011 at 22:16
  • 1
    I think base on information from PnpInstanceId you should find driver information in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\PCI and there you should see ConfigFlags key. If it is set to 0 NIC card is enabled if on 1 disabled.
    – Zuljin
    May 6, 2011 at 22:58
  • I don't remember this well, but I think firstly you have to call RegOpenKeyEx function to obtain key and then with that key you have to call RegQueryValueEx to get specific value. Please check SO old questions and MSDN documentation how exactly call these functions msdn.microsoft.com/en-us/library/ms724875(v=vs.85).aspx
    – Zuljin
    May 6, 2011 at 23:58
8

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; 
}
1
  • the 'done' variable is not used in the loop - a copy/paste issue :-) May 13, 2011 at 7:21
6
+100

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

1
  • If you find that this WMI class gives you the status you want, note that WMI does provide you with a 'polling service' to periodically check whether the values have changed. Take a look at __InstanceModificationEvent (but if you're building this into an application, be careful as you may find WMI polling to be heavy-weight despite its apparent simplicity.)
    – Daryn
    May 27, 2013 at 23:34
4

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;
}
1
  • Unfortunately, nothing in here indicates enabled. I tried this method and it behaves the same as GetIfTable() only returning status (linked list) for one nic...I expected to at least get info back with a IF_OPER_STATUS....
    – T.T.T.
    May 9, 2011 at 20:44
3

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.