ThePhD / infoware

C++ Library for pulling system and hardware information, without hitting the command line.
Creative Commons Zero v1.0 Universal
410 stars 84 forks source link

Fix wrongly handling RPC_E_CHANGED_MODE in Windows #54

Closed demian51 closed 3 years ago

demian51 commented 3 years ago

When CoInitializeEx returns RPC_E_CHANGED_MODE, it DOES NOT increase the reference counter of the COM library. Therefore version_name() decreases the reference counter without increment, and the COM library can be unloaded unintentionally.

My code which using infoware initially calls CoInitializeEx with COINIT_APARTMENTTHREADED, then calls version_name() via iware::system::OS_info(). In version_name() it unloads the COM library, and my following codes fails.

This commit fixes the issue by retrying CoInitializeEx with COINIT_APARTMENTTHREADED. By doing this, the reference counter of the COM library will be increased successfully.

Refers to: https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-coinitializeex

nabijaczleweli commented 3 years ago

Applied with minor editorial changes as 9aa485dd56df146e05f7153965b783fa097c2d9e; thanks!