Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. How to correctly use CoInitialize?

How to correctly use CoInitialize?

Scheduled Pinned Locked Moved COM
tutorialquestion
7 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Waldermort
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • W Waldermort

      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

      M Offline
      M Offline
      Milton Karimbekallil
      wrote on last edited by
      #2

      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 call CoInitialize(NULL); in the initinstace and CoUninitialize(); in the ExitInstance. No need to call this in every class and method. If u r in a multithreded app then u need to call CoInitialize and CoUninitialize in each thread. hope this help...mil10

      W 1 Reply Last reply
      0
      • M Milton Karimbekallil

        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 call CoInitialize(NULL); in the initinstace and CoUninitialize(); in the ExitInstance. No need to call this in every class and method. If u r in a multithreded app then u need to call CoInitialize and CoUninitialize in each thread. hope this help...mil10

        W Offline
        W Offline
        Waldermort
        wrote on last edited by
        #3

        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.

        L R 2 Replies Last reply
        0
        • W Waldermort

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          are you using smart pointers?* * *

          Regards, Sohail Kadiwala (My Blog - http://blogs.wdevs.com/sohail/[^])

          1 Reply Last reply
          0
          • W Waldermort

            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.

            R Offline
            R Offline
            Rory Solley
            wrote on last edited by
            #5

            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.

            W 1 Reply Last reply
            0
            • R Rory Solley

              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.

              W Offline
              W Offline
              Waldermort
              wrote on last edited by
              #6

              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.

              W 1 Reply Last reply
              0
              • W Waldermort

                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.

                W Offline
                W Offline
                Waldermort
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups