Repository navigation
Expand file tree
/
Copy pathSendToDesktopShortcut11.cpp
More file actions
213 lines (176 loc) · 6.26 KB
/
Copy pathSendToDesktopShortcut11.cpp
File metadata and controls
213 lines (176 loc) · 6.26 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#define INITGUID
#include "common.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.3.0\n";
if (GetUserDefaultUILanguage() == 2052)
{
aboutMessage += L"为 Windows 11 设计的右键菜单扩展,可将选中的文件或文件夹发送到桌面快捷方式\n\n";
aboutMessage += L"仓库地址:https://github.com/SJEllipses/SendToDesktopShortcut11\n\n";
aboutMessage += L"当前右键菜单拓展状态:";
aboutMessage += IsExtensionEnabled() ? L"已启用\n\n" : L"已禁用\n\n";
aboutMessage += L"设置右键菜单拓展状态:\n";
aboutMessage += L"是 (Y) = 启用\n否 (N) = 禁用\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";
aboutMessage += L"Current context menu extension status: ";
aboutMessage += IsExtensionEnabled() ? L"Enabled\n\n" : L"Disabled\n\n";
aboutMessage += L"Set context menu extension status: \n";
aboutMessage += L"Yes (Y) = Enable\nNo (N) = Disable\nCancel = Do not change";
}
int choice = MessageBoxW(nullptr, aboutMessage.c_str(), L"SendToDesktopShortcut11", MB_YESNOCANCEL);
if (choice == IDYES)
{
if(SetExtensionEnabled(true) == ERROR_SUCCESS)
{
if (GetUserDefaultUILanguage() == 2052)
MessageBoxW(nullptr, L"右键菜单拓展已启用", L"SendToDesktopShortcut11", MB_OK);
else
MessageBoxW(nullptr, L"Context menu extension enabled", L"SendToDesktopShortcut11", MB_OK);
}
else
{
if (GetUserDefaultUILanguage() == 2052)
MessageBoxW(nullptr, L"右键菜单拓展启用失败", L"SendToDesktopShortcut11", MB_OK);
else
MessageBoxW(nullptr, L"Failed to enable context menu extension", L"SendToDesktopShortcut11", MB_OK);
}
}
else if (choice == IDNO)
{
if(SetExtensionEnabled(false) == ERROR_SUCCESS)
{
if (GetUserDefaultUILanguage() == 2052)
MessageBoxW(nullptr, L"右键菜单拓展已禁用", L"SendToDesktopShortcut11", MB_OK);
else
MessageBoxW(nullptr, L"Context menu extension disabled", L"SendToDesktopShortcut11", MB_OK);
}
else
{
if (GetUserDefaultUILanguage() == 2052)
MessageBoxW(nullptr, L"右键菜单拓展禁用失败", L"SendToDesktopShortcut11", MB_OK);
else
MessageBoxW(nullptr, L"Failed to disable context menu extension", 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;
}
}