CoInitialize has not been called
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: CoInitialize has not been called

  1. #1
    Join Date
    Apr 2009
    Posts
    21

    CoInitialize has not been called

    Hi all,
    I got problem with my project

    First-chance exception at 0x75669617 in BasicReport.exe: 0x800401F0: CoInitialize has not been called.
    Warning: constructing COleException, scode = CO_E_NOTINITIALIZED ($800401F0).
    First-chance exception at 0x75669617 in BasicReport.exe: Microsoft C++ exception: COleException at memory location 0x034bed94..
    Unhandled exception at 0x75669617 in BasicReport.exe: Microsoft C++ exception: COleException at memory location 0x034bed94..

    Can any1 explain to me what the problem is ? and how can i fix it

    Thanks
    Attached Files Attached Files

  2. #2
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,440

    Re: CoInitialize has not been called

    You need to call CoInitialize when your program starts and CoUnitialize when it terminates.

    The best place to do this is in InitInstance and ExitInstance.

    Just make CoInistialize the first call to InitInstance.

    Code:
     
    BOOL CBasicReportApp::InitInstance()
    {
      CoInitialize( NULL );
     
      if(!AfxOleInit())
     
      // etc.
    }
    For the CoUnitialize call, you need to override ExitInstance.
    To do this, Open the Class view, select the CBasicReportApp class.
    Right click and choose "Properties".
    At the top of the properties window you'll see a series of icons.
    One is a little lightning bolt, one looks like a small window, and one looks like a little green box. If you hover over these, you get a tooltip for each one. In order this will be "Events", "Messages", and "Overrides". Click on the Green Icon ("Overrides"),
    Scroll down until you find ExitInstance and create an override for this method.

    Well, that's how you normally do it. Looks like using CWinAppEx breaks that.

    So enter it manually (and put a declaration in the header file too).

    Code:
     
    INT CBasicReportApp::ExitInstance()
    {
      CoUninitialize( );
      return CWinAppEx::ExitInstance( );
    }

  3. #3
    Join Date
    Apr 2009
    Posts
    21

    Re: CoInitialize has not been called

    i have followed ur help and add both CoInitialize, CoUnInitialize function into CBasicReportApp::InitInstance() and CBasicReportApp::ExitInstance() but till got the same problem

    First-chance exception at 0x75669617 in BasicReport.exe: 0x800401F0: CoInitialize has not been called.
    Warning: constructing COleException, scode = CO_E_NOTINITIALIZED ($800401F0).
    First-chance exception at 0x75669617 in BasicReport.exe: Microsoft C++ exception: COleException at memory location 0x0259f13c..
    Unhandled exception at 0x75669617 in BasicReport.exe: Microsoft C++ exception: COleException at memory location 0x0259f13c..

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: CoInitialize has not been called

    Are you using a worker thread?

  5. #5
    Join Date
    Apr 2009
    Posts
    21

    Re: CoInitialize has not been called

    Yeah, I used Worker thread in my project.
    I have att my project with this post
    You can see my code there and please tell me what is the problems with my code.

  6. #6
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,440

    Re: CoInitialize has not been called

    Quote Originally Posted by pcuong1983 View Post
    Yeah, I used Worker thread in my project.
    I have att my project with this post
    You can see my code there and please tell me what is the problems with my code.
    You'll need to put the CoInitialize/CoUnitialize pair into each thread where COM objects are called.

  7. #7
    Join Date
    Apr 2009
    Posts
    21

    Re: CoInitialize has not been called

    Can you give me more information about CoInitialize/CoUnInitialize in Thread ?

    i tried to add CoInitialize at the begining of my thread function. It look like

    Code:
    UINT ThreadFunc(LPVOID lpParam)
    {
               CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
               ......................
               CoUninitialize();
    }
    and now im facing new problem:

    First-chance exception at 0x75669617 in BasicReport.exe: 0x8001010E: The application called an interface that was marshalled for a different thread.
    Warning: constructing COleException, scode = RPC_E_WRONG_THREAD ($8001010E).
    First-chance exception at 0x75669617 in BasicReport.exe: Microsoft C++ exception: COleException at memory location 0x038ef28c..
    Unhandled exception at 0x75669617 in BasicReport.exe: Microsoft C++ exception: COleException at memory location 0x038ef28c.

    Im new in COM and Thread programming.

  8. #8
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,440

    Re: CoInitialize has not been called

    You can't directly share a STA COM object pointer across threads.

    You need to marshal it. Check out the CoMarshalInterThreadInterfaceInStream approach listed in this article.

    http://www.codeproject.com/KB/COM/CCOMThread2.aspx

  9. #9
    Join Date
    Apr 2009
    Posts
    21

    Re: CoInitialize has not been called

    Thanks you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center