Confusing CoInitialize issue

I have a audio playback class what works fine in a JUCE project that uses a GUI. I have just tried using the same class in a console application and I hit the following assertion:


jassert (hr != CO_E_NOTINITIALIZED); // You haven't called CoInitialize for the current thread!


in juce_win32_ComSmartPtr.h, when I try to initialize my AudioDeviceManager.


I did some googling and tried adding 'CoInitialize(0)' at the top of my class but CoInitialize is undefined.


Can someone tell me what I am missing?


Well, if you read the comment, it says you haven't called it for the current thread

Maybe wherever you added your call was being run from a different thread to the one that needs it?

I have just hit this same issue.

My console application pulls in juce_audio_basics and juce_audio_devices. When making a call to AudioDeviceManager::initialize I get this runtime trap.

I am guessing in a GUI app there is COM initialization that normally runs that is not present in this case.

My work-around is:

#include <combaseapi.h>

And then, on the thread calling - to call CoInitialize(nullptr) just before
the call that caused the initial error.

Suggestion: It might be useful to have some ‘initialize’ hook available for non-GUI applications to provide the required initialization without having to go down into windows- or linux-land prior to using cross-platform calls. Just a thought.