Blender V5.3
winstuff_registration.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#ifdef WIN32
10# ifdef WIN32_LEAN_AND_MEAN
11# undef WIN32_LEAN_AND_MEAN
12# endif
13# include <Windows.h>
14
15# include <KnownFolders.h>
16# include <filesystem>
17# include <propkey.h>
18# include <propvarutil.h>
19# include <shlobj_core.h>
20# include <wrl.h>
21
22# include "BLI_path_utils.hh"
23# include "BLI_winstuff.hh"
24# include "BLI_winstuff_com.hh"
25
26# include "utf_winfunc.hh"
27# include "utfconv.hh"
28
29namespace blender {
30
48bool BLI_windows_update_pinned_launcher(const char *launcher_path)
49{
50 WCHAR launcher_path_w[FILE_MAX];
51
52 if (conv_utf_8_to_16(launcher_path, launcher_path_w, ARRAY_SIZE(launcher_path_w)) != 0) {
53 return false;
54 }
55
56 CoInitializeWrapper initialize(COINIT_APARTMENTTHREADED);
57 if (FAILED(initialize)) {
58 return false;
59 }
60
61 LPWSTR quick_launch_folder_path;
62 if (SHGetKnownFolderPath(
63 FOLDERID_ImplicitAppShortcuts, KF_FLAG_DEFAULT, nullptr, &quick_launch_folder_path) !=
64 S_OK)
65 {
66 return false;
67 }
68
69 std::wstring search_path = quick_launch_folder_path;
70 CoTaskMemFree(quick_launch_folder_path);
71
72 for (auto const &dir_entry : std::filesystem::recursive_directory_iterator(search_path)) {
73
74 Microsoft::WRL::ComPtr<IShellLinkW> shell_link;
75 if (CoCreateInstance(__uuidof(ShellLink), nullptr, CLSCTX_ALL, IID_PPV_ARGS(&shell_link)) !=
76 S_OK)
77 {
78 return false;
79 }
80
81 Microsoft::WRL::ComPtr<IPersistFile> persist_file;
82 if (shell_link.As(&persist_file) != S_OK) {
83 return false;
84 }
85
86 if (persist_file->Load(dir_entry.path().c_str(), STGM_READWRITE) != S_OK) {
87 continue;
88 }
89
90 Microsoft::WRL::ComPtr<IPropertyStore> property_store;
91 if (shell_link.As(&property_store) != S_OK) {
92 continue;
93 }
94
95 UTF16_ENCODE(BLENDER_WIN_APPID);
96 PROPVARIANT app_model;
97 PropVariantInit(&app_model);
98 if (property_store->GetValue(PKEY_AppUserModel_ID, &app_model) == S_OK) {
99 if (app_model.vt == VT_LPWSTR && std::wstring(BLENDER_WIN_APPID_16) == app_model.pwszVal) {
100 shell_link->SetPath(launcher_path_w);
101 persist_file->Save(nullptr, TRUE);
102 }
103 }
104 PropVariantClear(&app_model);
105 UTF16_UN_ENCODE(BLENDER_WIN_APPID);
106 }
107 return true;
108}
109
110} // namespace blender
111
112#endif
#define FILE_MAX
Compatibility-like things for windows.
COM helper functions for windows.
void initialize()
BMesh Iterators.
bool BLI_windows_update_pinned_launcher(const char *launcher_path)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
int conv_utf_8_to_16(const char *in8, wchar_t *out16, size_t size16)
Definition utfconv.cc:182
#define UTF16_ENCODE(in8str)
Definition utfconv.hh:80
#define UTF16_UN_ENCODE(in8str)
Definition utfconv.hh:84