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__
54
57 "more info about errors at http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx"
58
64
65 @property
68
69 @property
72
75
77 return '{self.__class__.__name__}({self.winerror})'.format(**vars())
78
82
83
84
85
86
87 CreateFileMapping = ctypes.windll.kernel32.CreateFileMappingW
88 CreateFileMapping.argtypes = [
89 ctypes.wintypes.HANDLE,
90 ctypes.c_void_p,
91 ctypes.wintypes.DWORD,
92 ctypes.wintypes.DWORD,
93 ctypes.wintypes.DWORD,
94 ctypes.wintypes.LPWSTR,
95 ]
96 CreateFileMapping.restype = ctypes.wintypes.HANDLE
97
98 MapViewOfFile = ctypes.windll.kernel32.MapViewOfFile
99 MapViewOfFile.restype = ctypes.wintypes.HANDLE
102 """
103 A memory map object which can have security attributes overrideden.
104 """
105 - def __init__(self, name, length, security_attributes=None):
106 self.name = name
107 self.length = length
108 self.security_attributes = security_attributes
109 self.pos = 0
110
112 p_SA = (
113 ctypes.byref(self.security_attributes)
114 if self.security_attributes else None
115 )
116 INVALID_HANDLE_VALUE = -1
117 PAGE_READWRITE = 0x4
118 FILE_MAP_WRITE = 0x2
119 filemap = ctypes.windll.kernel32.CreateFileMappingW(
120 INVALID_HANDLE_VALUE, p_SA, PAGE_READWRITE, 0, self.length,
121 unicode(self.name))
122 handle_nonzero_success(filemap)
123 if filemap == INVALID_HANDLE_VALUE:
124 raise Exception("Failed to create file mapping")
125 self.filemap = filemap
126 self.view = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0)
127 return self
128
129 - def seek(self, pos):
131
133 ctypes.windll.msvcrt.memcpy(self.view + self.pos, msg, len(msg))
134 self.pos += len(msg)
135
137 """
138 Read n bytes from mapped view.
139 """
140 out = ctypes.create_string_buffer(n)
141 ctypes.windll.msvcrt.memcpy(out, self.view + self.pos, n)
142 self.pos += n
143 return out.raw
144
145 - def __exit__(self, exc_type, exc_val, tb):
146 ctypes.windll.kernel32.UnmapViewOfFile(self.view)
147 ctypes.windll.kernel32.CloseHandle(self.filemap)
148
154
156 num = 1
157 _fields_ = [
158 ('SID', ctypes.c_void_p),
159 ('ATTRIBUTES', ctypes.wintypes.DWORD),
160 ]
161
164 """
165 typedef struct _SECURITY_DESCRIPTOR
166 {
167 UCHAR Revision;
168 UCHAR Sbz1;
169 SECURITY_DESCRIPTOR_CONTROL Control;
170 PSID Owner;
171 PSID Group;
172 PACL Sacl;
173 PACL Dacl;
174 } SECURITY_DESCRIPTOR;
175 """
176 SECURITY_DESCRIPTOR_CONTROL = ctypes.wintypes.USHORT
177 REVISION = 1
178
179 _fields_ = [
180 ('Revision', ctypes.c_ubyte),
181 ('Sbz1', ctypes.c_ubyte),
182 ('Control', SECURITY_DESCRIPTOR_CONTROL),
183 ('Owner', ctypes.c_void_p),
184 ('Group', ctypes.c_void_p),
185 ('Sacl', ctypes.c_void_p),
186 ('Dacl', ctypes.c_void_p),
187 ]
188
190 """
191 typedef struct _SECURITY_ATTRIBUTES {
192 DWORD nLength;
193 LPVOID lpSecurityDescriptor;
194 BOOL bInheritHandle;
195 } SECURITY_ATTRIBUTES;
196 """
197 _fields_ = [
198 ('nLength', ctypes.wintypes.DWORD),
199 ('lpSecurityDescriptor', ctypes.c_void_p),
200 ('bInheritHandle', ctypes.wintypes.BOOL),
201 ]
202
206
208 return self._descriptor
212 descriptor = property(_get_descriptor, _set_descriptor)
213
227
230
232 result = ctypes.wintypes.HANDLE()
233 proc_handle = ctypes.wintypes.HANDLE(proc_handle)
234 handle_nonzero_success(ctypes.windll.advapi32.OpenProcessToken(
235 proc_handle, access, ctypes.byref(result)))
236 return result
237
247
249 """
250 Return a SECURITY_ATTRIBUTES structure with the SID set to the
251 specified user (uses current user if none is specified).
252 """
253 if user is None:
254 user = get_current_user()
255
256 assert isinstance(user, TOKEN_USER), "user must be TOKEN_USER instance"
257
258 SD = SECURITY_DESCRIPTOR()
259 SA = SECURITY_ATTRIBUTES()
260
261
262 SA.descriptor = SD
263 SA.bInheritHandle = 1
264
265 ctypes.windll.advapi32.InitializeSecurityDescriptor(ctypes.byref(SD),
266 SECURITY_DESCRIPTOR.REVISION)
267 ctypes.windll.advapi32.SetSecurityDescriptorOwner(ctypes.byref(SD),
268 user.SID, 0)
269 return SA
270