1 /*************************************************************************
2 * OLE Automation - SafeArray
3 *
4 * This file contains the implementation of the SafeArray functions.
5 *
6 * Copyright 1999 Sylvain St-Germain
7 * Copyright 2002-2003 Marcus Meissner
8 * Copyright 2003 Jon Griffiths
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24 /* Memory Layout of a SafeArray:
25 *
26 * -0x10: start of memory.
27 * -0x10: GUID for VT_DISPATCH and VT_UNKNOWN safearrays (if FADF_HAVEIID)
28 * -0x04: DWORD varianttype; (for all others, except VT_RECORD) (if FADF_HAVEVARTYPE)
29 * -0x4: IRecordInfo* iface; (if FADF_RECORD, for VT_RECORD (can be NULL))
30 * 0x00: SAFEARRAY,
31 * 0x10: SAFEARRAYBOUNDS[0...]
32 */
33
34 #include "config.h"
35
36 #include <string.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39
40 #define COBJMACROS
41
42 #include "windef.h"
43 #include "winerror.h"
44 #include "winbase.h"
45 #include "variant.h"
46 #include "wine/debug.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(variant);
49
50 /************************************************************************
51 * SafeArray {OLEAUT32}
52 *
53 * NOTES
54 * The SafeArray data type provides the underlying interface for Ole
55 * Automations arrays, used for example to represent array types in
56 * Visual Basic(tm) and to gather user defined parameters for invocation through
57 * an IDispatch interface.
58 *
59 * Safe arrays provide bounds checking and automatically manage the data
60 * types they contain, for example handing reference counting and copying
61 * of interface pointers. User defined types can be stored in arrays
62 * using the IRecordInfo interface.
63 *
64 * There are two types of SafeArray, normal and vectors. Normal arrays can have
65 * multiple dimensions and the data for the array is allocated separately from
66 * the array header. This is the most flexible type of array. Vectors, on the
67 * other hand, are fixed in size and consist of a single allocated block, and a
68 * single dimension.
69 *
70 * DATATYPES
71 * The following types of data can be stored within a SafeArray.
72 * Numeric:
73 *| VT_I1, VT_UI1, VT_I2, VT_UI2, VT_I4, VT_UI4, VT_I8, VT_UI8, VT_INT, VT_UINT,
74 *| VT_R4, VT_R8, VT_CY, VT_DECIMAL
75 * Interfaces:
76 *| VT_DISPATCH, VT_UNKNOWN, VT_RECORD
77 * Other:
78 *| VT_VARIANT, VT_INT_PTR, VT_UINT_PTR, VT_BOOL, VT_ERROR, VT_DATE, VT_BSTR
79 *
80 * FUNCTIONS
81 * BstrFromVector()
82 * VectorFromBstr()
83 */
84
85 /* Undocumented hidden space before the start of a SafeArray descriptor */
86 #define SAFEARRAY_HIDDEN_SIZE sizeof(GUID)
87
88 /* Allocate memory */
89 static inline LPVOID SAFEARRAY_Malloc(ULONG ulSize)
90 {
91 /* FIXME: Memory should be allocated and freed using a per-thread IMalloc
92 * instance returned from CoGetMalloc().
93 */
94 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulSize);
95 }
96
97 /* Free memory */
98 static inline BOOL SAFEARRAY_Free(LPVOID lpData)
99 {
100 return HeapFree(GetProcessHeap(), 0, lpData);
101 }
102
103 /* Get the size of a supported VT type (0 means unsupported) */
104 static DWORD SAFEARRAY_GetVTSize(VARTYPE vt)
105 {
106 switch (vt)
107 {
108 case VT_I1:
109 case VT_UI1: return sizeof(BYTE);
110 case VT_BOOL:
111 case VT_I2:
112 case VT_UI2: return sizeof(SHORT);
113 case VT_I4:
114 case VT_UI4:
115 case VT_R4:
116 case VT_ERROR: return sizeof(LONG);
117 case VT_R8:
118 case VT_I8:
119 case VT_UI8: return sizeof(LONG64);
120 case VT_INT:
121 case VT_UINT: return sizeof(INT);
122 case VT_INT_PTR:
123 case VT_UINT_PTR: return sizeof(UINT_PTR);
124 case VT_CY: return sizeof(CY);
125 case VT_DATE: return sizeof(DATE);
126 case VT_BSTR: return sizeof(BSTR);
127 case VT_DISPATCH: return sizeof(LPDISPATCH);
128 case VT_VARIANT: return sizeof(VARIANT);
129 case VT_UNKNOWN: return sizeof(LPUNKNOWN);
130 case VT_DECIMAL: return sizeof(DECIMAL);
131 /* Note: Return a non-zero size to indicate vt is valid. The actual size
132 * of a UDT is taken from the result of IRecordInfo_GetSize().
133 */
134 case VT_RECORD: return 32;
135 }
136 return 0;
137 }
138
139 /* Set the hidden data for an array */
140 static inline void SAFEARRAY_SetHiddenDWORD(SAFEARRAY* psa, DWORD dw)
141 {
142 /* Implementation data is stored in the 4 bytes before the header */
143 LPDWORD lpDw = (LPDWORD)psa;
144 lpDw[-1] = dw;
145 }
146
147 /* Get the hidden data from an array */
148 static inline DWORD SAFEARRAY_GetHiddenDWORD(SAFEARRAY* psa)
149 {
150 LPDWORD lpDw = (LPDWORD)psa;
151 return lpDw[-1];
152 }
153
154 /* Get the number of cells in a SafeArray */
155 static ULONG SAFEARRAY_GetCellCount(const SAFEARRAY *psa)
156 {
157 const SAFEARRAYBOUND* psab = psa->rgsabound;
158 USHORT cCount = psa->cDims;
159 ULONG ulNumCells = 1;
160
161 while (cCount--)
162 {
163 /* This is a valid bordercase. See testcases. -Marcus */
164 if (!psab->cElements)
165 return 0;
166 ulNumCells *= psab->cElements;
167 psab++;
168 }
169 return ulNumCells;
170 }
171
172 /* Allocate a descriptor for an array */
173 static HRESULT SAFEARRAY_AllocDescriptor(ULONG ulSize, SAFEARRAY **ppsaOut)
174 {
175 char *ptr = SAFEARRAY_Malloc(ulSize + SAFEARRAY_HIDDEN_SIZE);
176
177 if (!ptr)
178 {
179 *ppsaOut = NULL;
180 return E_UNEXPECTED;
181 }
182
183 *ppsaOut = (SAFEARRAY*)(ptr + SAFEARRAY_HIDDEN_SIZE);
184 return S_OK;
185 }
186
187 /* Set the features of an array */
188 static void SAFEARRAY_SetFeatures(VARTYPE vt, SAFEARRAY *psa)
189 {
190 /* Set the IID if we have one, otherwise set the type */
191 if (vt == VT_DISPATCH)
192 {
193 psa->fFeatures = FADF_HAVEIID;
194 SafeArraySetIID(psa, &IID_IDispatch);
195 }
196 else if (vt == VT_UNKNOWN)
197 {
198 psa->fFeatures = FADF_HAVEIID;
199 SafeArraySetIID(psa, &IID_IUnknown);
200 }
201 else if (vt == VT_RECORD)
202 psa->fFeatures = FADF_RECORD;
203 else
204 {
205 psa->fFeatures = FADF_HAVEVARTYPE;
206 SAFEARRAY_SetHiddenDWORD(psa, vt);
207 }
208 }
209
210 /* Create an array */
211 static SAFEARRAY* SAFEARRAY_Create(VARTYPE vt, UINT cDims, const SAFEARRAYBOUND *rgsabound, ULONG ulSize)
212 {
213 SAFEARRAY *psa = NULL;
214 unsigned int i;
215
216 if (!rgsabound)
217 return NULL;
218
219 if (SUCCEEDED(SafeArrayAllocDescriptorEx(vt, cDims, &psa)))
220 {
221 switch (vt)
222 {
223 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
224 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
225 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
226 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
227 }
228
229 for (i = 0; i < cDims; i++)
230 memcpy(psa->rgsabound + i, rgsabound + cDims - 1 - i, sizeof(SAFEARRAYBOUND));
231
232 if (ulSize)
233 psa->cbElements = ulSize;
234
235 if (!psa->cbElements || FAILED(SafeArrayAllocData(psa)))
236 {
237 SafeArrayDestroyDescriptor(psa);
238 psa = NULL;
239 }
240 }
241 return psa;
242 }
243
244 /* Create an array as a vector */
245 static SAFEARRAY* SAFEARRAY_CreateVector(VARTYPE vt, LONG lLbound, ULONG cElements, ULONG ulSize)
246 {
247 SAFEARRAY *psa = NULL;
248
249 if (ulSize || (vt == VT_RECORD))
250 {
251 /* Allocate the header and data together */
252 if (SUCCEEDED(SAFEARRAY_AllocDescriptor(sizeof(SAFEARRAY) + ulSize * cElements, &psa)))
253 {
254 SAFEARRAY_SetFeatures(vt, psa);
255
256 psa->cDims = 1;
257 psa->fFeatures |= FADF_CREATEVECTOR;
258 psa->pvData = &psa[1]; /* Data follows the header */
259 psa->cbElements = ulSize;
260 psa->rgsabound[0].cElements = cElements;
261 psa->rgsabound[0].lLbound = lLbound;
262
263 switch (vt)
264 {
265 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
266 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
267 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
268 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
269 }
270 }
271 }
272 return psa;
273 }
274
275 /* Free data items in an array */
276 static HRESULT SAFEARRAY_DestroyData(SAFEARRAY *psa, ULONG ulStartCell)
277 {
278 if (psa->pvData && !(psa->fFeatures & FADF_DATADELETED))
279 {
280 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
281
282 if (ulStartCell > ulCellCount) {
283 FIXME("unexpted ulcellcount %d, start %d\n",ulCellCount,ulStartCell);
284 return E_UNEXPECTED;
285 }
286
287 ulCellCount -= ulStartCell;
288
289 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
290 {
291 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)psa->pvData + ulStartCell;
292
293 while(ulCellCount--)
294 {
295 if (*lpUnknown)
296 IUnknown_Release(*lpUnknown);
297 lpUnknown++;
298 }
299 }
300 else if (psa->fFeatures & (FADF_RECORD))
301 {
302 IRecordInfo *lpRecInfo;
303
304 if (SUCCEEDED(SafeArrayGetRecordInfo(psa, &lpRecInfo)))
305 {
306 PBYTE pRecordData = psa->pvData;
307 while(ulCellCount--)
308 {
309 IRecordInfo_RecordClear(lpRecInfo, pRecordData);
310 pRecordData += psa->cbElements;
311 }
312 IRecordInfo_Release(lpRecInfo);
313 }
314 }
315 else if (psa->fFeatures & FADF_BSTR)
316 {
317 BSTR* lpBstr = (BSTR*)psa->pvData + ulStartCell;
318
319 while(ulCellCount--)
320 {
321 SysFreeString(*lpBstr);
322 lpBstr++;
323 }
324 }
325 else if (psa->fFeatures & FADF_VARIANT)
326 {
327 VARIANT* lpVariant = (VARIANT*)psa->pvData + ulStartCell;
328
329 while(ulCellCount--)
330 {
331 HRESULT hRet = VariantClear(lpVariant);
332
333 if (FAILED(hRet)) FIXME("VariantClear of element failed!\n");
334 lpVariant++;
335 }
336 }
337 }
338 return S_OK;
339 }
340
341 /* Copy data items from one array to another */
342 static HRESULT SAFEARRAY_CopyData(SAFEARRAY *psa, SAFEARRAY *dest)
343 {
344 if (!psa->pvData)
345 return S_OK;
346 else if (!dest->pvData || psa->fFeatures & FADF_DATADELETED)
347 return E_INVALIDARG;
348 else
349 {
350 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
351
352 dest->fFeatures = (dest->fFeatures & FADF_CREATEVECTOR) |
353 (psa->fFeatures & ~(FADF_CREATEVECTOR|FADF_DATADELETED));
354
355 if (psa->fFeatures & FADF_VARIANT)
356 {
357 VARIANT* lpVariant = psa->pvData;
358 VARIANT* lpDest = dest->pvData;
359
360 while(ulCellCount--)
361 {
362 HRESULT hRet;
363
364 hRet = VariantCopy(lpDest, lpVariant);
365 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
366 lpVariant++;
367 lpDest++;
368 }
369 }
370 else if (psa->fFeatures & FADF_BSTR)
371 {
372 BSTR* lpBstr = psa->pvData;
373 BSTR* lpDest = dest->pvData;
374
375 while(ulCellCount--)
376 {
377 if (*lpBstr)
378 {
379 *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
380 if (!*lpDest)
381 return E_OUTOFMEMORY;
382 }
383 else
384 *lpDest = NULL;
385 lpBstr++;
386 lpDest++;
387 }
388 }
389 else
390 {
391 /* Copy the data over */
392 memcpy(dest->pvData, psa->pvData, ulCellCount * psa->cbElements);
393
394 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
395 {
396 LPUNKNOWN *lpUnknown = dest->pvData;
397
398 while(ulCellCount--)
399 {
400 if (*lpUnknown)
401 IUnknown_AddRef(*lpUnknown);
402 lpUnknown++;
403 }
404 }
405 }
406
407 if (psa->fFeatures & FADF_RECORD)
408 {
409 IRecordInfo* pRecInfo = NULL;
410
411 SafeArrayGetRecordInfo(psa, &pRecInfo);
412 SafeArraySetRecordInfo(dest, pRecInfo);
413
414 if (pRecInfo)
415 {
416 /* Release because Get() adds a reference */
417 IRecordInfo_Release(pRecInfo);
418 }
419 }
420 else if (psa->fFeatures & FADF_HAVEIID)
421 {
422 GUID guid;
423 SafeArrayGetIID(psa, &guid);
424 SafeArraySetIID(dest, &guid);
425 }
426 else if (psa->fFeatures & FADF_HAVEVARTYPE)
427 {
428 SAFEARRAY_SetHiddenDWORD(dest, SAFEARRAY_GetHiddenDWORD(psa));
429 }
430 }
431 return S_OK;
432 }
433
434 /*************************************************************************
435 * SafeArrayAllocDescriptor (OLEAUT32.36)
436 *
437 * Allocate and initialise a descriptor for a SafeArray.
438 *
439 * PARAMS
440 * cDims [I] Number of dimensions of the array
441 * ppsaOut [O] Destination for new descriptor
442 *
443 * RETURNS
444 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
445 * Failure: An HRESULT error code indicating the error.
446 *
447 * NOTES
448 * See SafeArray.
449 */
450 HRESULT WINAPI SafeArrayAllocDescriptor(UINT cDims, SAFEARRAY **ppsaOut)
451 {
452 LONG allocSize;
453
454 TRACE("(%d,%p)\n", cDims, ppsaOut);
455
456 if (!cDims || cDims >= 0x10000) /* Maximum 65535 dimensions */
457 return E_INVALIDARG;
458
459 if (!ppsaOut)
460 return E_POINTER;
461
462 /* We need enough space for the header and its bounds */
463 allocSize = sizeof(SAFEARRAY) + sizeof(SAFEARRAYBOUND) * (cDims - 1);
464
465 if (FAILED(SAFEARRAY_AllocDescriptor(allocSize, ppsaOut)))
466 return E_UNEXPECTED;
467
468 (*ppsaOut)->cDims = cDims;
469
470 TRACE("(%d): %u bytes allocated for descriptor.\n", cDims, allocSize);
471 return S_OK;
472 }
473
474 /*************************************************************************
475 * SafeArrayAllocDescriptorEx (OLEAUT32.41)
476 *
477 * Allocate and initialise a descriptor for a SafeArray of a given type.
478 *
479 * PARAMS
480 * vt [I] The type of items to store in the array
481 * cDims [I] Number of dimensions of the array
482 * ppsaOut [O] Destination for new descriptor
483 *
484 * RETURNS
485 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
486 * Failure: An HRESULT error code indicating the error.
487 *
488 * NOTES
489 * - This function does not check that vt is an allowed VARTYPE.
490 * - Unlike SafeArrayAllocDescriptor(), vt is associated with the array.
491 * See SafeArray.
492 */
493 HRESULT WINAPI SafeArrayAllocDescriptorEx(VARTYPE vt, UINT cDims, SAFEARRAY **ppsaOut)
494 {
495 ULONG cbElements;
496 HRESULT hRet = E_UNEXPECTED;
497
498 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, ppsaOut);
499
500 cbElements = SAFEARRAY_GetVTSize(vt);
501 if (!cbElements)
502 WARN("Creating a descriptor with an invalid VARTYPE!\n");
503
504 hRet = SafeArrayAllocDescriptor(cDims, ppsaOut);
505
506 if (SUCCEEDED(hRet))
507 {
508 SAFEARRAY_SetFeatures(vt, *ppsaOut);
509 (*ppsaOut)->cbElements = cbElements;
510 }
511 return hRet;
512 }
513
514 /*************************************************************************
515 * SafeArrayAllocData (OLEAUT32.37)
516 *
517 * Allocate the data area of a SafeArray.
518 *
519 * PARAMS
520 * psa [I] SafeArray to allocate the data area of.
521 *
522 * RETURNS
523 * Success: S_OK. The data area is allocated and initialised.
524 * Failure: An HRESULT error code indicating the error.
525 *
526 * NOTES
527 * See SafeArray.
528 */
529 HRESULT WINAPI SafeArrayAllocData(SAFEARRAY *psa)
530 {
531 HRESULT hRet = E_INVALIDARG;
532
533 TRACE("(%p)\n", psa);
534
535 if (psa)
536 {
537 ULONG ulSize = SAFEARRAY_GetCellCount(psa);
538
539 psa->pvData = SAFEARRAY_Malloc(ulSize * psa->cbElements);
540
541 if (psa->pvData)
542 {
543 hRet = S_OK;
544 TRACE("%u bytes allocated for data at %p (%u objects).\n",
545 ulSize * psa->cbElements, psa->pvData, ulSize);
546 }
547 else
548 hRet = E_OUTOFMEMORY;
549 }
550 return hRet;
551 }
552
553 /*************************************************************************
554 * SafeArrayCreate (OLEAUT32.15)
555 *
556 * Create a new SafeArray.
557 *
558 * PARAMS
559 * vt [I] Type to store in the safe array
560 * cDims [I] Number of array dimensions
561 * rgsabound [I] Bounds of the array dimensions
562 *
563 * RETURNS
564 * Success: A pointer to a new array object.
565 * Failure: NULL, if any parameter is invalid or memory allocation fails.
566 *
567 * NOTES
568 * Win32 allows arrays with 0 sized dimensions. This bug is not reproduced
569 * in the Wine implementation.
570 * See SafeArray.
571 */
572 SAFEARRAY* WINAPI SafeArrayCreate(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound)
573 {
574 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound);
575
576 if (vt == VT_RECORD)
577 return NULL;
578
579 return SAFEARRAY_Create(vt, cDims, rgsabound, 0);
580 }
581
582 /*************************************************************************
583 * SafeArrayCreateEx (OLEAUT32.15)
584 *
585 * Create a new SafeArray.
586 *
587 * PARAMS
588 * vt [I] Type to store in the safe array
589 * cDims [I] Number of array dimensions
590 * rgsabound [I] Bounds of the array dimensions
591 * pvExtra [I] Extra data
592 *
593 * RETURNS
594 * Success: A pointer to a new array object.
595 * Failure: NULL, if any parameter is invalid or memory allocation fails.
596 *
597 * NOTES
598 * See SafeArray.
599 */
600 SAFEARRAY* WINAPI SafeArrayCreateEx(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound, LPVOID pvExtra)
601 {
602 ULONG ulSize = 0;
603 IRecordInfo* iRecInfo = pvExtra;
604 SAFEARRAY* psa;
605
606 TRACE("(%d->%s,%d,%p,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound, pvExtra);
607
608 if (vt == VT_RECORD)
609 {
610 if (!iRecInfo)
611 return NULL;
612 IRecordInfo_GetSize(iRecInfo, &ulSize);
613 }
614 psa = SAFEARRAY_Create(vt, cDims, rgsabound, ulSize);
615
616 if (pvExtra)
617 {
618 switch(vt)
619 {
620 case VT_RECORD:
621 SafeArraySetRecordInfo(psa, pvExtra);
622 break;
623 case VT_UNKNOWN:
624 case VT_DISPATCH:
625 SafeArraySetIID(psa, pvExtra);
626 break;
627 }
628 }
629 return psa;
630 }
631
632 /************************************************************************
633 * SafeArrayCreateVector (OLEAUT32.411)
634 *
635 * Create a one dimensional, contiguous SafeArray.
636 *
637 * PARAMS
638 * vt [I] Type to store in the safe array
639 * lLbound [I] Lower bound of the array
640 * cElements [I] Number of elements in the array
641 *
642 * RETURNS
643 * Success: A pointer to a new array object.
644 * Failure: NULL, if any parameter is invalid or memory allocation fails.
645 *
646 * NOTES
647 * See SafeArray.
648 */
649 SAFEARRAY* WINAPI SafeArrayCreateVector(VARTYPE vt, LONG lLbound, ULONG cElements)
650 {
651 TRACE("(%d->%s,%d,%d\n", vt, debugstr_vt(vt), lLbound, cElements);
652
653 if (vt == VT_RECORD)
654 return NULL;
655
656 return SAFEARRAY_CreateVector(vt, lLbound, cElements, SAFEARRAY_GetVTSize(vt));
657 }
658
659 /************************************************************************
660 * SafeArrayCreateVectorEx (OLEAUT32.411)
661 *
662 * Create a one dimensional, contiguous SafeArray.
663 *
664 * PARAMS
665 * vt [I] Type to store in the safe array
666 * lLbound [I] Lower bound of the array
667 * cElements [I] Number of elements in the array
668 * pvExtra [I] Extra data
669 *
670 * RETURNS
671 * Success: A pointer to a new array object.
672 * Failure: NULL, if any parameter is invalid or memory allocation fails.
673 *
674 * NOTES
675 * See SafeArray.
676 */
677 SAFEARRAY* WINAPI SafeArrayCreateVectorEx(VARTYPE vt, LONG lLbound, ULONG cElements, LPVOID pvExtra)
678 {
679 ULONG ulSize;
680 IRecordInfo* iRecInfo = pvExtra;
681 SAFEARRAY* psa;
682
683 TRACE("(%d->%s,%d,%d,%p\n", vt, debugstr_vt(vt), lLbound, cElements, pvExtra);
684
685 if (vt == VT_RECORD)
686 {
687 if (!iRecInfo)
688 return NULL;
689 IRecordInfo_GetSize(iRecInfo, &ulSize);
690 }
691 else
692 ulSize = SAFEARRAY_GetVTSize(vt);
693
694 psa = SAFEARRAY_CreateVector(vt, lLbound, cElements, ulSize);
695
696 if (pvExtra)
697 {
698 switch(vt)
699 {
700 case VT_RECORD:
701 SafeArraySetRecordInfo(psa, iRecInfo);
702 break;
703 case VT_UNKNOWN:
704 case VT_DISPATCH:
705 SafeArraySetIID(psa, pvExtra);
706 break;
707 }
708 }
709 return psa;
710 }
711
712 /*************************************************************************
713 * SafeArrayDestroyDescriptor (OLEAUT32.38)
714 *
715 * Destroy a SafeArray.
716 *
717 * PARAMS
718 * psa [I] SafeArray to destroy.
719 *
720 * RETURNS
721 * Success: S_OK. The resources used by the array are freed.
722 * Failure: An HRESULT error code indicating the error.
723 *
724 * NOTES
725 * See SafeArray.
726 */
727 HRESULT WINAPI SafeArrayDestroyDescriptor(SAFEARRAY *psa)
728 {
729 TRACE("(%p)\n", psa);
730
731 if (psa)
732 {
733 LPVOID lpv = (char*)psa - SAFEARRAY_HIDDEN_SIZE;
734
735 if (psa->cLocks)
736 return DISP_E_ARRAYISLOCKED; /* Can't destroy a locked array */
737
738 if (psa->fFeatures & FADF_RECORD)
739 SafeArraySetRecordInfo(psa, NULL);
740
741 if (psa->fFeatures & FADF_CREATEVECTOR &&
742 !(psa->fFeatures & FADF_DATADELETED))
743 SAFEARRAY_DestroyData(psa, 0); /* Data not previously deleted */
744
745 if (!SAFEARRAY_Free(lpv))
746 return E_UNEXPECTED;
747 }
748 return S_OK;
749 }
750
751 /*************************************************************************
752 * SafeArrayLock (OLEAUT32.21)
753 *
754 * Increment the lock counter of a SafeArray.
755 *
756 * PARAMS
757 * psa [O] SafeArray to lock
758 *
759 * RETURNS
760 * Success: S_OK. The array lock is incremented.
761 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
762 * are held on the array at once.
763 *
764 * NOTES
765 * In Win32 these locks are not thread safe.
766 * See SafeArray.
767 */
768 HRESULT WINAPI SafeArrayLock(SAFEARRAY *psa)
769 {
770 ULONG ulLocks;
771
772 TRACE("(%p)\n", psa);
773
774 if (!psa)
775 return E_INVALIDARG;
776
777 ulLocks = InterlockedIncrement( (LONG*) &psa->cLocks);
778
779 if (ulLocks > 0xffff) /* Maximum of 16384 locks at a time */
780 {
781 WARN("Out of locks!\n");
782 InterlockedDecrement( (LONG*) &psa->cLocks);
783 return E_UNEXPECTED;
784 }
785 return S_OK;
786 }
787
788 /*************************************************************************
789 * SafeArrayUnlock (OLEAUT32.22)
790 *
791 * Decrement the lock counter of a SafeArray.
792 *
793 * PARAMS
794 * psa [O] SafeArray to unlock
795 *
796 * RETURNS
797 * Success: S_OK. The array lock is decremented.
798 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
799 * held on the array.
800 *
801 * NOTES
802 * See SafeArray.
803 */
804 HRESULT WINAPI SafeArrayUnlock(SAFEARRAY *psa)
805 {
806 TRACE("(%p)\n", psa);
807
808 if (!psa)
809 return E_INVALIDARG;
810
811 if (InterlockedDecrement( (LONG*) &psa->cLocks) < 0)
812 {
813 WARN("Unlocked but no lock held!\n");
814 InterlockedIncrement( (LONG*) &psa->cLocks);
815 return E_UNEXPECTED;
816 }
817 return S_OK;
818 }
819
820 /*************************************************************************
821 * SafeArrayPutElement (OLEAUT32.26)
822 *
823 * Put an item into a SafeArray.
824 *
825 * PARAMS
826 * psa [I] SafeArray to insert into
827 * rgIndices [I] Indices to insert at
828 * pvData [I] Data to insert
829 *
830 * RETURNS
831 * Success: S_OK. The item is inserted
832 * Failure: An HRESULT error code indicating the error.
833 *
834 * NOTES
835 * See SafeArray.
836 */
837 HRESULT WINAPI SafeArrayPutElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
838 {
839 HRESULT hRet;
840
841 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
842
843 if (!psa || !rgIndices)
844 return E_INVALIDARG;
845
846 hRet = SafeArrayLock(psa);
847
848 if (SUCCEEDED(hRet))
849 {
850 PVOID lpvDest;
851
852 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvDest);
853
854 if (SUCCEEDED(hRet))
855 {
856 if (psa->fFeatures & FADF_VARIANT)
857 {
858 VARIANT* lpVariant = pvData;
859 VARIANT* lpDest = lpvDest;
860
861 hRet = VariantClear(lpDest);
862 if (FAILED(hRet)) FIXME("VariantClear failed with 0x%x\n", hRet);
863 hRet = VariantCopy(lpDest, lpVariant);
864 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
865 }
866 else if (psa->fFeatures & FADF_BSTR)
867 {
868 BSTR lpBstr = (BSTR)pvData;
869 BSTR* lpDest = lpvDest;
870
871 SysFreeString(*lpDest);
872
873 *lpDest = SysAllocStringByteLen((char*)lpBstr, SysStringByteLen(lpBstr));
874 if (!*lpDest)
875 hRet = E_OUTOFMEMORY;
876 }
877 else
878 {
879 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
880 {
881 LPUNKNOWN lpUnknown = pvData;
882 LPUNKNOWN *lpDest = lpvDest;
883
884 if (lpUnknown)
885 IUnknown_AddRef(lpUnknown);
886 if (*lpDest)
887 IUnknown_Release(*lpDest);
888 *lpDest = lpUnknown;
889 } else {
890 /* Copy the data over */
891 memcpy(lpvDest, pvData, psa->cbElements);
892 }
893 }
894 }
895 SafeArrayUnlock(psa);
896 }
897 return hRet;
898 }
899
900
901 /*************************************************************************
902 * SafeArrayGetElement (OLEAUT32.25)
903 *
904 * Get an item from a SafeArray.
905 *
906 * PARAMS
907 * psa [I] SafeArray to get from
908 * rgIndices [I] Indices to get from
909 * pvData [O] Destination for data
910 *
911 * RETURNS
912 * Success: S_OK. The item data is returned in pvData.
913 * Failure: An HRESULT error code indicating the error.
914 *
915 * NOTES
916 * See SafeArray.
917 */
918 HRESULT WINAPI SafeArrayGetElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
919 {
920 HRESULT hRet;
921
922 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
923
924 if (!psa || !rgIndices || !pvData)
925 return E_INVALIDARG;
926
927 hRet = SafeArrayLock(psa);
928
929 if (SUCCEEDED(hRet))
930 {
931 PVOID lpvSrc;
932
933 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvSrc);
934
935 if (SUCCEEDED(hRet))
936 {
937 if (psa->fFeatures & FADF_VARIANT)
938 {
939 VARIANT* lpVariant = lpvSrc;
940 VARIANT* lpDest = pvData;
941
942 /* The original content of pvData is ignored. */
943 V_VT(lpDest) = VT_EMPTY;
944 hRet = VariantCopy(lpDest, lpVariant);
945 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
946 }
947 else if (psa->fFeatures & FADF_BSTR)
948 {
949 BSTR* lpBstr = lpvSrc;
950 BSTR* lpDest = pvData;
951
952 if (*lpBstr)
953 {
954 *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
955 if (!*lpBstr)
956 hRet = E_OUTOFMEMORY;
957 }
958 else
959 *lpDest = NULL;
960 }
961 else
962 {
963 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
964 {
965 LPUNKNOWN *lpUnknown = lpvSrc;
966
967 if (*lpUnknown)
968 IUnknown_AddRef(*lpUnknown);
969 }
970 /* Copy the data over */
971 memcpy(pvData, lpvSrc, psa->cbElements);
972 }
973 }
974 SafeArrayUnlock(psa);
975 }
976 return hRet;
977 }
978
979 /*************************************************************************
980 * SafeArrayGetUBound (OLEAUT32.19)
981 *
982 * Get the upper bound for a given SafeArray dimension
983 *
984 * PARAMS
985 * psa [I] Array to get dimension upper bound from
986 * nDim [I] The dimension number to get the upper bound of
987 * plUbound [O] Destination for the upper bound
988 *
989 * RETURNS
990 * Success: S_OK. plUbound contains the dimensions upper bound.
991 * Failure: An HRESULT error code indicating the error.
992 *
993 * NOTES
994 * See SafeArray.
995 */
996 HRESULT WINAPI SafeArrayGetUBound(SAFEARRAY *psa, UINT nDim, LONG *plUbound)
997 {
998 TRACE("(%p,%d,%p)\n", psa, nDim, plUbound);
999
1000 if (!psa || !plUbound)
1001 return E_INVALIDARG;
1002
1003 if(!nDim || nDim > psa->cDims)
1004 return DISP_E_BADINDEX;
1005
1006 *plUbound = psa->rgsabound[psa->cDims - nDim].lLbound +
1007 psa->rgsabound[psa->cDims - nDim].cElements - 1;
1008
1009 return S_OK;
1010 }
1011
1012 /*************************************************************************
1013 * SafeArrayGetLBound (OLEAUT32.20)
1014 *
1015 * Get the lower bound for a given SafeArray dimension
1016 *
1017 * PARAMS
1018 * psa [I] Array to get dimension lower bound from
1019 * nDim [I] The dimension number to get the lower bound of
1020 * plLbound [O] Destination for the lower bound
1021 *
1022 * RETURNS
1023 * Success: S_OK. plUbound contains the dimensions lower bound.
1024 * Failure: An HRESULT error code indicating the error.
1025 *
1026 * NOTES
1027 * See SafeArray.
1028 */
1029 HRESULT WINAPI SafeArrayGetLBound(SAFEARRAY *psa, UINT nDim, LONG *plLbound)
1030 {
1031 TRACE("(%p,%d,%p)\n", psa, nDim, plLbound);
1032
1033 if (!psa || !plLbound)
1034 return E_INVALIDARG;
1035
1036 if(!nDim || nDim > psa->cDims)
1037 return DISP_E_BADINDEX;
1038
1039 *plLbound = psa->rgsabound[psa->cDims - nDim].lLbound;
1040 return S_OK;
1041 }
1042
1043 /*************************************************************************
1044 * SafeArrayGetDim (OLEAUT32.17)
1045 *
1046 * Get the number of dimensions in a SafeArray.
1047 *
1048 * PARAMS
1049 * psa [I] Array to get the dimensions of
1050 *
1051 * RETURNS
1052 * The number of array dimensions in psa, or 0 if psa is NULL.
1053 *
1054 * NOTES
1055 * See SafeArray.
1056 */
1057 UINT WINAPI SafeArrayGetDim(SAFEARRAY *psa)
1058 {
1059 TRACE("(%p) returning %d\n", psa, psa ? psa->cDims : 0u);
1060 return psa ? psa->cDims : 0;
1061 }
1062
1063 /*************************************************************************
1064 * SafeArrayGetElemsize (OLEAUT32.18)
1065 *
1066 * Get the size of an element in a SafeArray.
1067 *
1068 * PARAMS
1069 * psa [I] Array to get the element size from
1070 *
1071 * RETURNS
1072 * The size of a single element in psa, or 0 if psa is NULL.
1073 *
1074 * NOTES
1075 * See SafeArray.
1076 */
1077 UINT WINAPI SafeArrayGetElemsize(SAFEARRAY *psa)
1078 {
1079 TRACE("(%p) returning %d\n", psa, psa ? psa->cbElements : 0u);
1080 return psa ? psa->cbElements : 0;
1081 }
1082
1083 /*************************************************************************
1084 * SafeArrayAccessData (OLEAUT32.23)
1085 *
1086 * Lock a SafeArray and return a pointer to its data.
1087 *
1088 * PARAMS
1089 * psa [I] Array to get the data pointer from
1090 * ppvData [O] Destination for the arrays data pointer
1091 *
1092 * RETURNS
1093 * Success: S_OK. ppvData contains the arrays data pointer, and the array
1094 * is locked.
1095 * Failure: An HRESULT error code indicating the error.
1096 *
1097 * NOTES
1098 * See SafeArray.
1099 */
1100 HRESULT WINAPI SafeArrayAccessData(SAFEARRAY *psa, void **ppvData)
1101 {
1102 TRACE("(%p,%p)\n", psa, ppvData);
1103
1104 if(!psa || !ppvData)
1105 return E_INVALIDARG;
1106
1107 if (SUCCEEDED(SafeArrayLock(psa)))
1108 {
1109 *ppvData = psa->pvData;
1110 return S_OK;
1111 }
1112 *ppvData = NULL;
1113 return E_UNEXPECTED;
1114 }
1115
1116
1117 /*************************************************************************
1118 * SafeArrayUnaccessData (OLEAUT32.24)
1119 *
1120 * Unlock a SafeArray after accessing its data.
1121 *
1122 * PARAMS
1123 * psa [I] Array to unlock
1124 *
1125 * RETURNS
1126 * Success: S_OK. The array is unlocked.
1127 * Failure: An HRESULT error code indicating the error.
1128 *
1129 * NOTES
1130 * See SafeArray.
1131 */
1132 HRESULT WINAPI SafeArrayUnaccessData(SAFEARRAY *psa)
1133 {
1134 TRACE("(%p)\n", psa);
1135 return SafeArrayUnlock(psa);
1136 }
1137
1138 /************************************************************************
1139 * SafeArrayPtrOfIndex (OLEAUT32.148)
1140 *
1141 * Get the address of an item in a SafeArray.
1142 *
1143 * PARAMS
1144 * psa [I] Array to get the items address from
1145 * rgIndices [I] Index of the item in the array
1146 * ppvData [O] Destination for item address
1147 *
1148 * RETURNS
1149 * Success: S_OK. ppvData contains a pointer to the item.
1150 * Failure: An HRESULT error code indicating the error.
1151 *
1152 * NOTES
1153 * This function does not lock the array.
1154 *
1155 * NOTES
1156 * See SafeArray.
1157 */
1158 HRESULT WINAPI SafeArrayPtrOfIndex(SAFEARRAY *psa, LONG *rgIndices, void **ppvData)
1159 {
1160 USHORT dim;
1161 ULONG cell = 0, dimensionSize = 1;
1162 SAFEARRAYBOUND* psab;
1163 LONG c1;
1164
1165 TRACE("(%p,%p,%p)\n", psa, rgIndices, ppvData);
1166
1167 /* The general formula for locating the cell number of an entry in an n
1168 * dimensional array (where cn = coordinate in dimension dn) is:
1169 *
1170 * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1171 *
1172 * We calculate the size of the last dimension at each step through the
1173 * dimensions to avoid recursing to calculate the last dimensions size.
1174 */
1175 if (!psa || !rgIndices || !ppvData)
1176 return E_INVALIDARG;
1177
1178 psab = psa->rgsabound + psa->cDims - 1;
1179 c1 = *rgIndices++;
1180
1181 if (c1 < psab->lLbound || c1 >= psab->lLbound + (LONG)psab->cElements)
1182 return DISP_E_BADINDEX; /* Initial index out of bounds */
1183
1184 for (dim = 1; dim < psa->cDims; dim++)
1185 {
1186 dimensionSize *= psab->cElements;
1187
1188 psab--;
1189
1190 if (!psab->cElements ||
1191 *rgIndices < psab->lLbound ||
1192 *rgIndices >= psab->lLbound + (LONG)psab->cElements)
1193 return DISP_E_BADINDEX; /* Index out of bounds */
1194
1195 cell += (*rgIndices - psab->lLbound) * dimensionSize;
1196 rgIndices++;
1197 }
1198
1199 cell += (c1 - psa->rgsabound[psa->cDims - 1].lLbound);
1200
1201 *ppvData = (char*)psa->pvData + cell * psa->cbElements;
1202 return S_OK;
1203 }
1204
1205 /************************************************************************
1206 * SafeArrayDestroyData (OLEAUT32.39)
1207 *
1208 * Destroy the data associated with a SafeArray.
1209 *
1210 * PARAMS
1211 * psa [I] Array to delete the data from
1212 *
1213 * RETURNS
1214 * Success: S_OK. All items and the item data are freed.
1215 * Failure: An HRESULT error code indicating the error.
1216 *
1217 * NOTES
1218 * See SafeArray.
1219 */
1220 HRESULT WINAPI SafeArrayDestroyData(SAFEARRAY *psa)
1221 {
1222 TRACE("(%p)\n", psa);
1223
1224 if (!psa)
1225 return E_INVALIDARG;
1226
1227 if (psa->cLocks)
1228 return DISP_E_ARRAYISLOCKED; /* Can't delete a locked array */
1229
1230 /* Delete the actual item data */
1231 if (FAILED(SAFEARRAY_DestroyData(psa, 0)))
1232 return E_UNEXPECTED;
1233
1234 if (psa->pvData)
1235 {
1236 if (psa->fFeatures & FADF_STATIC)
1237 {
1238 ZeroMemory(psa->pvData, SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1239 return S_OK;
1240 }
1241 /* If this is not a vector, free the data memory block */
1242 if (!(psa->fFeatures & FADF_CREATEVECTOR))
1243 {
1244 if (!SAFEARRAY_Free(psa->pvData))
1245 return E_UNEXPECTED;
1246 psa->pvData = NULL;
1247 }
1248 else
1249 psa->fFeatures |= FADF_DATADELETED; /* Mark the data deleted */
1250
1251 }
1252 return S_OK;
1253 }
1254
1255 /************************************************************************
1256 * SafeArrayCopyData (OLEAUT32.412)
1257 *
1258 * Copy all data from one SafeArray to another.
1259 *
1260 * PARAMS
1261 * psaSource [I] Source for copy
1262 * psaTarget [O] Destination for copy
1263 *
1264 * RETURNS
1265 * Success: S_OK. psaTarget contains a copy of psaSource.
1266 * Failure: An HRESULT error code indicating the error.
1267 *
1268 * NOTES
1269 * The two arrays must have the same number of dimensions and elements.
1270 *
1271 * NOTES
1272 * See SafeArray.
1273 */
1274 HRESULT WINAPI SafeArrayCopyData(SAFEARRAY *psaSource, SAFEARRAY *psaTarget)
1275 {
1276 int dim;
1277
1278 TRACE("(%p,%p)\n", psaSource, psaTarget);
1279
1280 if (!psaSource || !psaTarget ||
1281 psaSource->cDims != psaTarget->cDims ||
1282 psaSource->cbElements != psaTarget->cbElements)
1283 return E_INVALIDARG;
1284
1285 /* Each dimension must be the same size */
1286 for (dim = psaSource->cDims - 1; dim >= 0 ; dim--)
1287 if (psaSource->rgsabound[dim].cElements !=
1288 psaTarget->rgsabound[dim].cElements)
1289 return E_INVALIDARG;
1290
1291 if (SUCCEEDED(SAFEARRAY_DestroyData(psaTarget, 0)) &&
1292 SUCCEEDED(SAFEARRAY_CopyData(psaSource, psaTarget)))
1293 return S_OK;
1294 return E_UNEXPECTED;
1295 }
1296
1297 /************************************************************************
1298 * SafeArrayDestroy (OLEAUT32.16)
1299 *
1300 * Destroy a SafeArray.
1301 *
1302 * PARAMS
1303 * psa [I] Array to destroy
1304 *
1305 * RETURNS
1306 * Success: S_OK. All resources used by the array are freed.
1307 * Failure: An HRESULT error code indicating the error.
1308 *
1309 * NOTES
1310 * See SafeArray.
1311 */
1312 HRESULT WINAPI SafeArrayDestroy(SAFEARRAY *psa)
1313 {
1314 TRACE("(%p)\n", psa);
1315
1316 if(!psa)
1317 return S_OK;
1318
1319 if(psa->cLocks > 0)
1320 return DISP_E_ARRAYISLOCKED;
1321
1322 /* Native doesn't check to see if the free succeeds */
1323 SafeArrayDestroyData(psa);
1324 SafeArrayDestroyDescriptor(psa);
1325 return S_OK;
1326 }
1327
1328 /************************************************************************
1329 * SafeArrayCopy (OLEAUT32.27)
1330 *
1331 * Make a duplicate of a SafeArray.
1332 *
1333 * PARAMS
1334 * psa [I] Source for copy
1335 * ppsaOut [O] Destination for new copy
1336 *
1337 * RETURNS
1338 * Success: S_OK. ppsaOut contains a copy of the array.
1339 * Failure: An HRESULT error code indicating the error.
1340 *
1341 * NOTES
1342 * See SafeArray.
1343 */
1344 HRESULT WINAPI SafeArrayCopy(SAFEARRAY *psa, SAFEARRAY **ppsaOut)
1345 {
1346 HRESULT hRet;
1347
1348 TRACE("(%p,%p)\n", psa, ppsaOut);
1349
1350 if (!ppsaOut)
1351 return E_INVALIDARG;
1352
1353 *ppsaOut = NULL;
1354
1355 if (!psa)
1356 return S_OK; /* Handles copying of NULL arrays */
1357
1358 if (!psa->cbElements)
1359 return E_INVALIDARG;
1360
1361 if (psa->fFeatures & (FADF_RECORD|FADF_HAVEIID|FADF_HAVEVARTYPE))
1362 {
1363 VARTYPE vt;
1364 if (FAILED(SafeArrayGetVartype(psa, &vt)))
1365 hRet = E_UNEXPECTED;
1366 else
1367 hRet = SafeArrayAllocDescriptorEx(vt, psa->cDims, ppsaOut);
1368 }
1369 else
1370 {
1371 hRet = SafeArrayAllocDescriptor(psa->cDims, ppsaOut);
1372 if (SUCCEEDED(hRet))
1373 {
1374 (*ppsaOut)->fFeatures = psa->fFeatures & ~FADF_CREATEVECTOR;
1375 (*ppsaOut)->cbElements = psa->cbElements;
1376 }
1377 }
1378
1379 if (SUCCEEDED(hRet))
1380 {
1381 /* Copy dimension bounds */
1382 memcpy((*ppsaOut)->rgsabound, psa->rgsabound, psa->cDims * sizeof(SAFEARRAYBOUND));
1383
1384 (*ppsaOut)->pvData = SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1385
1386 if ((*ppsaOut)->pvData)
1387 {
1388 hRet = SAFEARRAY_CopyData(psa, *ppsaOut);
1389
1390 if (SUCCEEDED(hRet))
1391 return hRet;
1392
1393 SAFEARRAY_Free((*ppsaOut)->pvData);
1394 }
1395 SafeArrayDestroyDescriptor(*ppsaOut);
1396 }
1397 *ppsaOut = NULL;
1398 return hRet;
1399 }
1400
1401 /************************************************************************
1402 * SafeArrayRedim (OLEAUT32.40)
1403 *
1404 * Changes the characteristics of the last dimension of a SafeArray
1405 *
1406 * PARAMS
1407 * psa [I] Array to change
1408 * psabound [I] New bound details for the last dimension
1409 *
1410 * RETURNS
1411 * Success: S_OK. psa is updated to reflect the new bounds.
1412 * Failure: An HRESULT error code indicating the error.
1413 *
1414 * NOTES
1415 * See SafeArray.
1416 */
1417 HRESULT WINAPI SafeArrayRedim(SAFEARRAY *psa, SAFEARRAYBOUND *psabound)
1418 {
1419 SAFEARRAYBOUND *oldBounds;
1420
1421 TRACE("(%p,%p)\n", psa, psabound);
1422
1423 if (!psa || psa->fFeatures & FADF_FIXEDSIZE || !psabound)
1424 return E_INVALIDARG;
1425
1426 if (psa->cLocks > 0)
1427 return DISP_E_ARRAYISLOCKED;
1428
1429 if (FAILED(SafeArrayLock(psa)))
1430 return E_UNEXPECTED;
1431
1432 oldBounds = psa->rgsabound;
1433 oldBounds->lLbound = psabound->lLbound;
1434
1435 if (psabound->cElements != oldBounds->cElements)
1436 {
1437 if (psabound->cElements < oldBounds->cElements)
1438 {
1439 /* Shorten the final dimension. */
1440 ULONG ulStartCell = psabound->cElements *
1441 (SAFEARRAY_GetCellCount(psa) / oldBounds->cElements);
1442 SAFEARRAY_DestroyData(psa, ulStartCell);
1443 }
1444 else
1445 {
1446 /* Lengthen the final dimension */
1447 ULONG ulOldSize, ulNewSize;
1448 PVOID pvNewData;
1449
1450 ulOldSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1451 if (ulOldSize)
1452 ulNewSize = (ulOldSize / oldBounds->cElements) * psabound->cElements;
1453 else {
1454 int oldelems = oldBounds->cElements;
1455 oldBounds->cElements = psabound->cElements;
1456 ulNewSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1457 oldBounds->cElements = oldelems;
1458 }
1459
1460 if (!(pvNewData = SAFEARRAY_Malloc(ulNewSize)))
1461 {
1462 SafeArrayUnlock(psa);
1463 return E_UNEXPECTED;
1464 }
1465
1466 memcpy(pvNewData, psa->pvData, ulOldSize);
1467 SAFEARRAY_Free(psa->pvData);
1468 psa->pvData = pvNewData;
1469 }
1470 oldBounds->cElements = psabound->cElements;
1471 }
1472
1473 SafeArrayUnlock(psa);
1474 return S_OK;
1475 }
1476
1477 /************************************************************************
1478 * SafeArrayGetVartype (OLEAUT32.77)
1479 *
1480 * Get the type of the items in a SafeArray.
1481 *
1482 * PARAMS
1483 * psa [I] Array to get the type from
1484 * pvt [O] Destination for the type
1485 *
1486 * RETURNS
1487 * Success: S_OK. pvt contains the type of the items.
1488 * Failure: An HRESULT error code indicating the error.
1489 *
1490 * NOTES
1491 * See SafeArray.
1492 */
1493 HRESULT WINAPI SafeArrayGetVartype(SAFEARRAY* psa, VARTYPE* pvt)
1494 {
1495 TRACE("(%p,%p)\n", psa, pvt);
1496
1497 if (!psa || !pvt)
1498 return E_INVALIDARG;
1499
1500 if (psa->fFeatures & FADF_RECORD)
1501 *pvt = VT_RECORD;
1502 else if ((psa->fFeatures & (FADF_HAVEIID|FADF_DISPATCH)) == (FADF_HAVEIID|FADF_DISPATCH))
1503 *pvt = VT_DISPATCH;
1504 else if (psa->fFeatures & FADF_HAVEIID)
1505 *pvt = VT_UNKNOWN;
1506 else if (psa->fFeatures & FADF_HAVEVARTYPE)
1507 {
1508 VARTYPE vt = SAFEARRAY_GetHiddenDWORD(psa);
1509 *pvt = vt;
1510 }
1511 else
1512 return E_INVALIDARG;
1513
1514 return S_OK;
1515 }
1516
1517 /************************************************************************
1518 * SafeArraySetRecordInfo (OLEAUT32.@)
1519 *
1520 * Set the record info for a SafeArray.
1521 *
1522 * PARAMS
1523 * psa [I] Array to set the record info for
1524 * pRinfo [I] Record info
1525 *
1526 * RETURNS
1527 * Success: S_OK. The record info is stored with the array.
1528 * Failure: An HRESULT error code indicating the error.
1529 *
1530 * NOTES
1531 * See SafeArray.
1532 */
1533 HRESULT WINAPI SafeArraySetRecordInfo(SAFEARRAY *psa, IRecordInfo *pRinfo)
1534 {
1535 IRecordInfo** dest = (IRecordInfo**)psa;
1536
1537 TRACE("(%p,%p)\n", psa, pRinfo);
1538
1539 if (!psa || !(psa->fFeatures & FADF_RECORD))
1540 return E_INVALIDARG;
1541
1542 if (pRinfo)
1543 IRecordInfo_AddRef(pRinfo);
1544
1545 if (dest[-1])
1546 IRecordInfo_Release(dest[-1]);
1547
1548 dest[-1] = pRinfo;
1549 return S_OK;
1550 }
1551
1552 /************************************************************************
1553 * SafeArrayGetRecordInfo (OLEAUT32.@)
1554 *
1555 * Get the record info from a SafeArray.
1556 *
1557 * PARAMS
1558 * psa [I] Array to get the record info from
1559 * pRinfo [O] Destination for the record info
1560 *
1561 * RETURNS
1562 * Success: S_OK. pRinfo contains the record info, or NULL if there was none.
1563 * Failure: An HRESULT error code indicating the error.
1564 *
1565 * NOTES
1566 * See SafeArray.
1567 */
1568 HRESULT WINAPI SafeArrayGetRecordInfo(SAFEARRAY *psa, IRecordInfo **pRinfo)
1569 {
1570 IRecordInfo** src = (IRecordInfo**)psa;
1571
1572 TRACE("(%p,%p)\n", psa, pRinfo);
1573
1574 if (!psa || !pRinfo || !(psa->fFeatures & FADF_RECORD))
1575 return E_INVALIDARG;
1576
1577 *pRinfo = src[-1];
1578
1579 if (*pRinfo)
1580 IRecordInfo_AddRef(*pRinfo);
1581 return S_OK;
1582 }
1583
1584 /************************************************************************
1585 * SafeArraySetIID (OLEAUT32.@)
1586 *
1587 * Set the IID for a SafeArray.
1588 *
1589 * PARAMS
1590 * psa [I] Array to set the IID from
1591 * guid [I] IID
1592 *
1593 * RETURNS
1594 * Success: S_OK. The IID is stored with the array
1595 * Failure: An HRESULT error code indicating the error.
1596 *
1597 * NOTES
1598 * See SafeArray.
1599 */
1600 HRESULT WINAPI SafeArraySetIID(SAFEARRAY *psa, REFGUID guid)
1601 {
1602 GUID* dest = (GUID*)psa;
1603
1604 TRACE("(%p,%s)\n", psa, debugstr_guid(guid));
1605
1606 if (!psa || !guid || !(psa->fFeatures & FADF_HAVEIID))
1607 return E_INVALIDARG;
1608
1609 dest[-1] = *guid;
1610 return S_OK;
1611 }
1612
1613 /************************************************************************
1614 * SafeArrayGetIID (OLEAUT32.@)
1615 *
1616 * Get the IID from a SafeArray.
1617 *
1618 * PARAMS
1619 * psa [I] Array to get the ID from
1620 * pGuid [O] Destination for the IID
1621 *
1622 * RETURNS
1623 * Success: S_OK. pRinfo contains the IID, or NULL if there was none.
1624 * Failure: An HRESULT error code indicating the error.
1625 *
1626 * NOTES
1627 * See SafeArray.
1628 */
1629 HRESULT WINAPI SafeArrayGetIID(SAFEARRAY *psa, GUID *pGuid)
1630 {
1631 GUID* src = (GUID*)psa;
1632
1633 TRACE("(%p,%p)\n", psa, pGuid);
1634
1635 if (!psa || !pGuid || !(psa->fFeatures & FADF_HAVEIID))
1636 return E_INVALIDARG;
1637
1638 *pGuid = src[-1];
1639 return S_OK;
1640 }
1641
1642 /************************************************************************
1643 * VectorFromBstr (OLEAUT32.@)
1644 *
1645 * Create a SafeArray Vector from the bytes of a BSTR.
1646 *
1647 * PARAMS
1648 * bstr [I] String to get bytes from
1649 * ppsa [O] Destination for the array
1650 *
1651 * RETURNS
1652 * Success: S_OK. ppsa contains the strings bytes as a VT_UI1 array.
1653 * Failure: An HRESULT error code indicating the error.
1654 *
1655 * NOTES
1656 * See SafeArray.
1657 */
1658 HRESULT WINAPI VectorFromBstr(BSTR bstr, SAFEARRAY **ppsa)
1659 {
1660 SAFEARRAYBOUND sab;
1661
1662 TRACE("(%p,%p)\n", bstr, ppsa);
1663
1664 if (!ppsa)
1665 return E_INVALIDARG;
1666
1667 sab.lLbound = 0;
1668 sab.cElements = SysStringByteLen(bstr);
1669
1670 *ppsa = SAFEARRAY_Create(VT_UI1, 1, &sab, 0);
1671
1672 if (*ppsa)
1673 {
1674 memcpy((*ppsa)->pvData, bstr, sab.cElements);
1675 return S_OK;
1676 }
1677 return E_OUTOFMEMORY;
1678 }
1679
1680 /************************************************************************
1681 * BstrFromVector (OLEAUT32.@)
1682 *
1683 * Create a BSTR from a SafeArray.
1684 *
1685 * PARAMS
1686 * psa [I] Source array
1687 * pbstr [O] Destination for output BSTR
1688 *
1689 * RETURNS
1690 * Success: S_OK. pbstr contains the arrays data.
1691 * Failure: An HRESULT error code indicating the error.
1692 *
1693 * NOTES
1694 * psa must be a 1 dimensional array of a 1 byte type.
1695 *
1696 * NOTES
1697 * See SafeArray.
1698 */
1699 HRESULT WINAPI BstrFromVector(SAFEARRAY *psa, BSTR *pbstr)
1700 {
1701 TRACE("(%p,%p)\n", psa, pbstr);
1702
1703 if (!pbstr)
1704 return E_INVALIDARG;
1705
1706 *pbstr = NULL;
1707
1708 if (!psa || psa->cbElements != 1 || psa->cDims != 1)
1709 return E_INVALIDARG;
1710
1711 *pbstr = SysAllocStringByteLen(psa->pvData, psa->rgsabound[0].cElements);
1712 if (!*pbstr)
1713 return E_OUTOFMEMORY;
1714 return S_OK;
1715 }
1716
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.