Blender V5.0
GHOST_SystemPathsWin32.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "GHOST_Debug.hh"
11
12#ifndef _WIN32_IE
13# define _WIN32_IE 0x0501
14#endif
15#include "utfconv.hh"
16#include <shlobj.h>
17
19
21
22const char *GHOST_SystemPathsWin32::getSystemDir(int, const char *versionstr) const
23{
24 const char *system_dir = nullptr;
25
26 /* 1 UTF16 might translate into 3 UTF8. 2 UTF16 translates into 4 UTF8. */
27 static char knownpath[MAX_PATH * 3 + 128] = {0};
28 PWSTR knownpath_16 = nullptr;
29
30 HRESULT hResult = SHGetKnownFolderPath(
31 FOLDERID_ProgramData, KF_FLAG_DEFAULT, nullptr, &knownpath_16);
32
33 if (hResult == S_OK) {
34 conv_utf_16_to_8(knownpath_16, knownpath, MAX_PATH * 3);
35 strcat(knownpath, "\\Blender Foundation\\Blender\\");
36 strcat(knownpath, versionstr);
37 system_dir = knownpath;
38 }
39
40 CoTaskMemFree(knownpath_16);
41 return system_dir;
42}
43
44const char *GHOST_SystemPathsWin32::getUserDir(int, const char *versionstr) const
45{
46 const char *user_dir = nullptr;
47
48 static char knownpath[MAX_PATH * 3 + 128] = {0};
49 PWSTR knownpath_16 = nullptr;
50
51 HRESULT hResult = SHGetKnownFolderPath(
52 FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, nullptr, &knownpath_16);
53
54 if (hResult == S_OK) {
55 conv_utf_16_to_8(knownpath_16, knownpath, MAX_PATH * 3);
56 strcat(knownpath, "\\Blender Foundation\\Blender\\");
57 strcat(knownpath, versionstr);
58 user_dir = knownpath;
59 }
60
61 CoTaskMemFree(knownpath_16);
62 return user_dir;
63}
64
67{
68 const char *special_dir = nullptr;
69
70 GUID folderid;
71 switch (type) {
73 folderid = FOLDERID_Desktop;
74 break;
76 folderid = FOLDERID_Documents;
77 break;
79 folderid = FOLDERID_Downloads;
80 break;
82 folderid = FOLDERID_Music;
83 break;
85 folderid = FOLDERID_Pictures;
86 break;
88 folderid = FOLDERID_Videos;
89 break;
91 folderid = FOLDERID_LocalAppData;
92 break;
93 default:
95 false,
96 "GHOST_SystemPathsWin32::getUserSpecialDir(): Invalid enum value for type parameter");
97 return std::nullopt;
98 }
99
100 static char knownpath[MAX_PATH * 3] = {0};
101 PWSTR knownpath_16 = nullptr;
102 HRESULT hResult = SHGetKnownFolderPath(folderid, KF_FLAG_DEFAULT, nullptr, &knownpath_16);
103
104 if (hResult == S_OK) {
105 conv_utf_16_to_8(knownpath_16, knownpath, MAX_PATH * 3);
106 special_dir = knownpath;
107 }
108
109 if ((special_dir == nullptr) || (special_dir[0] == '\0')) {
110 return std::nullopt;
111 }
112
113 CoTaskMemFree(knownpath_16);
114 return special_dir;
115}
116
118{
119 static char fullname[MAX_PATH * 3] = {0};
120 wchar_t fullname_16[MAX_PATH * 3];
121
122 if (GetModuleFileNameW(0, fullname_16, MAX_PATH)) {
123 conv_utf_16_to_8(fullname_16, fullname, MAX_PATH * 3);
124 return fullname;
125 }
126
127 return nullptr;
128}
129
131{
132 UTF16_ENCODE(filepath);
133 UTF16_ENCODE(BLENDER_WIN_APPID);
134 SHARDAPPIDINFO info;
135 IShellItem *shell_item;
136
137 HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
138 if (!SUCCEEDED(hr)) {
139 return;
140 }
141
142 hr = SHCreateItemFromParsingName(filepath_16, nullptr, IID_PPV_ARGS(&shell_item));
143 if (SUCCEEDED(hr)) {
144 info.psi = shell_item;
145 info.pszAppID = BLENDER_WIN_APPID_16;
146 SHAddToRecentDocs(SHARD_APPIDINFO, &info);
147 }
148
149 CoUninitialize();
150 UTF16_UN_ENCODE(BLENDER_WIN_APPID);
151 UTF16_UN_ENCODE(filepath);
152}
#define GHOST_ASSERT(x, info)
GHOST_TUserSpecialDirTypes
@ GHOST_kUserSpecialDirDesktop
@ GHOST_kUserSpecialDirMusic
@ GHOST_kUserSpecialDirPictures
@ GHOST_kUserSpecialDirVideos
@ GHOST_kUserSpecialDirDownloads
@ GHOST_kUserSpecialDirCaches
@ GHOST_kUserSpecialDirDocuments
const char * getBinaryDir() const override
const char * getSystemDir(int version, const char *versionstr) const override
std::optional< std::string > getUserSpecialDir(GHOST_TUserSpecialDirTypes type) const override
void addToSystemRecentFiles(const char *filepath) const override
const char * getUserDir(int version, const char *versionstr) const override
int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)
Definition utfconv.cc:116
#define UTF16_ENCODE(in8str)
Definition utfconv.hh:80
#define UTF16_UN_ENCODE(in8str)
Definition utfconv.hh:84