views:

554

answers:

5

I know this is kind of vague, I'm just shooting for general possibilities here to get me on the right track.

I include two libs and their .h's in my MFC Dialog program and compile it, no problem. When I call a function from one of the libs though, it shoots up a dialog box that says "Com Error" "CoInitialize Failed". That's not when I actually CALL the function, that's as soon as the program starts running. My assumption is that when it sees the function, it actually calls in the lib and when it does, possibly a CoInit is being called before the one in my MFC program, thus creating a conflict?

Stepping through the code, it seems to throw this at CDialog::DoModal

I can always add in more details, I was just hoping to get steered in the right direction. Thanks a lot in advance for any help!

EDIT::::: The thing is, I don't know where the DLL is calling CoInitialize. I really can't post code, because there's simply too much, even for a simple program. I'll try dependency walker and check out my InitInstance... Any other suggestions? Thanks so much guys

+1  A: 

A good direction will be to add more details, specially if you have them, like which is the HRESULT returned by CoInitialize?

My guess is that such lib have a static initialization, if none of the functions from the dll are called they are discarded by the linker, but if at least one function is called then the static initializers are linked.

Ismael
+3  A: 

Try adding in your own call to CoInitializeEx, and make sure you're using STA (SingleThreadedApartment) threading in your main thread.

Chances are something is setting up your main thread to be MTA, but your library expects and requires STA, so it's CoInitialize call is failing.

Reed Copsey
+1  A: 

You should call CoInitialize in your App InitInstance(). Then it should execute before the call to DoModal().

It might be that this error message isn't indicating that the lib tried to call CoInitialize itself but rather tried some other COM call and from the error it received there it deduced that CoInitialize hadn't been called by you.

Paul Mitchell
A: 

A common error is if a referenced DLL is missing so when the CoInitialize is called it tries to load the DLL and fails with good old E_FAIL. Try using dependency walker and check for any DLL's you may have missed.

Anders K.
+1  A: 

You can find out who is calling CoInitialize by putting a break point on the same.

This is the way you do it using Debugging Tools for Windows.

You first enable start with debugger option using gflags.exe for your exe.

For that

  1. run gflags.exe In the image file options give the name of your exe say xyz.exe. Press tab to enable image level options. In the debugger option type Full Path to windbg -g

This will result in your exe being started with debugger attached everytime you start xyz.exe.

Now to set a breakpoint on CoInitialize call, break the execution in Windbg. In the command pane type

bp Ole32!CoInitialize

stop debugging, save the workspace when it prompts and restart xyz.exe

This time the application will break into the debugger when anyone calls CoInitialize.

Hope it helps you

Canopus