Solved

CO_E_NOTINITIALIZED When Running Word Automation

Posted on 2004-04-05
16
609 Views
Last Modified: 2013-11-20
I am automating Word via the OLE interface.  I have no issues with this when I am using this in a standard windows application (dialog based, SDI, or MDI).  I am now developing a single threaded .DLL which is being called via a JSP page on a webserver (that is run by the command prompt so we do have screen interaction).

When I attempt to determine whether I have control of an instance of Word in this DLL, I attempt to get the HWnd and check it for NULL.  When I do that, I end up getting a CO_E_NOTINITIALIZED ($800401f0) error with "Abort, Retry, Ignore".  If I click ignore, the program keeps going and the program assumes Word is not running.  At this point, my app is capable of starting a new instance of Word, but subsequent calls fail (whether it is to get the HWnd or do a file open, etc).

In DllMain, I am calling:
      if(!AfxOleInit())
      {
         return FALSE;
      }
      AfxEnableControlContainer();
      CoInitialize(NULL);

Any ideas?
0
Comment
Question by:icestac
16 Comments
 
LVL 16

Expert Comment

by:nonubik
Comment Utility
You should call CoInitialize(NULL) to init COM
0
 
LVL 16

Expert Comment

by:nonubik
Comment Utility
0
 
LVL 3

Author Comment

by:icestac
Comment Utility
I am calling CoInitialize(NULL) when the DLL gets instantiated (and CoUninitialize when it gets unloaded)
0
 
LVL 11

Expert Comment

by:KurtVon
Comment Utility
From the link nonubik provided:

"Because there is no way to control the order in which in-process servers are loaded or unloaded, do not call CoInitialize, CoInitializeEx, or CoUninitialize from the DllMain function."

Hope this helps.
0
 
LVL 16

Expert Comment

by:nonubik
Comment Utility
Is your DLL MFC? Because MSDN states "If AfxOleInit is called from an MFC DLL, the call will fail. The failure occurs because the function assumes that, if it is called from a DLL, the OLE system was previously initialized by the calling application."
0
 
LVL 3

Author Comment

by:icestac
Comment Utility
I am taking it out of DllMain and putting it in the worker process... I will report back in a few.
0
 
LVL 3

Author Comment

by:icestac
Comment Utility
I removed it out of DllMain and put into the code.  I did find out that multiple threads will be calling the Dll, but there is only one instance of the Dll (and it is single threaded).

Here is the code I added:
   if (bCoInitialized == false)  // Static variable
   {
      if (CoInitialize(NULL) == S_OK)
      {
         ReportError("CoInitialize returned S_OK.", LOG_DEBUG);
         bCoInitialized = true;
      }
      else
      {
         ReportError("CoInitialize returned S_FALSE because it has already been initialized.", LOG_DEBUG);
      }
   }
   StartWord();

It does prevent the error message during the first call, but when I do a second call, it goes back to giviing me the same error.  When this happened, I removed the bCoInitialized so CoInitialize(NULL) would always be called, and it yeiled the same results.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 3

Author Comment

by:icestac
Comment Utility
Also, the call to AfxOleInit does not fail.  I removed it and AfxEnableControlContainer and got the same results as before.
0
 
LVL 3

Author Comment

by:icestac
Comment Utility
I put AfxEnableControlContainer back in, but left the AfxOleInit out and it still errored...
0
 
LVL 3

Author Comment

by:icestac
Comment Utility
I added some code to check the current thread ID and it turns out that the thread ID that gets returned is that of the calling thread.

   CString buf;
   buf.Format("The current thread ID is %08.8X", GetCurrentThreadId());
   ReportError(buf, LOG_DEBUG);

OUTPUT:
LEVEL 00        The current thread ID is 00000E70
LEVEL 00        CoInitialize returned S_OK.
LEVEL 00        The current thread ID is 00000158
0
 
LVL 3

Author Comment

by:icestac
Comment Utility
I changed CoInitialize(NULL) to CoInitializeEx(NULL, COINIT_MULTITHREADED) and it seems to be working correctly.  I will test some more and report back.

Thanks!
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
One thing to watch out for:  You need to put this:

      AFX_MANAGE_STATE(AfxGetStaticModuleState());

as the very first line of each entry point into a DLL that uses MFC -- including constructors for objects, etc.
0
 
LVL 3

Author Comment

by:icestac
Comment Utility
Apparently if the caller is multithreaded, the COM library needs to be initialized using CoInitializeEx(NULL, COINIT_MULTITHREADED).  I have gotten this to work.  I would like to point out one other thing with regards to JNI and Threading that I found out:

1. You must call JNI's AttachCurrentThread to let Java know about the thread or else you will crash
2. You can not use AttachCurrentThread with AfxBeginThread, instead you must use CreateThread or you will crash

Just some things I found out.

Thanks!
0
 
LVL 3

Author Comment

by:icestac
Comment Utility
Thanks for all of your help.

nonubik please go here to get your points.
http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_20945800.html

KurtVon please go here to get your points.
http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_20945802.html
0
 
LVL 2

Accepted Solution

by:
Lunchy earned 0 total points
Comment Utility
Closed, 500 points refunded.
Lunchy
Friendly Neighbourhood Community Support Moderator
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumNumber challenge 16 98
format the code in java 6 72
sumHeights  challenge 17 59
Path of Workbook 3 44
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now