Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CO_E_NOTINITIALIZED When Running Word Automation

Posted on 2004-04-05
16
Medium Priority
?
626 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
16 Comments
 
LVL 16

Expert Comment

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

Expert Comment

by:nonubik
ID: 10757604
0
 
LVL 3

Author Comment

by:icestac
ID: 10757737
I am calling CoInitialize(NULL) when the DLL gets instantiated (and CoUninitialize when it gets unloaded)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Expert Comment

by:KurtVon
ID: 10757817
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
ID: 10757831
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
ID: 10757838
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
ID: 10758036
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
 
LVL 3

Author Comment

by:icestac
ID: 10758055
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
ID: 10758067
I put AfxEnableControlContainer back in, but left the AfxOleInit out and it still errored...
0
 
LVL 3

Author Comment

by:icestac
ID: 10758108
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
ID: 10758223
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
ID: 10762879
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
ID: 10765569
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
ID: 10770461
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
ID: 10797989
Closed, 500 points refunded.
Lunchy
Friendly Neighbourhood Community Support Moderator
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

721 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