Repository navigation
Expand file tree
/
Copy pathSource1.cpp
More file actions
139 lines (107 loc) · 3.2 KB
/
Copy pathSource1.cpp
File metadata and controls
139 lines (107 loc) · 3.2 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
#include <Windows.h>
#include <ShObjIdl.h>
#include <ShlObj.h>
#include <Shlwapi.h>
#include <iostream>
#include <string>
#pragma comment(lib, "Ole32.lib")
#pragma comment(lib, "Shell32.lib")
#pragma comment(lib, "Shlwapi.lib")
HRESULT SetPdfIcon(IShellLinkW* link)
{
wchar_t iconSpec[1024];
DWORD length = ARRAYSIZE(iconSpec);
HRESULT hr = AssocQueryStringW(
ASSOCF_NONE,
ASSOCSTR_DEFAULTICON,
L".pdf",
nullptr,
iconSpec,
&length);
if (FAILED(hr))
return hr;
// Separates "path,index" into the path and index.
int iconIndex = PathParseIconLocationW(iconSpec);
// Resolve paths containing variables such as %SystemRoot%.
wchar_t expandedPath[1024];
DWORD result = ExpandEnvironmentStringsW(
iconSpec,
expandedPath,
ARRAYSIZE(expandedPath));
if (result == 0)
return HRESULT_FROM_WIN32(GetLastError());
if (result > ARRAYSIZE(expandedPath))
return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
return link->SetIconLocation(expandedPath, iconIndex);
}
HRESULT CreateShortcut(
PCWSTR shortcutPath,
PCWSTR targetPath,
PCWSTR arguments,
PCWSTR workingDirectory)
{
IShellLinkW* link = nullptr;
HRESULT hr = CoCreateInstance(
CLSID_ShellLink,
nullptr,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&link));
if (FAILED(hr))
return hr;
hr = link->SetPath(targetPath);
if (SUCCEEDED(hr))
hr = SetPdfIcon(link);
if (SUCCEEDED(hr) && arguments)
hr = link->SetArguments(arguments);
if (SUCCEEDED(hr) && workingDirectory)
hr = link->SetWorkingDirectory(workingDirectory);
if (SUCCEEDED(hr))
hr = link->SetDescription(L"Open a text file with Notepad");
if (SUCCEEDED(hr))
hr = link->SetShowCmd(SW_SHOWNORMAL);
if (SUCCEEDED(hr)) {
IPersistFile* file = nullptr;
hr = link->QueryInterface(IID_PPV_ARGS(&file));
if (SUCCEEDED(hr)) {
hr = file->Save(shortcutPath, TRUE);
file->Release();
}
}
link->Release();
return hr;
}
int wmain()
{
HRESULT initResult =
CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
if (FAILED(initResult)) {
std::wcerr << L"COM initialization failed: 0x"
<< std::hex << initResult << L'\n';
return 1;
}
PWSTR desktopFolder = nullptr;
HRESULT hr = SHGetKnownFolderPath(
FOLDERID_Desktop,
KF_FLAG_DEFAULT,
nullptr,
&desktopFolder);
if (SUCCEEDED(hr)) {
std::wstring shortcutPath(desktopFolder);
CoTaskMemFree(desktopFolder);
if (!shortcutPath.empty() && shortcutPath.back() != L'\\')
shortcutPath += L'\\';
shortcutPath += L"Revenue Report 2026.lnk";
hr = CreateShortcut(
shortcutPath.c_str(),
L"%SystemRoot%\\System32\\notepad.exe",
L"\"C:\\Temp\\notes.txt\"",
L"C:\\Temp");
}
if (SUCCEEDED(hr))
std::wcout << L"Shortcut created successfully.\n";
else
std::wcerr << L"Creation failed: 0x"
<< std::hex << hr << L'\n';
CoUninitialize();
return FAILED(hr) ? 1 : 0;
}