forked from python/pymanager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortcut.cpp
More file actions
146 lines (132 loc) · 4.14 KB
/
shortcut.cpp
File metadata and controls
146 lines (132 loc) · 4.14 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
#include <Python.h>
#include <windows.h>
#include <shlobj.h>
#include <shlguid.h>
#include "helpers.h"
extern "C" {
PyObject *
shortcut_create(PyObject *, PyObject *args, PyObject *kwargs)
{
static const char *keywords[] = {
"path", "target", "arguments", "working_directory",
"icon", "icon_index",
NULL
};
wchar_t *path = NULL;
wchar_t *target = NULL;
wchar_t *arguments = NULL;
wchar_t *workingDirectory = NULL;
wchar_t *iconPath = NULL;
int iconIndex = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"O&O&|O&O&O&i:shortcut_create", keywords,
as_utf16, &path, as_utf16, &target, as_utf16, &arguments, as_utf16, &workingDirectory,
as_utf16, &iconPath, &iconIndex)) {
return NULL;
}
PyObject *r = NULL;
IShellLinkW *lnk = NULL;
IPersistFile *persist = NULL;
HRESULT hr;
hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLinkW, (void **)&lnk);
if (FAILED(hr)) {
err_SetFromWindowsErrWithMessage(hr, "Creating system shortcut");
goto done;
}
if (FAILED(hr = lnk->SetPath(target))) {
err_SetFromWindowsErrWithMessage(hr, "Setting shortcut target");
goto done;
}
if (arguments && *arguments && FAILED(hr = lnk->SetArguments(arguments))) {
err_SetFromWindowsErrWithMessage(hr, "Setting shortcut arguments");
goto done;
}
if (workingDirectory && *workingDirectory && FAILED(hr = lnk->SetWorkingDirectory(workingDirectory))) {
err_SetFromWindowsErrWithMessage(hr, "Setting shortcut working directory");
goto done;
}
if (iconPath && *iconPath && FAILED(hr = lnk->SetIconLocation(iconPath, iconIndex))) {
err_SetFromWindowsErrWithMessage(hr, "Setting shortcut icon");
goto done;
}
if (FAILED(hr = lnk->QueryInterface(&persist))) {
err_SetFromWindowsErrWithMessage(hr, "Getting persist interface");
goto done;
}
// gh-15: Apparently ERROR_USER_MAPPED_FILE can occur here, which suggests
// contention on the file. We should be able to sleep and retry to handle it
// in most cases.
for (int retries = 5; retries; --retries) {
if (SUCCEEDED(hr = persist->Save(path, 0))) {
break;
}
if (retries == 1 || hr != HRESULT_FROM_WIN32(ERROR_USER_MAPPED_FILE)) {
err_SetFromWindowsErrWithMessage(hr, "Writing shortcut");
goto done;
}
Sleep(10);
}
r = Py_NewRef(Py_None);
done:
if (persist) {
persist->Release();
}
if (lnk) {
lnk->Release();
}
if (path) PyMem_Free(path);
if (target) PyMem_Free(target);
if (arguments) PyMem_Free(arguments);
if (workingDirectory) PyMem_Free(workingDirectory);
if (iconPath) PyMem_Free(iconPath);
return r;
}
PyObject *
shortcut_get_start_programs(PyObject *, PyObject *, PyObject *)
{
wchar_t *path;
HRESULT hr = SHGetKnownFolderPath(
FOLDERID_Programs,
KF_FLAG_NO_PACKAGE_REDIRECTION | KF_FLAG_CREATE,
NULL,
&path
);
if (FAILED(hr)) {
err_SetFromWindowsErrWithMessage(hr, "Obtaining Start Menu location");
return NULL;
}
PyObject *r = PyUnicode_FromWideChar(path, -1);
CoTaskMemFree(path);
return r;
}
PyObject *
hide_file(PyObject *, PyObject *args, PyObject *kwargs)
{
static const char *keywords[] = {"path", "hidden", NULL};
wchar_t *path;
int hidden = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|b:hide_file", keywords, as_utf16, &path, &hidden)) {
return NULL;
}
PyObject *r = NULL;
DWORD attr = GetFileAttributesW(path);
if (attr == INVALID_FILE_ATTRIBUTES) {
err_SetFromWindowsErrWithMessage(GetLastError(), "Reading file attributes");
goto done;
}
if (hidden) {
attr |= FILE_ATTRIBUTE_HIDDEN;
} else {
attr &= ~FILE_ATTRIBUTE_HIDDEN;
}
if (!SetFileAttributesW(path, attr)) {
err_SetFromWindowsErrWithMessage(GetLastError(), "Setting file attributes");
goto done;
}
r = Py_NewRef(Py_None);
done:
PyMem_Free(path);
return r;
}
}