Get information about all currently open explorer windows. Throws std::system_error exception to report errors.
| 185 | // Get information about all currently open explorer windows. |
| 186 | // Throws std::system_error exception to report errors. |
| 187 | static std::vector< ExplorerFolderInfo > GetCurrentExplorerFolders() |
| 188 | { |
| 189 | CComPtr< IShellWindows > pshWindows; |
| 190 | ThrowIfFailed( |
| 191 | pshWindows.CoCreateInstance(CLSID_ShellWindows), |
| 192 | "Could not create instance of IShellWindows"); |
| 193 | |
| 194 | long count = 0; |
| 195 | ThrowIfFailed( |
| 196 | pshWindows->get_Count(&count), |
| 197 | "Could not get number of shell windows"); |
| 198 | |
| 199 | std::vector< ExplorerFolderInfo > result; |
| 200 | result.reserve(count); |
| 201 | |
| 202 | for (long i = 0; i < count; ++i) |
| 203 | { |
| 204 | ExplorerFolderInfo info; |
| 205 | |
| 206 | CComVariant vi{ i }; |
| 207 | CComPtr< IDispatch > pDisp; |
| 208 | ThrowIfFailed( |
| 209 | pshWindows->Item(vi, &pDisp), |
| 210 | "Could not get item from IShellWindows"); |
| 211 | |
| 212 | if (!pDisp) |
| 213 | // Skip - this shell window was registered with a NULL IDispatch |
| 214 | continue; |
| 215 | |
| 216 | CComQIPtr< IWebBrowserApp > pApp{ pDisp }; |
| 217 | if (!pApp) |
| 218 | // This window doesn't implement IWebBrowserApp |
| 219 | continue; |
| 220 | |
| 221 | // Get the window handle. |
| 222 | pApp->get_HWND(reinterpret_cast<SHANDLE_PTR*>(&info.hwnd)); |
| 223 | |
| 224 | CComQIPtr< IServiceProvider > psp{ pApp }; |
| 225 | if (!psp) |
| 226 | // This window doesn't implement IServiceProvider |
| 227 | continue; |
| 228 | |
| 229 | CComPtr< IShellBrowser > pBrowser; |
| 230 | if (FAILED(psp->QueryService(SID_STopLevelBrowser, &pBrowser))) |
| 231 | // This window doesn't provide IShellBrowser |
| 232 | continue; |
| 233 | |
| 234 | CComPtr< IShellView > pShellView; |
| 235 | if (FAILED(pBrowser->QueryActiveShellView(&pShellView))) |
| 236 | // For some reason there is no active shell view |
| 237 | continue; |
| 238 | |
| 239 | CComQIPtr< IFolderView > pFolderView{ pShellView }; |
| 240 | if (!pFolderView) |
| 241 | // The shell view doesn't implement IFolderView |
| 242 | continue; |
| 243 | |
| 244 | // Get the interface from which we can finally query the PIDL of |