Thread View: borland.public.cppbuilder.nativeapi
3 messages
3 total messages
Started by JohnG
Mon, 14 Jan 2008 14:38
WMI Network unplugged
Author: JohnG
Date: Mon, 14 Jan 2008 14:38
Date: Mon, 14 Jan 2008 14:38
100 lines
3311 bytes
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();
Re: WMI Network unplugged
Author: Remy Lebeau (TeamB)
Date: Mon, 14 Jan 2008 18:05
Date: Mon, 14 Jan 2008 18:05
73 lines
3210 bytes
3210 bytes
"JohnG" <whatis@email.com> wrote in message
news:478b8203$1@newsgroups.borland.com...
> hr = locator->ConnectServer( L"root\\cimv2", NULL, NULL, NULL,
> WBEM_FLAG_CONNECT_USE_MAX_WAIT,
> NULL, NULL, &service );
You must use a true BSTR, not a raw wchar_t*, when passing strings to COM
objects. You can use the VCL's WideString class to help you with that, ie:
hr = locator->ConnectServer(WideString("root\\cimv2"), ...);
> 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 );
Likewise:
hr = service->ExecQuery(WideString("WQL"), WideString("select * from
MSNdis_MediaConnectStatus where InstanceName = "\Intel(R) PRO/1000 MT
Network Connection\""), ...);
> CoUninitialize();
All of your error handling is exiting your function without calling
CoUninitialize(). You need to call CoUninitialize() for every successful
call to CoInitialize/Ex(), regardless of how your code exits, ie:
{
HRESULT hr = ::CoInitialize(NULL);
if( FAILED(hr) )
{
ShowMessage("COM initialization failed");
return;
}
// ...
if( FAILED(hr) )
{
//...
CoUninitialize(); // <-- add this each time
return;
}
}
If you do not want to call CoUninitialize() manually before each 'return',
then I suggest you change you use the TInitOle class instead of calling
CoInitialize/Ex() manually, ie:
{
TInitOle ole; // constructor calls CoInitialize/Ex()
if( !ole ) // check the result
{
ShowMessage("COM initialization failed");
return;
}
// the rest of your code here ...
// TInitOle destructor will call CoUnitialize() automatically when
the object goes out of scope...
}
Gambit
Re: WMI Network unplugged
Author: JohnG
Date: Tue, 15 Jan 2008 13:35
Date: Tue, 15 Jan 2008 13:35
48 lines
2444 bytes
2444 bytes
> TInitOle ole; // constructor calls CoInitialize/Ex()
Thank you for that piece of advice!!
I have modified a portion of the code so that it correctly
detects the connections status of the specified nic. All that is left if to modify this code to allow me use WMI to monitor events.
TComInterface< IWbemServices > service;
hr = locator->ConnectServer( WideString("root\\cimv2"),
NULL, NULL, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT,
NULL, NULL, &service );
if ( SUCCEEDED( hr ) )
{
IEnumWbemClassObject* pEnumerator = NULL;
hr = service->ExecQuery( WideString("WQL"),
WideString("select * from Win32_NetworkAdapter Where Description = \"Intel(R) PRO/1000 MT Network Connection\" And NetConnectionStatus = 0"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hr))
{
ShowMessage("Query Failed");
return;
}
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE,
1,
&pclsObj,
&uReturn);
if(0 == uReturn)
{
break;
}
VARIANT vtProp;
// Get the value of the Name property
hr = pclsObj->Get(WideString("NetConnectionStatus"),
0, &vtProp, 0, 0);
//add code to check connection
VariantClear(&vtProp);
}
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads