1 """
2 Windows API functions implemented as ctypes functions and classes as found
3 in jaraco.windows (2.10).
4
5 If you encounter issues with this module, please consider reporting the issues
6 in jaraco.windows and asking the author to port the fixes back here.
7 """
8
9 import ctypes
10 import ctypes.wintypes
11 import __builtin__
12
13 try:
14 USHORT = ctypes.wintypes.USHORT
15 except AttributeError:
16 USHORT = ctypes.c_ushort
59
62 "more info about errors at http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx"
63
69
70 @property
73
74 @property
77
80
82 return '{self.__class__.__name__}({self.winerror})'.format(**vars())
83
87
88
89 CreateFileMapping = ctypes.windll.kernel32.CreateFileMappingW
90 CreateFileMapping.argtypes = [
91 ctypes.wintypes.HANDLE,
92 ctypes.c_void_p,
93 ctypes.wintypes.DWORD,
94 ctypes.wintypes.DWORD,
95 ctypes.wintypes.DWORD,
96 ctypes.wintypes.LPWSTR,
97 ]
98 CreateFileMapping.restype = ctypes.wintypes.HANDLE
99
100 MapViewOfFile = ctypes.windll.kernel32.MapViewOfFile
101 MapViewOfFile.restype = ctypes.wintypes.HANDLE
104 """
105 A memory map object which can have security attributes overrideden.
106 """
107 - def __init__(self, name, length, security_attributes=None):
108 self.name = name
109 self.length = length
110 self.security_attributes = security_attributes
111 self.pos = 0
112
114 p_SA = (
115 ctypes.byref(self.security_attributes)
116 if self.security_attributes else None
117 )
118 INVALID_HANDLE_VALUE = -1
119 PAGE_READWRITE = 0x4
120 FILE_MAP_WRITE = 0x2
121 filemap = ctypes.windll.kernel32.CreateFileMappingW(
122 INVALID_HANDLE_VALUE, p_SA, PAGE_READWRITE, 0, self.length,
123 unicode(self.name))
124 handle_nonzero_success(filemap)
125 if filemap == INVALID_HANDLE_VALUE:
126 raise Exception("Failed to create file mapping")
127 self.filemap = filemap
128 self.view = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0)
129 return self
130
131 - def seek(self, pos):
133
135 n = len(msg)
136 if self.pos + n >= self.length:
137 raise ValueError("Refusing to write %d bytes" % n)
138 ctypes.windll.kernel32.RtlMoveMemory(self.view + self.pos, msg, n)
139 self.pos += n
140
142 """
143 Read n bytes from mapped view.
144 """
145 out = ctypes.create_string_buffer(n)
146 ctypes.windll.kernel32.RtlMoveMemory(out, self.view + self.pos, n)
147 self.pos += n
148 return out.raw
149
150 - def __exit__(self, exc_type, exc_val, tb):
151 ctypes.windll.kernel32.UnmapViewOfFile(self.view)
152 ctypes.windll.kernel32.CloseHandle(self.filemap)
153
159
161 num = 1
162 _fields_ = [
163 ('SID', ctypes.c_void_p),
164 ('ATTRIBUTES', ctypes.wintypes.DWORD),
165 ]
166
169 """
170 typedef struct _SECURITY_DESCRIPTOR
171 {
172 UCHAR Revision;
173 UCHAR Sbz1;
174 SECURITY_DESCRIPTOR_CONTROL Control;
175 PSID Owner;
176 PSID Group;
177 PACL Sacl;
178 PACL Dacl;
179 } SECURITY_DESCRIPTOR;
180 """
181 SECURITY_DESCRIPTOR_CONTROL = USHORT
182 REVISION = 1
183
184 _fields_ = [
185 ('Revision', ctypes.c_ubyte),
186 ('Sbz1', ctypes.c_ubyte),
187 ('Control', SECURITY_DESCRIPTOR_CONTROL),
188 ('Owner', ctypes.c_void_p),
189 ('Group', ctypes.c_void_p),
190 ('Sacl', ctypes.c_void_p),
191 ('Dacl', ctypes.c_void_p),
192 ]
193
195 """
196 typedef struct _SECURITY_ATTRIBUTES {
197 DWORD nLength;
198 LPVOID lpSecurityDescriptor;
199 BOOL bInheritHandle;
200 } SECURITY_ATTRIBUTES;
201 """
202 _fields_ = [
203 ('nLength', ctypes.wintypes.DWORD),
204 ('lpSecurityDescriptor', ctypes.c_void_p),
205 ('bInheritHandle', ctypes.wintypes.BOOL),
206 ]
207
211
213 return self._descriptor
217 descriptor = property(_get_descriptor, _set_descriptor)
218
232
235
237 result = ctypes.wintypes.HANDLE()
238 proc_handle = ctypes.wintypes.HANDLE(proc_handle)
239 handle_nonzero_success(ctypes.windll.advapi32.OpenProcessToken(
240 proc_handle, access, ctypes.byref(result)))
241 return result
242
252
254 """
255 Return a SECURITY_ATTRIBUTES structure with the SID set to the
256 specified user (uses current user if none is specified).
257 """
258 if user is None:
259 user = get_current_user()
260
261 assert isinstance(user, TOKEN_USER), "user must be TOKEN_USER instance"
262
263 SD = SECURITY_DESCRIPTOR()
264 SA = SECURITY_ATTRIBUTES()
265
266
267 SA.descriptor = SD
268 SA.bInheritHandle = 1
269
270 ctypes.windll.advapi32.InitializeSecurityDescriptor(ctypes.byref(SD),
271 SECURITY_DESCRIPTOR.REVISION)
272 ctypes.windll.advapi32.SetSecurityDescriptorOwner(ctypes.byref(SD),
273 user.SID, 0)
274 return SA
275