Solved

CO_E_NOTINITIALIZED When Running Word Automation

Posted on 2004-04-05
16
618 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
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
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.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference between String.subString() and String.subSequence() 6 235
withoutTen challenge 14 132
FizzBuzz challenge 9 82
wordcount challenge 11 133
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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