linusthe3rd
linusthe3rd

Reputation: 3566

CoInitializeEx fails when calling OpenCV method cvLoadImage()

I am trying to integrate some OpenCV functionality into my application. Currently I have code set up with DirectShow to get a video feed from my camera, which is then showed in an MFC window. This code cannot be changed or removed.

The code runs completely fine, but regardless of the location i place the following line of code:

IplImage *img = cvLoadImage("C:/well.jpg");

The webcam fails to initialize correctly and breaks the program.

more directly, i get a FAILED HRESULT at:

CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)

More specifically, at some point in my code i Call CDialog::doModal(), which then hits CoInitializeEx(), and thus makes the program fail.

would anyone know what is going on here?

Upvotes: 4

Views: 2199

Answers (3)

Alessandro Jacopson
Alessandro Jacopson

Reputation: 18665

I had a similar problem. In my MFC app the call to AfxOleInit failed with RPC_E_CHANGED_MODE.

I cannot ignore the failure (I need the COM inside the app) and I cannot move the OpenCV call to a different thread (as Michael rightly suggests).

I found the thread "wxwidgets and opencv 1.1 ole initialization error" that solves my problem: I do not need the video input support from OpenCV and so I can remove the #define HAVE_VIDEOINPUT 1 as suggested in http://tech.dir.groups.yahoo.com/group/OpenCV/message/60060

go to _highgui.h, comment line 96 ("#define HAVE_VIDEOINPUT 1") and recompile

It works with OpenCV_1.1pre1a.

Upvotes: 0

AndersK
AndersK

Reputation: 36082

In addition to what Michael said check also for external dependent DLL's, if one is missing CoInitialize will also fail.

Upvotes: 1

Michael
Michael

Reputation: 55415

CoInitialize will fail if the thread was previously initialized as a different apartment, i.e., if there was a previous CoInitializeEx(NULL, COINIT_MULTITHREADED)

I would guess that OpenCV calls CoInitializeEx(NULL, COINIT_MULTITHREADED), causing your subsequent calls to CoInitializeEx to fail. You can confirm this by checking the return of CoInitializeEx - it'll be RPC_E_CHANGED_MODE in this case.

There is no straightforward fix, the most straightforward will be to move the OpenCV calls into a separate thread.

Upvotes: 4

Related Questions