icestac
asked on
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?
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?
You should call CoInitialize(NULL) to init COM
Better take a look here http://msdn.microsoft.com/library/en-us/com/htm/cmf_a2c_36qt.asp
ASKER
I am calling CoInitialize(NULL) when the DLL gets instantiated (and CoUninitialize when it gets unloaded)
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.
"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.
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."
ASKER
I am taking it out of DllMain and putting it in the worker process... I will report back in a few.
ASKER
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.
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.
ASKER
Also, the call to AfxOleInit does not fail. I removed it and AfxEnableControlContainer and got the same results as before.
ASKER
I put AfxEnableControlContainer back in, but left the AfxOleInit out and it still errored...
ASKER
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
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
ASKER
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!
Thanks!
One thing to watch out for: You need to put this:
AFX_MANAGE_STATE(AfxGetSta ticModuleS tate());
as the very first line of each entry point into a DLL that uses MFC -- including constructors for objects, etc.
AFX_MANAGE_STATE(AfxGetSta
as the very first line of each entry point into a DLL that uses MFC -- including constructors for objects, etc.
ASKER
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!
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!
ASKER
Thanks for all of your help.
nonubik please go here to get your points.
https://www.experts-exchange.com/questions/20945800/Points-for-nonubik-re-Question-Q-20943860.html
KurtVon please go here to get your points.
https://www.experts-exchange.com/questions/20945802/Points-for-KurtVon-re-Question-Q-20943860.html
nonubik please go here to get your points.
https://www.experts-exchange.com/questions/20945800/Points-for-nonubik-re-Question-Q-20943860.html
KurtVon please go here to get your points.
https://www.experts-exchange.com/questions/20945802/Points-for-KurtVon-re-Question-Q-20943860.html
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.