-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemArray.au3
196 lines (183 loc) · 7.62 KB
/
MemArray.au3
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include<Memory.au3>
#include<Array.au3>
Global Const $__MemArray_HEAD = "dword iElSize;"
Global Const $__MemArray_HEADSIZE = __MemArray_SIZEOF($__MemArray_HEAD)
; Author: Prog@ndy
Func __MemArray_SIZEOF($tagStruct)
Return DllStructGetSize(DllStructCreate($tagStruct, 1))
EndFunc ;==>__MemArray_SIZEOF
Func __MemArrayLockedPtr($hMem)
If Not IsPtr($hMem) Or $hMem = 0 Or Not __MemIsGlobal($hMem) Then Return SetError(1,0,0)
Return _MemGlobalLock($hMem)+$__MemArray_HEADSIZE
EndFunc
Func __MemArrayUnLock($hMem)
If Not IsPtr($hMem) Or $hMem = 0 Or Not __MemIsGlobal($hMem) Then Return SetError(1,0,0)
_MemGlobalUnlock($hMem)
EndFunc
; Author: Prog@ndy
Func _MemArrayCreate($tagStruct)
Local $iSize = __MemArray_SIZEOF($tagStruct)
If $iSize = 0 Then Return SetError(1, 0, 0)
Local $hMem = _MemGlobalAlloc($__MemArray_HEADSIZE, $GMEM_MOVEABLE)
If $hMem = 0 Then Return SetError(2, 0, 0)
DllStructSetData(DllStructCreate($__MemArray_HEAD, _MemGlobalLock($hMem)), 1, $iSize)
_MemGlobalUnlock($hMem)
Return $hMem
EndFunc ;==>_MemArrayCreate
; Author: Prog@ndy
Func __MemArrayElementSize($hMem)
If Not IsPtr($hMem) Or $hMem = 0 Or Not __MemIsGlobal($hMem) Then Return SetError(1, 0, 0)
Local $iSize = DllStructGetData(DllStructCreate($__MemArray_HEAD, _MemGlobalLock($hMem)), 1)
_MemGlobalUnlock($hMem)
Return $iSize
EndFunc ;==>__MemArrayElementSize
; Author: Prog@ndy
Func _MemArrayFree($hMem)
Return _MemGlobalFree($hMem)
EndFunc ;==>_MemArrayFree
; Author: Prog@ndy
Func _MemArrayAdd($hMem, ByRef $stEntry)
If Not IsPtr($hMem) Or $hMem = 0 Or Not __MemIsGlobal($hMem) Then Return SetError(1, 0, -1)
If Not (IsDllStruct($stEntry) Or IsPtr($stEntry)) Then Return SetError(2, 0, -1)
Local $size = _MemGlobalSize($hMem)
Local $iElSize = __MemArrayElementSize($hMem)
Local $result = __MemGlobalReAlloc($hMem, $size + $iElSize, $GHND)
If Not $result Then Return SetError(2, 0, 0)
Local $indX = (($size - $__MemArray_HEADSIZE) / $iElSize)
If IsPtr($stEntry) Then
__MemCopyMemory($stEntry, _MemGlobalLock($hMem) + $size, $iElSize)
Else
__MemCopyMemory(DllStructGetPtr($stEntry), _MemGlobalLock($hMem) + $size, $iElSize)
EndIf
_MemGlobalUnlock($hMem)
Return $indX
EndFunc ;==>_MemArrayAdd
; Author: Prog@ndy
Func _MemArrayDelete($hMem, $indX)
If Not IsPtr($hMem) Or $hMem = 0 Or Not __MemIsGlobal($hMem) Then Return SetError(1, 0, 0)
Local $size = _MemGlobalSize($hMem)
Local $iElSize = __MemArrayElementSize($hMem)
Local $maxIndX = ($size - $__MemArray_HEADSIZE) / $iElSize
If $indX < 0 Or $indX > $maxIndX Then Return SetError(2, 0, 0)
If $size > ($__MemArray_HEADSIZE + $iElSize) Then
Local $hPtr = _MemGlobalLock($hMem)
Local $deletedElementOffset = ($indX * $iElSize) + $__MemArray_HEADSIZE
_MemMoveMemory($hPtr + $deletedElementOffset + $iElSize, $hPtr + $deletedElementOffset, $size - ($deletedElementOffset + $iElSize))
_MemGlobalUnlock($hMem)
EndIf
__MemGlobalReAlloc($hMem, $size - $iElSize, $GMEM_MOVEABLE)
Return 1
EndFunc ;==>_MemArrayDelete
; Author: Prog@ndy
Func _MemArrayGet($hMem, $indX, $tagStruct)
If Not IsPtr($hMem) Or $hMem = 0 Or Not __MemIsGlobal($hMem) Then Return SetError(1, 0, 0)
Local $size = _MemGlobalSize($hMem)
Local $iElSize = __MemArrayElementSize($hMem)
Local $maxIndX = ($size - $__MemArray_HEADSIZE) / $iElSize
If $indX < 0 Or $indX > $maxIndX Then Return SetError(2, 0, 0)
If IsPtr($tagStruct) Then
__MemCopyMemory(_MemGlobalLock($hMem) + $__MemArray_HEADSIZE + $indX * $iElSize, $tagStruct, $iElSize)
Local $struct = $tagStruct
Else
Local $struct = DllStructCreate($tagStruct)
If @error Then Return SetError(2,0,0)
__MemCopyMemory(_MemGlobalLock($hMem) + $__MemArray_HEADSIZE + $indX * $iElSize, DllStructGetPtr($struct), $iElSize)
EndIf
_MemGlobalUnlock($hMem)
Return $struct
EndFunc ;==>_MemArrayGet
; Author: Prog@ndy
Func _MemArrayGetDelete($hMem, $indX, $tagStruct)
If Not IsPtr($hMem) Or $hMem = 0 Or Not __MemIsGlobal($hMem) Then Return SetError(1, 0, 0)
Local $size = _MemGlobalSize($hMem)
Local $iElSize = __MemArrayElementSize($hMem)
Local $maxIndX = ($size - $__MemArray_HEADSIZE) / $iElSize
If $indX < 0 Or $indX > $maxIndX Then Return SetError(2, 0, 0)
Local $hPtr = _MemGlobalLock($hMem)
If IsPtr($tagStruct) Then
__MemCopyMemory($hPtr + $__MemArray_HEADSIZE + $indX * $iElSize, $tagStruct, $iElSize)
Else
Local $struct = DllStructCreate($tagStruct)
If @error Then Return SetError(2,_MemGlobalUnlock($hMem),0)
__MemCopyMemory($hPtr + $__MemArray_HEADSIZE + $indX * $iElSize, DllStructGetPtr($struct), $iElSize)
EndIf
If $size > ($__MemArray_HEADSIZE + $iElSize) Then
Local $deletedElementOffset = ($indX * $iElSize) + $__MemArray_HEADSIZE
_MemMoveMemory($hPtr + $deletedElementOffset + $iElSize, $hPtr + $deletedElementOffset, $size - ($deletedElementOffset + $iElSize))
EndIf
_MemGlobalUnlock($hMem)
__MemGlobalReAlloc($hMem, $size - $iElSize, $GMEM_MOVEABLE)
Return $struct
EndFunc ;==>_MemArrayGet
#cs
Func _MemArrayGetToArray($hMem)
If Not IsPtr($hMem) Or $hMem = 0 Or Not __MemIsGlobal($hMem) Then Return SetError(1,0,0)
Local $size = _MemGlobalSize($hMem)
Local $maxIndX = $size/$__MemArray_PTRSIZE
If $maxIndX < 1 Then Return SetError(2,0,0)
Local $struct = DllStructCreate("ptr[" & $maxIndX & "]", _MemGlobalLock($hMem) )
Local $array[$maxIndX]
For $i = 1 To $maxIndX
$array[$i-1] = DllStructGetData($struct,1,$i)
Next
_MemGlobalUnlock($hMem)
Return $array
EndFunc
#ce
; Author: Prog@ndy
Func _MemArraySet($hMem, $indX, ByRef $stEntry)
If Not IsPtr($hMem) Or $hMem = 0 Or Not __MemIsGlobal($hMem) Then Return SetError(1, 0, 0)
Local $size = _MemGlobalSize($hMem)
Local $iElSize = __MemArrayElementSize($hMem)
Local $maxIndX = ($size - $__MemArray_HEADSIZE) / $iElSize
If $indX < 0 Or $indX > $maxIndX Then Return SetError(2, 0, 0)
Local $pEntry = _MemGlobalLock($hMem) + $__MemArray_HEADSIZE + $indX * $iElSize
__MemZeroMemory($pEntry, $iElSize)
If IsPtr($stEntry) Then
__MemCopyMemory($stEntry, $pEntry, $iElSize)
ElseIf IsDllStruct($stEntry) Then
__MemCopyMemory(DllStructGetPtr($stEntry), $pEntry, $iElSize)
Else
Return SetError(2,0,0)
EndIf
_MemGlobalUnlock($hMem)
Return 1
EndFunc ;==>_MemArraySet
; Author: Prog@ndy
Func _MemArrayUBound($hMem)
If Not IsPtr($hMem) Or $hMem = 0 Or Not __MemIsGlobal($hMem) Then Return SetError(1, 0, 0)
Local $iElSize = __MemArrayElementSize($hMem)
Return ((_MemGlobalSize($hMem) - $__MemArray_HEADSIZE) / $iElSize)
EndFunc ;==>_MemArrayUBound
; Author: Prog@ndy
Func __MemIsGlobal($hMem)
Local $result = __MemGlobalFlags($hMem)
If @error Or $result == $GMEM_INVALID_HANDLE Then Return 0
Return 1
EndFunc ;==>__MemIsGlobal
; Author: Prog@ndy
Func __MemGlobalReAlloc($hMem, $iBytes, $iFlags)
Local $aResult = DllCall("Kernel32.dll", "ptr", "GlobalReAlloc", "ptr", $hMem, "ulong", $iBytes, "uint", $iFlags)
Return $aResult[0]
EndFunc ;==>__MemGlobalReAlloc
; Author: Prog@ndy
Func __MemGlobalFlags($hMem)
Local $aResult = DllCall("Kernel32.dll", "uint", "GlobalFlags", "ptr", $hMem)
Return $aResult[0]
EndFunc ;==>__MemGlobalFlags
; Author: Prog@ndy
Func __MemGlobalDiscard($hMem)
Return __MemGlobalReAlloc($hMem, 0, $GMEM_MOVEABLE)
EndFunc ;==>__MemGlobalDiscard
; Author: Prog@ndy
Func __MemCopyMemory($pSource, $pDest, $iLength)
DllCall("msvcrt.dll", "none:cdecl", "memcpy", "ptr", $pDest, "ptr", $pSource, "dword", $iLength)
EndFunc ;==>__MemCopyMemory
; Author: Prog@ndy
Func __MemFillMemory($pDest, $ubFill, $iLength)
DllCall("kernel32.dll", "none", "RtlFillMemory", "ptr", $pDest, "dword", $iLength, "ubyte", $ubFill)
EndFunc ;==>__MemFillMemory
; Author: Prog@ndy
Func __MemZeroMemory($pDest, $iLength)
DllCall("kernel32.dll", "none", "RtlZeroMemory", "ptr", $pDest, "dword", $iLength)
EndFunc ;==>__MemZeroMemory