-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendToDesktopShortcut11.cpp
More file actions
166 lines (135 loc) · 4.09 KB
/
SendToDesktopShortcut11.cpp
File metadata and controls
166 lines (135 loc) · 4.09 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#define INITGUID
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shlobj.h> // SHGetKnownFolderPath
#include <shobjidl.h> // IShellLink
#include <objbase.h>
#include <knownfolders.h>
#include <string>
std::wstring GetDesktopPath()
{
PWSTR path = nullptr;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Desktop, 0, NULL, &path);
if (FAILED(hr))
return L"";
std::wstring result(path);
CoTaskMemFree(path);
return result;
}
std::wstring GetFileName(const std::wstring &fullPath)
{
if (fullPath.empty())
return fullPath;
std::wstring path = fullPath;
// Remove trailing slashes
while (path.length() > 1 &&
(path.back() == L'\\' || path.back() == L'/'))
{
// Keep paths like "C:\"
if (path.length() == 3 &&
path[1] == L':' &&
(path[2] == L'\\' || path[2] == L'/'))
{
break;
}
path.pop_back();
}
// "C:" -> "C"
if (path.length() == 2 && path[1] == L':')
{
return path.substr(0, 1);
}
// "C:\" -> "C"
if (path.length() == 3 &&
path[1] == L':' &&
(path[2] == L'\\' || path[2] == L'/'))
{
return path.substr(0, 1);
}
// "\\mynas" -> "mynas"
if (path.size() >= 2 &&
path[0] == L'\\' &&
path[1] == L'\\' &&
path.find(L'\\', 2) == std::wstring::npos)
{
return path.substr(2);
}
size_t pos = path.find_last_of(L"\\/");
if (pos == std::wstring::npos)
return path;
return path.substr(pos + 1);
}
bool CreateShortcut(const std::wstring &targetPath)
{
HRESULT hr = CoInitialize(nullptr);
if (FAILED(hr))
return false;
IShellLinkW *pShellLink = nullptr;
hr = CoCreateInstance(
CLSID_ShellLink,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IShellLinkW,
(void **)&pShellLink);
if (FAILED(hr))
{
CoUninitialize();
return false;
}
pShellLink->SetPath(targetPath.c_str());
IPersistFile *pPersistFile = nullptr;
hr = pShellLink->QueryInterface(IID_IPersistFile, (void **)&pPersistFile);
if (SUCCEEDED(hr))
{
std::wstring desktop = GetDesktopPath();
std::wstring name = GetFileName(targetPath);
size_t dot = name.find_last_of(L'.');
if (dot != std::wstring::npos && name.substr(dot) == L".exe") // .exe only
{
name = name.substr(0, dot);
}
std::wstring shortcutPath = desktop + L"\\" + name + L".lnk";
hr = pPersistFile->Save(shortcutPath.c_str(), TRUE);
pPersistFile->Release();
}
pShellLink->Release();
CoUninitialize();
return SUCCEEDED(hr);
}
int wmain(int argc, wchar_t *argv[])
{
if (argc < 2)
{
std::wstring aboutMessage = L"";
aboutMessage += L"SendToDesktopShortcut11";
aboutMessage += L" 0.2.0\n";
if (GetUserDefaultUILanguage() == 2052)
{
aboutMessage += L"为 Windows 11 设计的右键菜单扩展,可将选中的文件或文件夹发送到桌面快捷方式\n\n";
aboutMessage += L"仓库地址:https://github.com/SJEllipses/SendToDesktopShortcut11\n";
}
else
{
aboutMessage += L"Context menu extension designed for Windows 11, which can send selected files or folders to desktop shortcuts\n\n";
aboutMessage += L"Repo: https://github.com/SJEllipses/SendToDesktopShortcut11\n";
}
MessageBoxW(nullptr, aboutMessage.c_str(), L"SendToDesktopShortcut11", MB_OK);
return 1;
}
else
{
for (int i = 1; i < argc; i++)
{
HRESULT hr = CreateShortcut(argv[i]);
if (!hr)
{
if (GetUserDefaultUILanguage() == 2052)
MessageBoxW(nullptr, L" 创建快捷方式失败", L"SendToDesktopShortcut11", MB_ICONERROR | MB_OK);
else
MessageBoxW(nullptr, L" Failed to create shortcut.", L"SendToDesktopShortcut11", MB_ICONERROR | MB_OK);
return hr;
}
}
return 0;
}
}