mlabbe / nativefiledialog

A tiny, neat C library that portably invokes native file open and save dialogs.
zlib License
1.77k stars 209 forks source link

CoInitializeEx return value RPC_E_CHANGED_MODE not handled correctly #72

Closed hrydgard closed 5 years ago

hrydgard commented 5 years ago

On Windows, in NFD_OpenDialog, COM is initialized like this:

HRESULT coResult = ::CoInitializeEx(NULL,
                                        ::COINIT_APARTMENTTHREADED |
                                        ::COINIT_DISABLE_OLE1DDE );

Unfortunately, if COM has already been initialized in the process through COINIT_MULTITHREADED, the above call will fail (but COM will still work). See https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-coinitializeex.

And if the above call fails, NFD_OpenFileDialog just bails. This breaks our application.

I can think of three solutions:

We use this library through https://github.com/saurvs/nfd-rs , but this is easily reproducible by adding a a CoInitializeEx(NULL, COINIT_MULTITHREADED); call to main() in test_opendialog.c.

gunhaxxor commented 5 years ago

Thanks for finding this! I had the same issue. I changed the relevant lines in nfd_win.cpp to:

if (autoCoInit.Result() != RPC_E_CHANGED_MODE
        && !SUCCEEDED(autoCoInit.Result()))
    {
....

so that it won't bail out if that specific error is returned from the CoInitializeEx

mlabbe commented 5 years ago

Fix in master branch. Version bumped to 1.1.5.