add skeleton for variant type support
[pywinlite.git] / globalmem.py
blob0af9629604290ee5bb96186d8602ee1e7f0a3e9b
1 #Copyright (c) 2008 Vincent Povirk
3 #Permission is hereby granted, free of charge, to any person
4 #obtaining a copy of this software and associated documentation
5 #files (the "Software"), to deal in the Software without
6 #restriction, including without limitation the rights to use,
7 #copy, modify, merge, publish, distribute, sublicense, and/or sell
8 #copies of the Software, and to permit persons to whom the
9 #Software is furnished to do so, subject to the following
10 #conditions:
12 #The above copyright notice and this permission notice shall be
13 #included in all copies or substantial portions of the Software.
15 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 #EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 #OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 #NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 #WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 #FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 #OTHER DEALINGS IN THE SOFTWARE.
24 # Global memory definitions
25 # see http://msdn.microsoft.com/en-us/library/aa366596(VS.85).aspx
27 from ctypes import windll, WinError
28 from winliteutils import get_symbols, NONZERO, ZERO
29 from windef import UINT, SIZE_T, HGLOBAL, LPCVOID, LPVOID
31 _kernel32 = windll.kernel32
33 GMEM_FIXED = 0x0000
34 GMEM_MOVEABLE = 0x0002
35 GMEM_NOCOMPACT = 0x0010
36 GMEM_NODISCARD = 0x0020
37 GMEM_ZEROINIT = 0x0040
38 GMEM_MODIFY = 0x0080
39 GMEM_DISCARDABLE = 0x0100
40 GMEM_NOT_BANKED = 0x1000
41 GMEM_SHARE = 0x2000
42 GMEM_DDESHARE = 0x2000
43 GMEM_NOTIFY = 0x4000
44 GMEM_LOWER = GMEM_NOT_BANKED
45 GMEM_DISCARDED = 0x4000
46 GMEM_LOCKCOUNT = 0x00ff
47 GMEM_INVALID_HANDLE = 0x8000
49 GHND = (GMEM_MOVEABLE | GMEM_ZEROINIT)
50 GPTR = (GMEM_FIXED | GMEM_ZEROINIT)
52 def gmem_handle(x):
53 if x == GMEM_INVALID_HANDLE:
54 raise WinError()
55 return x
57 get_symbols(globals(), _kernel32, ['GlobalAlloc', 'GlobalFlags', 'GlobalFree', 'GlobalHandle', 'GlobalLock', 'GlobalReAlloc', 'GlobalSize', 'GlobalUnlock'])
59 GlobalAlloc.argtypes = [UINT, SIZE_T]
60 GlobalAlloc.restype = NONZERO
62 GlobalFlags.argtypes = [HGLOBAL]
63 GlobalFlags.restype = gmem_handle
65 GlobalFree.argtypes = [HGLOBAL]
66 GlobalFree.restype = ZERO
68 GlobalHandle.argtypes = [LPCVOID]
69 GlobalHandle.restype = NONZERO
71 GlobalLock.argtypes = [HGLOBAL]
72 GlobalLock.restype = NONZERO
74 GlobalReAlloc.argtypes = [HGLOBAL, SIZE_T, UINT]
75 GlobalReAlloc.restype = NONZERO
77 GlobalSize.argtypes = [HGLOBAL]
78 GlobalSize.restype = NONZERO
80 GlobalUnlock.argtypes = [HGLOBAL]
81 GlobalUnlock.restype = NONZERO
83 #obsolete:
84 #GlobalLRUNewest = lambda x: x
85 #GlobalLRUOldest = lambda x: x
87 def GlobalDiscard(handle):
88 return GlobalReAlloc(handle, 0, GMEM_MOVEABLE)
90 class GlobalMemoryObject(object):
91 __slots__ = ['handle', 'needs_free', '_as_parameter_']
93 def __init__(self, flags=GMEM_MOVEABLE, size=0, handle=None, needs_free=True):
94 if handle is None:
95 handle = GlobalAlloc(flags, size)
96 self.handle = self._as_parameter_ = handle
97 self.needs_free = needs_free
99 def free(self):
100 if self.needs_free:
101 if GlobalFree(self) != 0:
102 raise WinError()
103 self.needs_free = False
104 self.handle = self._as_parameter_ = 0
106 def lock(self):
107 return GlobalLock(self)
109 def unlock(self):
110 GlobalUnlock(self)
112 def __enter__(self):
113 return self.lock()
115 def __exit__(self, exc_type, exc_val, exc_tb):
116 self.unlock()
118 def realloc(self, size, flags=GMEM_MOVEABLE):
119 GlobalReAlloc(self, size, flags)
121 def discard(self):
122 GlobalDiscard(self)
124 def get_flags(self):
125 return GlobalFlags(self)
127 def get_size(self):
128 return GlobalSize(self)
130 def __del__(self):
131 self.free()