简体   繁体   中英

CoInitializeSecurity cannot succeeded when using TChromium (CEF3)

I have an application that is running a chromium client browser, and at some point I need to execute WMI code to access some device information, but it fails everytime. It is working only when the application is not using TChromium object. Possible TChromium(CEF3) Initializes the COM library, and only one instance is allowed on the current thread. I read that application should use CoInitializeEx with COINIT_APARTMENTTHREADED instead of CoInitialize.

It is possible to access COM library and CoInitializeSecurity when using TChromium (CEF3) in one application? If yes, how to do it?

Below is what I want to achive:

CoUninitialize();
   CoInitialize(NULL);
   if(CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0) == S_OK)
   { 
    // cannot get here, CoInitializeSecurity fails
     ... need to execute WMI code using IWbemLocator, IWbemServices ...
   }

COM can be initialized only 1 time per thread. CoInitialize/Ex() can be called multiple times per thread (with corresponding CoUninitialize() calls for every successful CoInitialize/Ex() call), but COM will only be initialized on the first call, and subsequent calls will return either S_FALSE or RPC_E_CHANGED_MODE if COM has already been initialized on the calling thread.

To solve your issue, try moving your WMI code to a separate worker thread, then you have total control over how you want to initialize COM for that thread, completely separate from how COM is initialized on the main UI thread. Have your main thread create the WMI thread when needed and wait for it to terminate, then the WMI thread can query the device info and pass it back to the main thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM