views:

701

answers:

2

Hello,

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?

+2  A: 

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.

Michael
+1  A: 

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

Anders K.