🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #798

WMI Network unplugged

#798
From: JohnG
Date: Mon, 14 Jan 2008 14:38
100 lines
3311 bytes
Hi,


I am trying to create code to detect when a network cable is unplugged.My 
intentions is to place code on a thread to monitor network connections 
and detect when a cable plugged\unplugged.

I have tried to combine code from various examples but I am not getting
the desired result. If I remove the WBEM_FLAG_RETURN_IMMEDIATELY flag
 the query fails to execute. If I leave the WBEM_FLAG_RETURN_IMMEDIATELY 
flag I fail to iterate through the results.


HRESULT hr = ::CoInitialize(NULL);
if ( FAILED( hr ) )
{
   ShowMessage("COM initialization failed");
   return;
}

// setup process-wide security context
hr = CoInitializeSecurity( NULL, // we're not a server
   		           -1, // we're not a server
			   NULL, // dito
			   NULL, // reserved
			   RPC_C_AUTHN_LEVEL_DEFAULT, // let DCOM decide
			   RPC_C_IMP_LEVEL_IMPERSONATE,
			   NULL,
			   EOAC_NONE,
			   NULL );

if ( FAILED( hr ) )
{
  ShowMessage("Security initialization failed");
  return;
}

int result = 0;
// connect to WMI
TComInterface< IWbemLocator > locator;
hr = CoCreateInstance( CLSID_WbemLocator, NULL,
     		       CLSCTX_INPROC_SERVER,
		       IID_IWbemLocator, reinterpret_cast< void** >( &locator ) );

if ( FAILED( hr ) )
{
   ShowMessage("Instantiation of IWbemLocator failed");
   return;
}

// connect to local service with current credentials
TComInterface< IWbemServices > service;
hr = locator->ConnectServer( L"root\\cimv2", NULL, NULL, NULL,
       			     WBEM_FLAG_CONNECT_USE_MAX_WAIT,
		 	     NULL, NULL, &service );

if ( SUCCEEDED( hr ) )
{
   // execute a query
   TComInterface< IEnumWbemClassObject > enumerator;
   hr = service->ExecQuery( L"WQL",
                            L"select * from MSNdis_MediaConnectStatus where "
                             "InstanceName =""\Intel(R) PRO/1000 MT Network Connection\"" ,
                             WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &enumerator );


   if ( SUCCEEDED( hr ) )
   {
	TComInterface< IWbemClassObject > ping;
	ULONG retcnt;

	// wait for result to become available
	while ( ( hr = enumerator->Next( 1L, 1L, &ping, &retcnt ) ) == WBEM_S_TIMEDOUT );
	
        if ( SUCCEEDED( hr ) )
	{
	   // add code to get connection status

        }
	else
	{
	   ShowMessage( "Error in iterating through enumeration" );
	   result;
	}
   }
   else
   {
     ShowMessage( "Query failed" );
     result;
   }
}
else
{
  ShowMessage( "Couldn't connect to service" );
  result;
	
}

CoUninitialize();
Technical Details
Message-ID: <478b8203$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,382c3d02983b6dd6,start
X-Google-Attributes: gid10cc67,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "JohnG" <whatis@email.com>
Sender: "JohnG" <whatis@email.com>
Reply-To: "JohnG" <whatis@email.com>
Subject: WMI Network unplugged
Newsgroups: borland.public.cppbuilder.nativeapi
X-User-Info: 138.11.0.153     
X-Forwarded-For: unknown
NNTP-Posting-Host: 207.105.83.66
Message-ID: <478b8203$1@newsgroups.borland.com>
Date: 14 Jan 2008 07:38:43 -0700
X-Trace: newsgroups.borland.com 1200325123 207.105.83.66 (14 Jan 2008 07:38:43 -0700)
Lines: 101
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail