How to correctly use CoInitialize?
-
It's my understanding that for every call to CoInitialize() you need a call to CoUninitialize(), at least that's my interpretation of the documents on MSDN. Please look at my code:
cParse::cParse() { try { CoInitialize(NULL); /** 1 **/ short vi = 1; xls.CreateInstance(__uuidof(clsXL)); xls->Visible(&vi); } catch(_com_error &e) { bstrDesc = e.Description(); MessageBox(NULL,_com_util::ConvertBSTRToString(bstrDesc),0,0); } // CoUninitialize(); /** 2 **/ } cParse::~cParse() { try { CoInitialize(NULL); /** 3 **/ xls->QuitAll(); } catch(_com_error &e) { bstrDesc = e.Description(); MessageBox(NULL,_com_util::ConvertBSTRToString(bstrDesc),0,0); } CoUninitialize(); /** 4 **/ }
If I use it any other way the program crashes. I'm not sure if what I have done is right or wrong (it works), but I would like a better understanding? The commented numbers are to make any replies easier to understand :D -
It's my understanding that for every call to CoInitialize() you need a call to CoUninitialize(), at least that's my interpretation of the documents on MSDN. Please look at my code:
cParse::cParse() { try { CoInitialize(NULL); /** 1 **/ short vi = 1; xls.CreateInstance(__uuidof(clsXL)); xls->Visible(&vi); } catch(_com_error &e) { bstrDesc = e.Description(); MessageBox(NULL,_com_util::ConvertBSTRToString(bstrDesc),0,0); } // CoUninitialize(); /** 2 **/ } cParse::~cParse() { try { CoInitialize(NULL); /** 3 **/ xls->QuitAll(); } catch(_com_error &e) { bstrDesc = e.Description(); MessageBox(NULL,_com_util::ConvertBSTRToString(bstrDesc),0,0); } CoUninitialize(); /** 4 **/ }
If I use it any other way the program crashes. I'm not sure if what I have done is right or wrong (it works), but I would like a better understanding? The commented numbers are to make any replies easier to understand :DCoInitialize
is to initialize the com libraries and for setting up the right apartment for the current thread where you are using the com objects. You need to do this only once for an application. For example in the caase of an MFC application u can callCoInitialize(NULL);
in the initinstace andCoUninitialize();
in the ExitInstance. No need to call this in every class and method. If u r in a multithreded app then u need to callCoInitialize
andCoUninitialize
in each thread. hope this help...mil10 -
CoInitialize
is to initialize the com libraries and for setting up the right apartment for the current thread where you are using the com objects. You need to do this only once for an application. For example in the caase of an MFC application u can callCoInitialize(NULL);
in the initinstace andCoUninitialize();
in the ExitInstance. No need to call this in every class and method. If u r in a multithreded app then u need to callCoInitialize
andCoUninitialize
in each thread. hope this help...mil10Thanks for the reply. That is exactly how I thought it should be done, but in my above code, if I remove it from the destructor, the program crashes on exit. I'm having a nightmare trying to debug this since the xls pointer is an AxtiveX VB coded dll. But I am pretty sure the problem is not even in the dll (it only clears some memory). I will keep trying nonetheless.
-
Thanks for the reply. That is exactly how I thought it should be done, but in my above code, if I remove it from the destructor, the program crashes on exit. I'm having a nightmare trying to debug this since the xls pointer is an AxtiveX VB coded dll. But I am pretty sure the problem is not even in the dll (it only clears some memory). I will keep trying nonetheless.
-
Thanks for the reply. That is exactly how I thought it should be done, but in my above code, if I remove it from the destructor, the program crashes on exit. I'm having a nightmare trying to debug this since the xls pointer is an AxtiveX VB coded dll. But I am pretty sure the problem is not even in the dll (it only clears some memory). I will keep trying nonetheless.
MFC is very particular about outstanding COM references when it shuts down. If you're using a STA, you may want to call AfxOleInit() in your CxxxApp::InitInstance() and let MFC take care of uninitialisation rather than explicitly calling CoInitialize(NULL) and CoUninitialize() in your code. Obviously, if things are a little more complicated or you want to support multiple STAs or a MTA, AfxOleInit() will not suffice. Hope that helps.
-
MFC is very particular about outstanding COM references when it shuts down. If you're using a STA, you may want to call AfxOleInit() in your CxxxApp::InitInstance() and let MFC take care of uninitialisation rather than explicitly calling CoInitialize(NULL) and CoUninitialize() in your code. Obviously, if things are a little more complicated or you want to support multiple STAs or a MTA, AfxOleInit() will not suffice. Hope that helps.
No matter what I do, whenever and however I remove the CoInitialize(NULL); /** 3 **/ from the destructor the program crashes on exit. I cannot for the life of me find out why. I am not using MFC, this is just a regular Win32 application. Also I should point out that this class is initalised in the global scope and no call is made to the destructor.
-
No matter what I do, whenever and however I remove the CoInitialize(NULL); /** 3 **/ from the destructor the program crashes on exit. I cannot for the life of me find out why. I am not using MFC, this is just a regular Win32 application. Also I should point out that this class is initalised in the global scope and no call is made to the destructor.
Thanks for all your effort guys, after reading my own reply to your posts I realised my error. I should have created a global pointer to the object, initialise it on WM_INITIALISE and destroy it on WM_CLOSE. It all works perfectly and as it should do now.