#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2021 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License // If the library is compiled as a DLL, this ensures symbols are exported #define NUCLEX_STORAGE_SOURCE 1 #include "StandardDirectoryResolver.h" #if defined(NUCLEX_STORAGE_WIN32) #include "../../Helpers/Utf8/checked.h" #include "../../Helpers/WindowsApi.h" #include "Nuclex/Support/Text/StringConverter.h" #include // for ::CoInitialize(), ::StringFromGUID2() #include // for _com_ptr_t and _COM_SMARTPTR_TYPEDEF #include // for std::vector /// Smart pointer for the known folder manager interface _COM_SMARTPTR_TYPEDEF(IKnownFolderManager, IID_IKnownFolderManager); /// Smart pointer for the interface to a known folder _COM_SMARTPTR_TYPEDEF(IKnownFolder, IID_IKnownFolder); using namespace Nuclex::Storage::Helpers; namespace { // ------------------------------------------------------------------------------------------- // /// Initializes COM for the duration of the scope class ComScope { /// Initializes COM public: ComScope() : wasAlreadyInitialized(false) { // COM may have already been intiialized by the application using this library. // Redundant calls to CoInitialize() are okay (and need a matching call to // CoUninitialize(), too), unless another threading model was used, in which case // we can RPC_E_CHANGED_MODE (but that's fine, the threading model is irrelevant // for what we do) HRESULT resultHandle = ::CoInitialize(nullptr); if(FAILED(resultHandle)) { if(resultHandle == RPC_E_CHANGED_MODE) { this->wasAlreadyInitialized = true; } else { std::string errorMessage(u8"Could not initialize COM"); Nuclex::Storage::Helpers::WindowsApi::ThrowExceptionForHResult( errorMessage, resultHandle ); } } } /// Shuts down COM public: ~ComScope() { if(!this->wasAlreadyInitialized) { ::CoUninitialize(); } } /// Set if CoInitialize() reported that COM was already initialized private: bool wasAlreadyInitialized; }; // ------------------------------------------------------------------------------------------- // /// Releases COM task memory when the scope ends class ComTaskMemoryScope { /// Initializes a new COM task memory scope /// Memory the scope will free public: ComTaskMemoryScope(void *comTaskMemory) : comTaskMemory(comTaskMemory) {} /// Shuts down COM public: ~ComTaskMemoryScope() { ::CoTaskMemFree(this->comTaskMemory); } /// COM task memory the scope will free private: void *comTaskMemory; }; // ------------------------------------------------------------------------------------------- // /// Converts a known folder id GUID into a string /// Known folder id GUID that will be convertedA string representation of the specified GUID /// /// This is only used by error handling code and thus will return something /// rather than throw even if Microsoft's string conversion function fails. /// std::string stringFromKnownFolderId(const KNOWNFOLDERID &knownFolderId) { std::wstring buffer; buffer.resize(39); int result = ::StringFromGUID2( knownFolderId, buffer.data(), static_cast(buffer.size()) ); if(result >= 1) { buffer.resize(static_cast(result - 1)); return Nuclex::Support::Text::StringConverter::Utf8FromWide(buffer); } else { DWORD errorCode = ::GetLastError(); std::string errorMessage = u8"'); return errorMessage; } } // ------------------------------------------------------------------------------------------- // /// Fetches a path to a known folder from the known folder manager /// Known folder manager the path will be fetched from /// ID of the known folder that will be looked up /// The path to the specified known folder std::wstring getFolderFromKnownFolderManager( const IKnownFolderManagerPtr &knownFolderManager, const KNOWNFOLDERID &knownFolderId ) { // Ask it for the known folder with the specified KNOWNFOLDERID value IKnownFolderPtr knownFolder; HRESULT resultHandle = knownFolderManager->GetFolder(knownFolderId, &knownFolder); if(FAILED(resultHandle)) { std::string errorMessage(u8"Could not query known folder "); errorMessage.append(stringFromKnownFolderId(knownFolderId)); Nuclex::Storage::Helpers::WindowsApi::ThrowExceptionForHResult(errorMessage, resultHandle); } // Now that we've got the known folder object, ask it in turn to give us its path PWSTR knownFolderPath = nullptr; resultHandle = knownFolder->GetPath(0, &knownFolderPath); if(FAILED(resultHandle)) { std::string errorMessage(u8"Could not query path of known folder "); errorMessage.append(stringFromKnownFolderId(knownFolderId)); Nuclex::Storage::Helpers::WindowsApi::ThrowExceptionForHResult(errorMessage, resultHandle); } // Success, but now we've been provided with a pointer to a string that has been // allocated via CoTaskMemAlloc() and we're responsible for freeing it, so copy it // into an std::string and then drop all that COM stuff. { ComTaskMemoryScope comTaskMemoryScope(knownFolderPath); return std::wstring(knownFolderPath); } } // ------------------------------------------------------------------------------------------- // } // anonymous namespace namespace Nuclex { namespace Storage { namespace FileSystem { namespace Windows { // ------------------------------------------------------------------------------------------- // std::string StandardDirectoryResolver::GetFolderPath(int csidl) { std::vector directory(MAX_PATH); // MSDN states: "not supported" //BOOL result = ::SHGetSpecialFolderPathW(nullptr, &directory[0], csidl, create); // Ask for the folder HRESULT resultHandle = ::SHGetFolderPathW( nullptr, csidl, nullptr, SHGFP_TYPE_CURRENT, &directory[0] ); if(FAILED(resultHandle)) { std::string errorMessage(u8"Could not obtain special folder path"); Helpers::WindowsApi::ThrowExceptionForHResult(errorMessage, resultHandle); } return Nuclex::Support::Text::StringConverter::Utf8FromWide(std::wstring(&directory[0])); } // ------------------------------------------------------------------------------------------- // std::string StandardDirectoryResolver::GetKnownFolder(const KNOWNFOLDERID &knownFolderId) { { ComScope comScope; // Create a new instance of the "known folder manager" COM object which manages // (or at least looks up) the correct paths for special folders in Windows. IKnownFolderManagerPtr knownFolderManager; HRESULT resultHandle = knownFolderManager.CreateInstance( CLSID_KnownFolderManager, nullptr, CLSCTX_INPROC_SERVER ); if(FAILED(resultHandle)) { std::string errorMessage(u8"Could not create instance of known folder manager"); Helpers::WindowsApi::ThrowExceptionForHResult(errorMessage, resultHandle); } return Nuclex::Support::Text::StringConverter::Utf8FromWide( getFolderFromKnownFolderManager(knownFolderManager, knownFolderId) ); } // COM initialization scope } // ------------------------------------------------------------------------------------------- // std::vector StandardDirectoryResolver::GetMultipleKnownFolders( const std::vector &knownFolderIds ) { std::vector results; std::size_t requestedFolderCount = knownFolderIds.size(); results.reserve(requestedFolderCount); // This API requires COM to be initialized for the process { ComScope comScope; // Create a new instance of the "known folder manager" COM object which manages // (or at least looks up) the correct paths for special folders in Windows. IKnownFolderManagerPtr knownFolderManager; HRESULT resultHandle = knownFolderManager.CreateInstance( CLSID_KnownFolderManager, nullptr, CLSCTX_INPROC_SERVER ); if(FAILED(resultHandle)) { std::string errorMessage(u8"Could not create instance of known folder manager"); Helpers::WindowsApi::ThrowExceptionForHResult(errorMessage, resultHandle); } // Now request each folder in succession for(std::size_t index = 0; index < requestedFolderCount; ++index) { results.push_back( Nuclex::Support::Text::StringConverter::Utf8FromWide( getFolderFromKnownFolderManager(knownFolderManager, knownFolderIds[index]) ) ); } } // COM initialization scope return results; } // ------------------------------------------------------------------------------------------- // }}}} // namespace Nuclex::Storage::FileSystem::Windows #endif // defined(NUCLEX_STORAGE_WIN32)