1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
/*
* The Mana Client
* Copyright (C) 2010-2024 The Mana Developers
*
* This file is part of The Mana Client.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef _WIN32
#include "specialfolder.h"
#include <windows.h>
#include <stdlib.h>
#ifdef SPECIALFOLDERLOCATION_TEST
// compile with -DSPECIALFOLDERLOCATION_TEST to get a standalone
// binary for testing
#include <iostream>
#endif
/*
* Retrieve the pathname of special folders on Windows, or an empty string
* on error.
*
* See https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid
* for a list of folder IDs.
*/
std::string getSpecialFolderLocation(const KNOWNFOLDERID &folderId)
{
std::string ret;
PWSTR widePath = 0;
HRESULT hr = SHGetKnownFolderPath(folderId, 0, NULL, &widePath);
if (hr == S_OK)
{
// determine needed bytes
size_t len;
if (wcstombs_s(&len, nullptr, 0, widePath, 0) == 0)
{
ret.resize(len - 1); // subtract null character
if (wcstombs_s(nullptr, ret.data(), len, widePath, len - 1) != 0)
ret.clear();
}
}
CoTaskMemFree(widePath);
return ret;
}
#ifdef SPECIALFOLDERLOCATION_TEST
int main()
{
std::cout << "RoamingAppData " << getSpecialFolderLocation(FOLDERID_RoamingAppData)
<< std::endl;
std::cout << "Desktop " << getSpecialFolderLocation(FOLDERID_Desktop)
<< std::endl;
std::cout << "LocalAppData " << getSpecialFolderLocation(FOLDERID_LocalAppData)
<< std::endl;
std::cout << "Pictures " << getSpecialFolderLocation(FOLDERID_Pictures)
<< std::endl;
std::cout << "Documents " << getSpecialFolderLocation(FOLDERID_Documents)
<< std::endl;
}
#endif
#endif
|