QCoreApplication QApplication with WMI

The error you are receiving is due to the fact that COM is already initialized. QApplication calls OleInitialize in its constructor, but QCoreApplication does not, so that's why you get the error 0x80010106 (RPC_E_CHANGED_MODE) when you use QApplication. For further reading, see the documentation for CoInitializeEx. The following article should provide more insight.


At first I have used this code

hres =  CoInitializeEx(0, COINIT_MULTITHREADED);

if (hres == RPC_E_CHANGED_MODE) {
    OleUninitialize();
    hres =  CoInitializeEx(0, COINIT_MULTITHREADED);
}

When I get error "Failed to initialize COM library(0x80010106)

I just call OleUninitialize to deal with OleInitialize() in QApplication, but that could break some qt functionality.

As I found the best is to use flag COINIT_APARTMENTTHREADED for CoInitializeEx

MSDN

Generally, a thread that creates a window should use the COINIT_APARTMENTTHREADED flag, and other threads should use COINIT_MULTITHREADED. However, some COM components require a particular threading model. The MSDN documentation should tell you when that is the case.

For me it`s a black magic, but it helped me

Tags:

C++

Qt

Wmi