CO_E_NOTINITIALIZED When Running Word Automation

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?
LVL 3
icestacAsked:
Who is Participating?
 
LunchyConnect With a Mentor Commented:
Closed, 500 points refunded.
Lunchy
Friendly Neighbourhood Community Support Moderator
0
 
nonubikCommented:
You should call CoInitialize(NULL) to init COM
0
 
nonubikCommented:
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
icestacAuthor Commented:
I am calling CoInitialize(NULL) when the DLL gets instantiated (and CoUninitialize when it gets unloaded)
0
 
KurtVonCommented:
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
 
nonubikCommented:
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
 
icestacAuthor Commented:
I am taking it out of DllMain and putting it in the worker process... I will report back in a few.
0
 
icestacAuthor Commented:
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
 
icestacAuthor Commented:
Also, the call to AfxOleInit does not fail.  I removed it and AfxEnableControlContainer and got the same results as before.
0
 
icestacAuthor Commented:
I put AfxEnableControlContainer back in, but left the AfxOleInit out and it still errored...
0
 
icestacAuthor Commented:
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
 
icestacAuthor Commented:
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
 
DanRollinsCommented:
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
 
icestacAuthor Commented:
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
 
icestacAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.