forked from python/pymanager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.cpp
More file actions
236 lines (208 loc) · 6.15 KB
/
misc.cpp
File metadata and controls
236 lines (208 loc) · 6.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <Python.h>
#include <windows.h>
#include <appmodel.h>
#include "helpers.h"
extern "C" {
PyObject *coinitialize(PyObject *, PyObject *args, PyObject *kwargs) {
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(hr)) {
PyErr_SetFromWindowsErr(hr);
return NULL;
}
Py_RETURN_NONE;
}
static void _invalid_parameter(
const wchar_t * expression,
const wchar_t * function,
const wchar_t * file,
unsigned int line,
uintptr_t pReserved
) { }
PyObject *fd_supports_vt100(PyObject *, PyObject *args, PyObject *kwargs) {
static const char * keywords[] = {"fd", NULL};
int fd;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:fd_supports_vt100", keywords, &fd)) {
return NULL;
}
PyObject *r = NULL;
HANDLE h;
DWORD mode;
const DWORD expect_flags = ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
auto handler = _set_thread_local_invalid_parameter_handler(_invalid_parameter);
h = (HANDLE)_get_osfhandle(fd);
_set_thread_local_invalid_parameter_handler(handler);
if (GetConsoleMode(h, &mode)) {
if ((mode & expect_flags) == expect_flags) {
r = Py_GetConstant(Py_CONSTANT_TRUE);
} else {
r = Py_GetConstant(Py_CONSTANT_FALSE);
}
} else {
PyErr_SetFromWindowsErr(0);
}
return r;
}
PyObject *date_as_str(PyObject *, PyObject *, PyObject *) {
wchar_t buffer[256];
DWORD cch = GetDateFormatEx(
LOCALE_NAME_INVARIANT,
0,
NULL,
L"yyyyMMdd",
buffer,
sizeof(buffer) / sizeof(buffer[0]),
NULL
);
if (!cch) {
PyErr_SetFromWindowsErr(0);
return NULL;
}
return PyUnicode_FromWideChar(buffer, cch - 1);
}
PyObject *datetime_as_str(PyObject *, PyObject *, PyObject *) {
wchar_t buffer[256];
DWORD cch = GetDateFormatEx(
LOCALE_NAME_INVARIANT,
0,
NULL,
L"yyyyMMdd",
buffer,
sizeof(buffer) / sizeof(buffer[0]),
NULL
);
if (!cch) {
PyErr_SetFromWindowsErr(0);
return NULL;
}
cch -= 1;
cch += GetTimeFormatEx(
LOCALE_NAME_INVARIANT,
0,
NULL,
L"HHmmss",
&buffer[cch],
sizeof(buffer) / sizeof(buffer[0]) - cch
);
return PyUnicode_FromWideChar(buffer, cch - 1);
}
PyObject *reg_rename_key(PyObject *, PyObject *args, PyObject *kwargs) {
static const char * keywords[] = {"handle", "name1", "name2", NULL};
PyObject *handle_obj;
wchar_t *name1 = NULL;
wchar_t *name2 = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO&O&:reg_rename_key", keywords,
&handle_obj, as_utf16, &name1, as_utf16, &name2)) {
return NULL;
}
PyObject *r = NULL;
HKEY h;
if (PyLong_AsNativeBytes(handle_obj, &h, sizeof(h), -1) >= 0) {
int err = (int)RegRenameKey(h, name1, name2);
if (!err) {
r = Py_GetConstant(Py_CONSTANT_NONE);
} else {
PyErr_SetFromWindowsErr(err);
}
}
PyMem_Free(name1);
PyMem_Free(name2);
return r;
}
PyObject *get_current_package(PyObject *, PyObject *, PyObject *) {
wchar_t package_name[256];
UINT32 cch = sizeof(package_name) / sizeof(package_name[0]);
int err = GetCurrentPackageFamilyName(&cch, package_name);
switch (err) {
case ERROR_SUCCESS:
return PyUnicode_FromWideChar(package_name, cch ? cch - 1 : 0);
case APPMODEL_ERROR_NO_PACKAGE:
return Py_GetConstant(Py_CONSTANT_NONE);
default:
PyErr_SetFromWindowsErr(err);
return NULL;
}
}
PyObject *read_alias_package(PyObject *, PyObject *args, PyObject *kwargs) {
static const char * keywords[] = {"path", NULL};
wchar_t *path = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:read_alias_package", keywords,
as_utf16, &path)) {
return NULL;
}
HANDLE h = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);
PyMem_Free(path);
if (h == INVALID_HANDLE_VALUE) {
PyErr_SetFromWindowsErr(0);
return NULL;
}
struct {
DWORD tag;
DWORD _reserved1;
DWORD _reserved2;
wchar_t package_name[256];
wchar_t nul;
} buffer;
DWORD nread;
if (!DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0,
&buffer, sizeof(buffer), &nread, NULL)
// we expect our buffer to be too small, but we only want the package
&& GetLastError() != ERROR_MORE_DATA) {
PyErr_SetFromWindowsErr(0);
CloseHandle(h);
return NULL;
}
CloseHandle(h);
if (buffer.tag != IO_REPARSE_TAG_APPEXECLINK) {
return Py_GetConstant(Py_CONSTANT_NONE);
}
buffer.nul = 0;
return PyUnicode_FromWideChar(buffer.package_name, -1);
}
typedef LRESULT (*PSendMessageTimeoutW)(
HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam,
UINT fuFlags,
UINT uTimeout,
PDWORD_PTR lpdwResult
);
PyObject *broadcast_settings_change(PyObject *, PyObject *, PyObject *) {
// Avoid depending on user32 because it's so slow
HMODULE user32 = LoadLibraryExW(L"user32.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
if (!user32) {
PyErr_SetFromWindowsErr(0);
return NULL;
}
PSendMessageTimeoutW sm = (PSendMessageTimeoutW)GetProcAddress(user32, "SendMessageTimeoutW");
if (!sm) {
PyErr_SetFromWindowsErr(0);
FreeLibrary(user32);
return NULL;
}
// SendMessageTimeout needs special error handling
SetLastError(0);
LPARAM lParam = (LPARAM)L"Environment";
if (!(*sm)(
HWND_BROADCAST,
WM_SETTINGCHANGE,
NULL,
lParam,
SMTO_ABORTIFHUNG,
50,
NULL
)) {
int err = GetLastError();
if (!err) {
PyErr_SetString(PyExc_OSError, "Unspecified error");
} else {
PyErr_SetFromWindowsErr(err);
}
FreeLibrary(user32);
return NULL;
}
FreeLibrary(user32);
return Py_GetConstant(Py_CONSTANT_NONE);
}
}