Re: MFC and COM with WMI
You just need to intialize COM once in for each thread.
You should also be aware of MFC threading problems, such as
MFC application stops responding when you initialize the application as a
multithreaded apartment in Visual C++ .NET or in Visual C++ 2005
http://support.microsoft.com/default.aspx?scid=kb;en-us;828643
Description of CWnd derived MFC objects and multithreaded applications in
Visual C++
http://support.microsoft.com/kb/147578/en-us
"one-trick-pony" <worldofpain.aamir@gmail.com> wrote in message
news:1182963446.162675.59110@n2g2000hse.googlegroups.com...
Greetings,
I have developed an MFC based application which uses WMI for
managment of networking. I am getting some odd behavior with this
application. Basically my question is, how often must I intialize COM
library via CoInitialize(NULL). For example, GUI has several buttons
for displaying/getting or setting network settings. Suppose user
presses a button to display IP address for Adapter 1 in the system.
The code for button connects with WMI and runs CoInitialize(NULL) and
displays IP address. Next user wants to set an IP address(static
address) and clicks on a button and code is executed which again
initializes COM library, connects with WMI and sets adapter IP
address. Is this acceptable or valid to issue COM intialization in
every routine(or upon click of a button)? Also, on the same token, is
it valid to unitialize COM libarary in every routine (or upon click of
a button)?
Should COM library be initialized and unitilized ONLY once upon
execution of the application? This is the approach I am following-I
am initializing COM only once in OnInitDialog routine. The
CoUnintialize is run when application is exited inside Dialog
destructor. I have Dialog class which has both public and private
member methods/functions. My public member function seem to have no
issue. It is my private member function or utility functions that
reported problem with CoCreateInstance call. CoCreateInstance was
failing inside private member function. So I added in CoInitialize
and that fixed the issue. However, my public member function do not
call CoInitialize yet they seem to be working fine.
To recap, public member functions work fine having initialize COM
library in OnInitialDialog routine where as private member function
seem to explicitly require COM library intialization. Is this normal
behavior? If I am doing something wrong, what could it be?
Another mystry is the following: ( only part of the code )
IWbemClassObject * pClassObject = NULL;
VARIANT v;
hRes = pEnumObject->Next(WBEM_INFINITE,1, &pClassObject, &uReturned);
if(hRes != S_OK)
{
if ( hRes == WBEM_S_FALSE ) {
VariantClear( &v );
pIWbemLocator->Release();
pWbemServices->Release();
pEnumObject->Release();
return 2;
}
}
This works ok. However, if I try to release pClassOjbect, program
dies. There are no error messages and program just quits completely.
So the only thing I can think of is, eventhough we have passed
pClassOjbect pointer to be filled, it does not actually instantiate
until it is used to access its members such as:
hRes = pClassObject->Get(L"GetSomeProperty", 0, &v, 0,0);
AFTER above statement is executed, if I issue pClassObject->Release()
no issues are encountered.
Thanks for help