forked from ropnop/go-clr
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsafearray.go
340 lines (288 loc) · 10.5 KB
/
safearray.go
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// +build windows
package clr
import (
"fmt"
"syscall"
"unsafe"
)
// SafeArray represents a safe array
// defined in OAIdl.h
// typedef struct tagSAFEARRAY {
// USHORT cDims;
// USHORT fFeatures;
// ULONG cbElements;
// ULONG cLocks;
// PVOID pvData;
// SAFEARRAYBOUND rgsabound[1];
// } SAFEARRAY;
// https://docs.microsoft.com/en-us/windows/win32/api/oaidl/ns-oaidl-safearray
// https://docs.microsoft.com/en-us/archive/msdn-magazine/2017/march/introducing-the-safearray-data-structure
type SafeArray struct {
// cDims is the number of dimensions
cDims uint16
// fFeatures is the feature flags
fFeatures uint16
// cbElements is the size of an array element
cbElements uint32
// cLocks is the number of times the array has been locked without a corresponding unlock
cLocks uint32
// pvData is the data
pvData uintptr
// rgsabout is one bound for each dimension
rgsabound [1]SafeArrayBound
}
// SafeArrayBound represents the bounds of one dimension of the array
// typedef struct tagSAFEARRAYBOUND {
// ULONG cElements;
// LONG lLbound;
// } SAFEARRAYBOUND, *LPSAFEARRAYBOUND;
// https://docs.microsoft.com/en-us/windows/win32/api/oaidl/ns-oaidl-safearraybound
type SafeArrayBound struct {
// cElements is the number of elements in the dimension
cElements uint32
// lLbound is the lowerbound of the dimension
lLbound int32
}
// CreateSafeArray is a wrapper function that takes in a Go byte array and creates a SafeArray containing unsigned bytes
// by making two syscalls and copying raw memory into the correct spot.
func CreateSafeArray(rawBytes []byte) (*SafeArray, error) {
debugPrint("Entering into safearray.CreateSafeArray()...")
safeArrayBounds := SafeArrayBound{
cElements: uint32(len(rawBytes)),
lLbound: int32(0),
}
safeArray, err := SafeArrayCreate(VT_UI1, 1, &safeArrayBounds)
if err != nil {
return nil, err
}
// now we need to use RtlCopyMemory to copy our bytes to the SafeArray
modNtDll := syscall.MustLoadDLL("ntdll.dll")
procRtlCopyMemory := modNtDll.MustFindProc("RtlCopyMemory")
// TODO Replace RtlCopyMemory with SafeArrayPutElement or SafeArrayAccessData
// void RtlCopyMemory(
// void* Destination,
// const void* Source,
// size_t Length
// );
// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-rtlcopymemory
_, _, err = procRtlCopyMemory.Call(
safeArray.pvData,
uintptr(unsafe.Pointer(&rawBytes[0])),
uintptr(len(rawBytes)),
)
if err != syscall.Errno(0) {
return nil, err
}
return safeArray, nil
}
// SafeArrayCreate creates a new array descriptor, allocates and initializes the data for the array, and returns a pointer to the new array descriptor.
// SAFEARRAY * SafeArrayCreate(
// VARTYPE vt,
// UINT cDims,
// SAFEARRAYBOUND *rgsabound
// );
// Varient types: https://docs.microsoft.com/en-us/windows/win32/api/wtypes/ne-wtypes-varenum
// https://docs.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-safearraycreate
func SafeArrayCreate(vt uint16, cDims uint32, rgsabound *SafeArrayBound) (safeArray *SafeArray, err error) {
debugPrint("Entering into safearray.SafeArrayCreate()...")
modOleAuto := syscall.MustLoadDLL("OleAut32.dll")
procSafeArrayCreate := modOleAuto.MustFindProc("SafeArrayCreate")
ret, _, err := procSafeArrayCreate.Call(
uintptr(vt),
uintptr(cDims),
uintptr(unsafe.Pointer(rgsabound)),
)
if err != syscall.Errno(0) {
return
}
err = nil
if ret == 0 {
err = fmt.Errorf("the OleAut32!SafeArrayCreate function return 0x%x and the SafeArray was not created", ret)
return
}
// Unable to avoid misuse of unsafe.Pointer because the Windows API call returns the safeArray pointer in the "ret" value. This is a go vet false positive
safeArray = (*SafeArray)(unsafe.Pointer(ret))
return
}
// SysAllocString converts a Go string to a BTSR string, that is a unicode string prefixed with its length.
// Allocates a new string and copies the passed string into it.
// It returns a pointer to the string's content.
// BSTR SysAllocString(
// const OLECHAR *psz
// );
// https://docs.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-sysallocstring
func SysAllocString(str string) (unsafe.Pointer, error) {
debugPrint("Entering into safearray.SysAllocString()...")
modOleAuto := syscall.MustLoadDLL("OleAut32.dll")
sysAllocString := modOleAuto.MustFindProc("SysAllocString")
input := utf16Le(str)
ret, _, err := sysAllocString.Call(
uintptr(unsafe.Pointer(&input[0])),
)
if err != syscall.Errno(0) {
return nil, err
}
// TODO Return a pointer to a BSTR instead of an unsafe.Pointer
// Unable to avoid misuse of unsafe.Pointer because the Windows API call returns the safeArray pointer in the "ret" value. This is a go vet false positive
return unsafe.Pointer(ret), nil
}
// SafeArrayPutElement pushes an element to the safe array at a given index
// HRESULT SafeArrayPutElement(
// SAFEARRAY *psa,
// LONG *rgIndices,
// void *pv
// );
// https://docs.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-safearrayputelement
func SafeArrayPutElement(psa *SafeArray, rgIndices int32, pv unsafe.Pointer) error {
debugPrint("Entering into safearray.SafeArrayPutElement()...")
modOleAuto := syscall.MustLoadDLL("OleAut32.dll")
safeArrayPutElement := modOleAuto.MustFindProc("SafeArrayPutElement")
hr, _, err := safeArrayPutElement.Call(
uintptr(unsafe.Pointer(psa)),
uintptr(unsafe.Pointer(&rgIndices)),
uintptr(pv),
)
if err != syscall.Errno(0) {
return err
}
if hr != S_OK {
return fmt.Errorf("the OleAut32!SafeArrayPutElement call returned a non-zero HRESULT: 0x%x", hr)
}
return nil
}
// SafeArrayLock increments the lock count of an array, and places a pointer to the array data in pvData of the array descriptor
// HRESULT SafeArrayLock(
// SAFEARRAY *psa
// );
// https://docs.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-safearraylock
func SafeArrayLock(psa *SafeArray) error {
debugPrint("Entering into safearray.SafeArrayLock()...")
modOleAuto := syscall.MustLoadDLL("OleAut32.dll")
safeArrayCreate := modOleAuto.MustFindProc("SafeArrayCreate")
hr, _, err := safeArrayCreate.Call(uintptr(unsafe.Pointer(psa)))
if err != syscall.Errno(0) {
return err
}
if hr != S_OK {
return fmt.Errorf("the OleAut32!SafeArrayCreate function returned a non-zero HRESULT: 0x%x", hr)
}
return nil
}
// SafeArrayGetVartype gets the VARTYPE stored in the specified safe array
// HRESULT SafeArrayGetVartype(
// SAFEARRAY *psa,
// VARTYPE *pvt
// );
// https://docs.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-safearraygetvartype
func SafeArrayGetVartype(psa *SafeArray) (uint16, error) {
debugPrint("Entering into safearray.SafeArrayGetVartype()...")
var vt uint16
modOleAuto := syscall.MustLoadDLL("OleAut32.dll")
safeArrayGetVartype := modOleAuto.MustFindProc("SafeArrayGetVartype")
hr, _, err := safeArrayGetVartype.Call(
uintptr(unsafe.Pointer(psa)),
uintptr(unsafe.Pointer(&vt)),
)
if err != syscall.Errno(0) {
return 0, err
}
if hr != S_OK {
return 0, fmt.Errorf("the OleAut32!SafeArrayGetVartype function returned a non-zero HRESULT: 0x%x", hr)
}
return vt, nil
}
// SafeArrayAccessData increments the lock count of an array, and retrieves a pointer to the array data
// HRESULT SafeArrayAccessData(
// SAFEARRAY *psa,
// void HUGEP **ppvData
// );
// https://docs.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-safearrayaccessdata
func SafeArrayAccessData(psa *SafeArray) (*uintptr, error) {
debugPrint("Entering into safearray.SafeArrayAccessData()...")
var ppvData *uintptr
modOleAuto := syscall.MustLoadDLL("OleAut32.dll")
safeArrayAccessData := modOleAuto.MustFindProc("SafeArrayAccessData")
hr, _, err := safeArrayAccessData.Call(
uintptr(unsafe.Pointer(psa)),
uintptr(unsafe.Pointer(&ppvData)),
)
if err != syscall.Errno(0) {
return nil, err
}
if hr != S_OK {
return nil, fmt.Errorf("the oleaut32!SafeArrayAccessData function returned a non-zero HRESULT: 0x%x", hr)
}
return ppvData, nil
}
// SafeArrayGetLBound gets the lower bound for any dimension of the specified safe array
// HRESULT SafeArrayGetLBound(
// SAFEARRAY *psa,
// UINT nDim,
// LONG *plLbound
// );
// https://docs.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-safearraygetlbound
func SafeArrayGetLBound(psa *SafeArray, nDim uint32) (uint32, error) {
debugPrint("Entering into safearray.SafeArrayGetLBound()...")
var plLbound uint32
modOleAuto := syscall.MustLoadDLL("OleAut32.dll")
safeArrayGetLBound := modOleAuto.MustFindProc("SafeArrayGetLBound")
hr, _, err := safeArrayGetLBound.Call(
uintptr(unsafe.Pointer(psa)),
uintptr(nDim),
uintptr(unsafe.Pointer(&plLbound)),
)
if err != syscall.Errno(0) {
return 0, err
}
if hr != S_OK {
return 0, fmt.Errorf("the oleaut32!SafeArrayGetLBound function returned a non-zero HRESULT: 0x%x", hr)
}
return plLbound, nil
}
// SafeArrayGetUBound gets the upper bound for any dimension of the specified safe array
// HRESULT SafeArrayGetUBound(
// SAFEARRAY *psa,
// UINT nDim,
// LONG *plUbound
// );
// https://docs.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-safearraygetubound
func SafeArrayGetUBound(psa *SafeArray, nDim uint32) (uint32, error) {
debugPrint("Entering into safearray.SafeArrayGetUBound()...")
var plUbound uint32
modOleAuto := syscall.MustLoadDLL("OleAut32.dll")
safeArrayGetUBound := modOleAuto.MustFindProc("SafeArrayGetUBound")
hr, _, err := safeArrayGetUBound.Call(
uintptr(unsafe.Pointer(psa)),
uintptr(nDim),
uintptr(unsafe.Pointer(&plUbound)),
)
if err != syscall.Errno(0) {
return 0, err
}
if hr != S_OK {
return 0, fmt.Errorf("the oleaut32!SafeArrayGetUBound function returned a non-zero HRESULT: 0x%x", hr)
}
return plUbound, nil
}
// SafeArrayDestroy Destroys an existing array descriptor and all of the data in the array.
// If objects are stored in the array, Release is called on each object in the array.
// HRESULT SafeArrayDestroy(
// SAFEARRAY *psa
// );
func SafeArrayDestroy(psa *SafeArray) error {
debugPrint("Entering into safearray.SafeArrayDestroy()...")
modOleAuto := syscall.MustLoadDLL("OleAut32.dll")
safeArrayDestroy := modOleAuto.MustFindProc("SafeArrayDestroy")
hr, _, err := safeArrayDestroy.Call(
uintptr(unsafe.Pointer(psa)),
0,
0,
)
if err != syscall.Errno(0) {
return fmt.Errorf("the oleaut32!SafeArrayDestroy function call returned an error:\n%s", err)
}
if hr != S_OK {
return fmt.Errorf("the oleaut32!SafeArrayDestroy function returned a non-zero HRESULT: 0x%x", hr)
}
return nil
}