-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortcutManager.cpp
More file actions
61 lines (53 loc) · 2.15 KB
/
ShortcutManager.cpp
File metadata and controls
61 lines (53 loc) · 2.15 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
#include <windows.h>
#include <shlobj.h>
#include <shlguid.h>
#include <string>
#include <filesystem>
namespace ShortcutLogic {
// 内部使用的辅助函数:获取快捷方式应有的完整路径
static std::wstring GetTargetLnkPath() {
WCHAR szPath[MAX_PATH];
// CSIDL_PROGRAMS 指向当前用户的“开始菜单/程序”
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PROGRAMS, NULL, 0, szPath))) {
return std::wstring(szPath) + L"\\FreeMove_Square.lnk";
}
return L"";
}
// --- 逻辑 A:供初始化时判断是否显示的函数 ---
bool IsAlreadyCreated() {
std::wstring path = GetTargetLnkPath();
if (path.empty()) return false;
return std::filesystem::exists(path);
}
// --- 逻辑 B:供按钮 case 调用的“一键创建”函数 ---
bool CreateStartMenuShortcut() {
std::wstring shortcutPath = GetTargetLnkPath();
if (shortcutPath.empty()) return false;
// 获取当前 exe 的完整路径
WCHAR szExePath[MAX_PATH];
GetModuleFileNameW(NULL, szExePath, MAX_PATH);
// 初始化 COM(确保在同一个线程内)
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
bool success = false;
IShellLinkW* psl;
hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (LPVOID*)&psl);
if (SUCCEEDED(hr)) {
psl->SetPath(szExePath);
psl->SetDescription(L"FreeMove Square Tool");
// 设置起始位置为 exe 所在目录
std::filesystem::path p(szExePath);
psl->SetWorkingDirectory(p.parent_path().c_str());
IPersistFile* ppf;
hr = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
if (SUCCEEDED(hr)) {
hr = ppf->Save(shortcutPath.c_str(), TRUE);
if (SUCCEEDED(hr)) success = true;
ppf->Release();
}
psl->Release();
}
// 如果之前已经初始化过 COM,这里调用 Uninitialize 不会破坏全局
CoUninitialize();
return success;
}
}