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
122 lines (110 loc) · 3.08 KB
/
misc.cpp
File metadata and controls
122 lines (110 loc) · 3.08 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
#include <Python.h>
#include <windows.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;
}
}