tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

GetKnownFolderPath.cpp (1269B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "GetKnownFolderPath.h"
      6 
      7 namespace mozilla {
      8 
      9 UniquePtr<wchar_t, LoadedCoTaskMemFreeDeleter> GetKnownFolderPath(
     10    REFKNOWNFOLDERID folderId) {
     11  static decltype(SHGetKnownFolderPath)* shGetKnownFolderPath = nullptr;
     12  if (!shGetKnownFolderPath) {
     13    // We could go out of our way to `FreeLibrary` on this, decrementing its
     14    // ref count and potentially unloading it. However doing so would be either
     15    // effectively a no-op, or counterproductive. Just let it get cleaned up
     16    // when the process is terminated, because we're going to load it anyway
     17    // elsewhere.
     18    HMODULE shell32Dll = ::LoadLibraryW(L"shell32");
     19    if (!shell32Dll) {
     20      return nullptr;
     21    }
     22    shGetKnownFolderPath = reinterpret_cast<decltype(shGetKnownFolderPath)>(
     23        ::GetProcAddress(shell32Dll, "SHGetKnownFolderPath"));
     24    if (!shGetKnownFolderPath) {
     25      return nullptr;
     26    }
     27  }
     28  PWSTR path = nullptr;
     29  shGetKnownFolderPath(folderId, 0, nullptr, &path);
     30  return UniquePtr<wchar_t, LoadedCoTaskMemFreeDeleter>(path);
     31 }
     32 
     33 }  // namespace mozilla