+ Reply to Thread
Results 1 to 2 of 2

Thread: Verify whether network adapter is connected or not

  1. #1

    Verify whether network adapter is connected or not

    How can I check the status of the Network adapter?

    Mainly whether it is connected or not using C/C++ in MFC application.

  2. #2
    You can use below example:

    Code:
    BOOL GetNetworkAdapterStatus(CString strNetworkConection)
    {
    	BOOL bIsNetworkConnectionConnected = FALSE ;
    
    	typedef void (WINAPI *PFNNCFREENETCONPROPERTIES) (NETCON_PROPERTIES *) ;
    	HMODULE hModule = LoadLibrary("netshell.dll") ; 
    	if (NULL != hModule)
    	{
    		PFNNCFREENETCONPROPERTIES pfnNcFreeNetconProperties = NULL ;
    		pfnNcFreeNetconProperties = (PFNNCFREENETCONPROPERTIES) GetProcAddress(hModule, "NcFreeNetconProperties") ;
    		
    		if (NULL != pfnNcFreeNetconProperties)
    		{
    			CComPtr<INetConnectionManager> pNetConnectionManager = NULL ;
    
    			HRESULT hResult = CoCreateInstance(CLSID_ConnectionManager, 0, CLSCTX_ALL, __uuidof(INetConnectionManager), (void**) &pNetConnectionManager) ; 
    
    			if (SUCCEEDED(hResult)) 
    			{
    				CComPtr<IEnumNetConnection> pEnumNetConnection = NULL ;  
    				if (SUCCEEDED(pNetConnectionManager->EnumConnections(NCME_DEFAULT, &pEnumNetConnection))) 
    				{
    					INetConnection* pNetConnection = NULL ; 
    					ULONG nCount = 0 ;
    					BOOL bNext = TRUE ;
    					while (bNext && SUCCEEDED(pEnumNetConnection->Next(1, &pNetConnection, &nCount))) 
    					{
    						if (NULL != pNetConnection)
    						{
    							NETCON_PROPERTIES * pNetProperties = NULL ; 
    							if (SUCCEEDED(pNetConnection->GetProperties(&pNetProperties))) 
    							{ 
    								USES_CONVERSION ;
    									if (wcscmp(pNetProperties->pszwName, T2W(strNetworkConection.GetBuffer(0))) == 0) 
    								{ 
    									if (pNetProperties->Status == NCS_CONNECTED)
    									{
    										bIsNetworkConnectionConnected = TRUE ;
    										bNext = FALSE ;
    									}
    								} 
    
    								pfnNcFreeNetconProperties(pNetProperties) ; 
    								pNetProperties = NULL ;
    							} 
    
    							pNetConnection->Release() ;
    						}
    						else
    						{
    							bNext = FALSE ;
    						}
    					} 
    				}
    			}
    		}
    	}
    
    	return bIsNetworkConnectionConnected ;
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts