blob: 8c9fe1a64968b8fa6292273390dea40a3b11a5f0 [file] [log] [blame]
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001######################################################################
2# This file should be kept compatible with Python 2.3, see PEP 291. #
3######################################################################
Thomas Hellerbabddfc2006-03-08 19:56:54 +00004"""create and manipulate C data types in Python"""
5
Thomas Hellerbabddfc2006-03-08 19:56:54 +00006import os as _os, sys as _sys
Thomas Hellerbabddfc2006-03-08 19:56:54 +00007
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00008__version__ = "1.1.0"
Thomas Hellerbabddfc2006-03-08 19:56:54 +00009
10from _ctypes import Union, Structure, Array
11from _ctypes import _Pointer
12from _ctypes import CFuncPtr as _CFuncPtr
13from _ctypes import __version__ as _ctypes_version
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014from _ctypes import RTLD_LOCAL, RTLD_GLOBAL
Thomas Hellerbabddfc2006-03-08 19:56:54 +000015from _ctypes import ArgumentError
16
17from struct import calcsize as _calcsize
18
19if __version__ != _ctypes_version:
20 raise Exception, ("Version number mismatch", __version__, _ctypes_version)
21
22if _os.name in ("nt", "ce"):
23 from _ctypes import FormatError
24
Thomas Wouters0e3f5912006-08-11 14:57:12 +000025DEFAULT_MODE = RTLD_LOCAL
26if _os.name == "posix" and _sys.platform == "darwin":
27 import gestalt
28
29 # gestalt.gestalt("sysv") returns the version number of the
30 # currently active system file as BCD.
31 # On OS X 10.4.6 -> 0x1046
32 # On OS X 10.2.8 -> 0x1028
33 # See also http://www.rgaros.nl/gestalt/
34 #
35 # On OS X 10.3, we use RTLD_GLOBAL as default mode
36 # because RTLD_LOCAL does not work at least on some
37 # libraries.
38
39 if gestalt.gestalt("sysv") < 0x1040:
40 DEFAULT_MODE = RTLD_GLOBAL
41
Thomas Hellerbabddfc2006-03-08 19:56:54 +000042from _ctypes import FUNCFLAG_CDECL as _FUNCFLAG_CDECL, \
43 FUNCFLAG_PYTHONAPI as _FUNCFLAG_PYTHONAPI
44
Thomas Hellerbabddfc2006-03-08 19:56:54 +000045"""
46WINOLEAPI -> HRESULT
47WINOLEAPI_(type)
48
49STDMETHODCALLTYPE
50
51STDMETHOD(name)
52STDMETHOD_(type, name)
53
54STDAPICALLTYPE
55"""
56
57def create_string_buffer(init, size=None):
58 """create_string_buffer(aString) -> character array
59 create_string_buffer(anInteger) -> character array
60 create_string_buffer(aString, anInteger) -> character array
61 """
Walter Dörwaldaa97f042007-05-03 21:05:51 +000062 if isinstance(init, str):
Thomas Hellerbabddfc2006-03-08 19:56:54 +000063 if size is None:
64 size = len(init)+1
65 buftype = c_char * size
66 buf = buftype()
67 buf.value = init
68 return buf
Walter Dörwaldaa97f042007-05-03 21:05:51 +000069 elif isinstance(init, int):
Thomas Hellerbabddfc2006-03-08 19:56:54 +000070 buftype = c_char * init
71 buf = buftype()
72 return buf
73 raise TypeError, init
74
75def c_buffer(init, size=None):
76## "deprecated, use create_string_buffer instead"
77## import warnings
78## warnings.warn("c_buffer is deprecated, use create_string_buffer instead",
79## DeprecationWarning, stacklevel=2)
80 return create_string_buffer(init, size)
81
82_c_functype_cache = {}
83def CFUNCTYPE(restype, *argtypes):
84 """CFUNCTYPE(restype, *argtypes) -> function prototype.
Tim Peterse8d09e52006-03-09 01:15:05 +000085
Thomas Hellerbabddfc2006-03-08 19:56:54 +000086 restype: the result type
87 argtypes: a sequence specifying the argument types
Tim Peterse8d09e52006-03-09 01:15:05 +000088
Thomas Wouters0e3f5912006-08-11 14:57:12 +000089 The function prototype can be called in different ways to create a
Thomas Hellerbabddfc2006-03-08 19:56:54 +000090 callable object:
Tim Peterse8d09e52006-03-09 01:15:05 +000091
Thomas Wouters477c8d52006-05-27 19:21:47 +000092 prototype(integer address) -> foreign function
93 prototype(callable) -> create and return a C callable function from callable
94 prototype(integer index, method name[, paramflags]) -> foreign function calling a COM method
95 prototype((ordinal number, dll object)[, paramflags]) -> foreign function exported by ordinal
96 prototype((function name, dll object)[, paramflags]) -> foreign function exported by name
Thomas Hellerbabddfc2006-03-08 19:56:54 +000097 """
98 try:
99 return _c_functype_cache[(restype, argtypes)]
100 except KeyError:
101 class CFunctionType(_CFuncPtr):
102 _argtypes_ = argtypes
103 _restype_ = restype
104 _flags_ = _FUNCFLAG_CDECL
105 _c_functype_cache[(restype, argtypes)] = CFunctionType
106 return CFunctionType
107
108if _os.name in ("nt", "ce"):
109 from _ctypes import LoadLibrary as _dlopen
110 from _ctypes import FUNCFLAG_STDCALL as _FUNCFLAG_STDCALL
111 if _os.name == "ce":
112 # 'ce' doesn't have the stdcall calling convention
113 _FUNCFLAG_STDCALL = _FUNCFLAG_CDECL
114
115 _win_functype_cache = {}
116 def WINFUNCTYPE(restype, *argtypes):
117 # docstring set later (very similar to CFUNCTYPE.__doc__)
118 try:
119 return _win_functype_cache[(restype, argtypes)]
120 except KeyError:
121 class WinFunctionType(_CFuncPtr):
122 _argtypes_ = argtypes
123 _restype_ = restype
124 _flags_ = _FUNCFLAG_STDCALL
125 _win_functype_cache[(restype, argtypes)] = WinFunctionType
126 return WinFunctionType
127 if WINFUNCTYPE.__doc__:
128 WINFUNCTYPE.__doc__ = CFUNCTYPE.__doc__.replace("CFUNCTYPE", "WINFUNCTYPE")
129
130elif _os.name == "posix":
131 from _ctypes import dlopen as _dlopen
132
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000133from _ctypes import sizeof, byref, addressof, alignment, resize
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000134from _ctypes import _SimpleCData
135
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000136def _check_size(typ, typecode=None):
137 # Check if sizeof(ctypes_type) against struct.calcsize. This
138 # should protect somewhat against a misconfigured libffi.
139 from struct import calcsize
140 if typecode is None:
141 # Most _type_ codes are the same as used in struct
142 typecode = typ._type_
143 actual, required = sizeof(typ), calcsize(typecode)
144 if actual != required:
145 raise SystemError("sizeof(%s) wrong: %d instead of %d" % \
146 (typ, actual, required))
147
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000148class py_object(_SimpleCData):
149 _type_ = "O"
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000150 def __repr__(self):
151 try:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000152 return super().__repr__()
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000153 except ValueError:
154 return "%s(<NULL>)" % type(self).__name__
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000155_check_size(py_object, "P")
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000156
157class c_short(_SimpleCData):
158 _type_ = "h"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000159_check_size(c_short)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000160
161class c_ushort(_SimpleCData):
162 _type_ = "H"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000163_check_size(c_ushort)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000164
165class c_long(_SimpleCData):
166 _type_ = "l"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000167_check_size(c_long)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000168
169class c_ulong(_SimpleCData):
170 _type_ = "L"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000171_check_size(c_ulong)
Tim Peterse8d09e52006-03-09 01:15:05 +0000172
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000173if _calcsize("i") == _calcsize("l"):
174 # if int and long have the same size, make c_int an alias for c_long
175 c_int = c_long
176 c_uint = c_ulong
177else:
178 class c_int(_SimpleCData):
179 _type_ = "i"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000180 _check_size(c_int)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000181
182 class c_uint(_SimpleCData):
183 _type_ = "I"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000184 _check_size(c_uint)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000185
186class c_float(_SimpleCData):
187 _type_ = "f"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000188_check_size(c_float)
Tim Peterse8d09e52006-03-09 01:15:05 +0000189
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000190class c_double(_SimpleCData):
191 _type_ = "d"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000192_check_size(c_double)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000193
194if _calcsize("l") == _calcsize("q"):
195 # if long and long long have the same size, make c_longlong an alias for c_long
196 c_longlong = c_long
197 c_ulonglong = c_ulong
198else:
199 class c_longlong(_SimpleCData):
200 _type_ = "q"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000201 _check_size(c_longlong)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000202
203 class c_ulonglong(_SimpleCData):
204 _type_ = "Q"
205 ## def from_param(cls, val):
206 ## return ('d', float(val), val)
207 ## from_param = classmethod(from_param)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000208 _check_size(c_ulonglong)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000209
210class c_ubyte(_SimpleCData):
211 _type_ = "B"
212c_ubyte.__ctype_le__ = c_ubyte.__ctype_be__ = c_ubyte
213# backward compatibility:
214##c_uchar = c_ubyte
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000215_check_size(c_ubyte)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000216
217class c_byte(_SimpleCData):
218 _type_ = "b"
219c_byte.__ctype_le__ = c_byte.__ctype_be__ = c_byte
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000220_check_size(c_byte)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000221
222class c_char(_SimpleCData):
223 _type_ = "c"
224c_char.__ctype_le__ = c_char.__ctype_be__ = c_char
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000225_check_size(c_char)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000226
227class c_char_p(_SimpleCData):
228 _type_ = "z"
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000229_check_size(c_char_p, "P")
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000230
231class c_void_p(_SimpleCData):
232 _type_ = "P"
233c_voidp = c_void_p # backwards compatibility (to a bug)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000234_check_size(c_void_p)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000235
Guido van Rossumd8faa362007-04-27 19:54:29 +0000236class c_bool(_SimpleCData):
237 _type_ = "t"
238
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000239# This cache maps types to pointers to them.
240_pointer_type_cache = {}
241
242def POINTER(cls):
243 try:
244 return _pointer_type_cache[cls]
245 except KeyError:
246 pass
247 if type(cls) is str:
248 klass = type(_Pointer)("LP_%s" % cls,
249 (_Pointer,),
250 {})
251 _pointer_type_cache[id(klass)] = klass
252 return klass
253 else:
254 name = "LP_%s" % cls.__name__
255 klass = type(_Pointer)(name,
256 (_Pointer,),
257 {'_type_': cls})
258 _pointer_type_cache[cls] = klass
259 return klass
260
261try:
262 from _ctypes import set_conversion_mode
263except ImportError:
264 pass
265else:
266 if _os.name in ("nt", "ce"):
267 set_conversion_mode("mbcs", "ignore")
268 else:
269 set_conversion_mode("ascii", "strict")
270
271 class c_wchar_p(_SimpleCData):
272 _type_ = "Z"
273
274 class c_wchar(_SimpleCData):
275 _type_ = "u"
276
277 POINTER(c_wchar).from_param = c_wchar_p.from_param #_SimpleCData.c_wchar_p_from_param
278
279 def create_unicode_buffer(init, size=None):
280 """create_unicode_buffer(aString) -> character array
281 create_unicode_buffer(anInteger) -> character array
282 create_unicode_buffer(aString, anInteger) -> character array
283 """
Walter Dörwaldaa97f042007-05-03 21:05:51 +0000284 if isinstance(init, str):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000285 if size is None:
286 size = len(init)+1
287 buftype = c_wchar * size
288 buf = buftype()
289 buf.value = init
290 return buf
Walter Dörwaldaa97f042007-05-03 21:05:51 +0000291 elif isinstance(init, int):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000292 buftype = c_wchar * init
293 buf = buftype()
294 return buf
295 raise TypeError, init
296
297POINTER(c_char).from_param = c_char_p.from_param #_SimpleCData.c_char_p_from_param
298
299# XXX Deprecated
300def SetPointerType(pointer, cls):
301 if _pointer_type_cache.get(cls, None) is not None:
302 raise RuntimeError, \
303 "This type already exists in the cache"
Guido van Rossum1b01e5c2006-08-19 02:45:06 +0000304 if id(pointer) not in _pointer_type_cache:
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000305 raise RuntimeError, \
306 "What's this???"
307 pointer.set_type(cls)
308 _pointer_type_cache[cls] = pointer
309 del _pointer_type_cache[id(pointer)]
310
311
312def pointer(inst):
313 return POINTER(type(inst))(inst)
314
315# XXX Deprecated
316def ARRAY(typ, len):
317 return typ * len
318
319################################################################
320
321
322class CDLL(object):
323 """An instance of this class represents a loaded dll/shared
324 library, exporting functions using the standard C calling
325 convention (named 'cdecl' on Windows).
326
327 The exported functions can be accessed as attributes, or by
328 indexing with the function name. Examples:
329
330 <obj>.qsort -> callable object
331 <obj>['qsort'] -> callable object
332
333 Calling the functions releases the Python GIL during the call and
334 reaquires it afterwards.
335 """
336 class _FuncPtr(_CFuncPtr):
337 _flags_ = _FUNCFLAG_CDECL
338 _restype_ = c_int # default, can be overridden in instances
339
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000340 def __init__(self, name, mode=DEFAULT_MODE, handle=None):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000341 self._name = name
342 if handle is None:
343 self._handle = _dlopen(self._name, mode)
344 else:
345 self._handle = handle
346
347 def __repr__(self):
348 return "<%s '%s', handle %x at %x>" % \
349 (self.__class__.__name__, self._name,
350 (self._handle & (_sys.maxint*2 + 1)),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000351 id(self) & (_sys.maxint*2 + 1))
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000352
353 def __getattr__(self, name):
354 if name.startswith('__') and name.endswith('__'):
355 raise AttributeError, name
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000356 func = self.__getitem__(name)
357 setattr(self, name, func)
358 return func
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000359
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000360 def __getitem__(self, name_or_ordinal):
361 func = self._FuncPtr((name_or_ordinal, self))
Walter Dörwaldaa97f042007-05-03 21:05:51 +0000362 if not isinstance(name_or_ordinal, int):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000363 func.__name__ = name_or_ordinal
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000364 return func
365
366class PyDLL(CDLL):
367 """This class represents the Python library itself. It allows to
368 access Python API functions. The GIL is not released, and
369 Python exceptions are handled correctly.
370 """
371 class _FuncPtr(_CFuncPtr):
372 _flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI
373 _restype_ = c_int # default, can be overridden in instances
374
375if _os.name in ("nt", "ce"):
Tim Peterse8d09e52006-03-09 01:15:05 +0000376
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000377 class WinDLL(CDLL):
378 """This class represents a dll exporting functions using the
379 Windows stdcall calling convention.
380 """
381 class _FuncPtr(_CFuncPtr):
382 _flags_ = _FUNCFLAG_STDCALL
383 _restype_ = c_int # default, can be overridden in instances
384
385 # XXX Hm, what about HRESULT as normal parameter?
386 # Mustn't it derive from c_long then?
387 from _ctypes import _check_HRESULT, _SimpleCData
388 class HRESULT(_SimpleCData):
389 _type_ = "l"
390 # _check_retval_ is called with the function's result when it
391 # is used as restype. It checks for the FAILED bit, and
392 # raises a WindowsError if it is set.
393 #
394 # The _check_retval_ method is implemented in C, so that the
395 # method definition itself is not included in the traceback
396 # when it raises an error - that is what we want (and Python
397 # doesn't have a way to raise an exception in the caller's
398 # frame).
399 _check_retval_ = _check_HRESULT
Tim Peterse8d09e52006-03-09 01:15:05 +0000400
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000401 class OleDLL(CDLL):
402 """This class represents a dll exporting functions using the
403 Windows stdcall calling convention, and returning HRESULT.
404 HRESULT error values are automatically raised as WindowsError
405 exceptions.
406 """
407 class _FuncPtr(_CFuncPtr):
408 _flags_ = _FUNCFLAG_STDCALL
409 _restype_ = HRESULT
410
Thomas Wouters477c8d52006-05-27 19:21:47 +0000411class LibraryLoader(object):
412 def __init__(self, dlltype):
413 self._dlltype = dlltype
414
415 def __getattr__(self, name):
416 if name[0] == '_':
417 raise AttributeError(name)
418 dll = self._dlltype(name)
419 setattr(self, name, dll)
420 return dll
421
422 def __getitem__(self, name):
423 return getattr(self, name)
424
425 def LoadLibrary(self, name):
426 return self._dlltype(name)
427
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000428cdll = LibraryLoader(CDLL)
429pydll = LibraryLoader(PyDLL)
430
431if _os.name in ("nt", "ce"):
432 pythonapi = PyDLL("python dll", None, _sys.dllhandle)
433elif _sys.platform == "cygwin":
434 pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
435else:
436 pythonapi = PyDLL(None)
437
438
439if _os.name in ("nt", "ce"):
440 windll = LibraryLoader(WinDLL)
441 oledll = LibraryLoader(OleDLL)
442
443 if _os.name == "nt":
444 GetLastError = windll.kernel32.GetLastError
445 else:
446 GetLastError = windll.coredll.GetLastError
447
448 def WinError(code=None, descr=None):
449 if code is None:
450 code = GetLastError()
451 if descr is None:
452 descr = FormatError(code).strip()
453 return WindowsError(code, descr)
454
455_pointer_type_cache[None] = c_void_p
456
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000457if sizeof(c_uint) == sizeof(c_void_p):
458 c_size_t = c_uint
459elif sizeof(c_ulong) == sizeof(c_void_p):
460 c_size_t = c_ulong
Thomas Wouters89f507f2006-12-13 04:49:30 +0000461elif sizeof(c_ulonglong) == sizeof(c_void_p):
462 c_size_t = c_ulonglong
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000463
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000464# functions
465
466from _ctypes import _memmove_addr, _memset_addr, _string_at_addr, _cast_addr
467
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000468## void *memmove(void *, const void *, size_t);
469memmove = CFUNCTYPE(c_void_p, c_void_p, c_void_p, c_size_t)(_memmove_addr)
470
471## void *memset(void *, int, size_t)
472memset = CFUNCTYPE(c_void_p, c_void_p, c_int, c_size_t)(_memset_addr)
473
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000474def PYFUNCTYPE(restype, *argtypes):
475 class CFunctionType(_CFuncPtr):
476 _argtypes_ = argtypes
477 _restype_ = restype
478 _flags_ = _FUNCFLAG_CDECL | _FUNCFLAG_PYTHONAPI
479 return CFunctionType
Thomas Wouters477c8d52006-05-27 19:21:47 +0000480
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000481_cast = PYFUNCTYPE(py_object, c_void_p, py_object, py_object)(_cast_addr)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000482def cast(obj, typ):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000483 return _cast(obj, obj, typ)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000484
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000485_string_at = CFUNCTYPE(py_object, c_void_p, c_int)(_string_at_addr)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000486def string_at(ptr, size=-1):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000487 """string_at(addr[, size]) -> string
488
489 Return the string at addr."""
490 return _string_at(ptr, size)
491
492try:
493 from _ctypes import _wstring_at_addr
494except ImportError:
495 pass
496else:
497 _wstring_at = CFUNCTYPE(py_object, c_void_p, c_int)(_wstring_at_addr)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000498 def wstring_at(ptr, size=-1):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000499 """wstring_at(addr[, size]) -> string
500
501 Return the string at addr."""
502 return _wstring_at(ptr, size)
Tim Peterse8d09e52006-03-09 01:15:05 +0000503
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000504
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000505if _os.name in ("nt", "ce"): # COM stuff
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000506 def DllGetClassObject(rclsid, riid, ppv):
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000507 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000508 ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*'])
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000509 except ImportError:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000510 return -2147221231 # CLASS_E_CLASSNOTAVAILABLE
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000511 else:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000512 return ccom.DllGetClassObject(rclsid, riid, ppv)
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000513
514 def DllCanUnloadNow():
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000515 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000516 ccom = __import__("comtypes.server.inprocserver", globals(), locals(), ['*'])
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000517 except ImportError:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000518 return 0 # S_OK
519 return ccom.DllCanUnloadNow()
Thomas Hellerbabddfc2006-03-08 19:56:54 +0000520
521from ctypes._endian import BigEndianStructure, LittleEndianStructure
522
523# Fill in specifically-sized types
524c_int8 = c_byte
525c_uint8 = c_ubyte
526for kind in [c_short, c_int, c_long, c_longlong]:
527 if sizeof(kind) == 2: c_int16 = kind
528 elif sizeof(kind) == 4: c_int32 = kind
529 elif sizeof(kind) == 8: c_int64 = kind
530for kind in [c_ushort, c_uint, c_ulong, c_ulonglong]:
531 if sizeof(kind) == 2: c_uint16 = kind
532 elif sizeof(kind) == 4: c_uint32 = kind
533 elif sizeof(kind) == 8: c_uint64 = kind
534del(kind)