2 * SafeArray test program
4 * Copyright 2002 Marcus Meissner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/test.h"
41 static HMODULE hOleaut32;
43 static HRESULT (WINAPI *pSafeArrayAllocDescriptorEx)(VARTYPE,UINT,SAFEARRAY**);
44 static HRESULT (WINAPI *pSafeArrayCopyData)(SAFEARRAY*,SAFEARRAY*);
45 static HRESULT (WINAPI *pSafeArrayGetIID)(SAFEARRAY*,GUID*);
46 static HRESULT (WINAPI *pSafeArraySetIID)(SAFEARRAY*,REFGUID);
47 static HRESULT (WINAPI *pSafeArrayGetVartype)(SAFEARRAY*,VARTYPE*);
48 static HRESULT (WINAPI *pSafeArrayGetRecordInfo)(SAFEARRAY*,IRecordInfo**);
49 static SAFEARRAY* (WINAPI *pSafeArrayCreateEx)(VARTYPE,UINT,SAFEARRAYBOUND*,LPVOID);
50 static SAFEARRAY* (WINAPI *pSafeArrayCreateVector)(VARTYPE,LONG,ULONG);
52 #define GETPTR(func) p##func = (void*)GetProcAddress(hOleaut32, #func)
54 /* Is a given function exported from oleaut32? */
55 #define HAVE_FUNC(func) ((void*)GetProcAddress(hOleaut32, #func) != NULL)
57 /* Have IRecordInfo data type? */
58 #define HAVE_OLEAUT32_RECORD HAVE_FUNC(SafeArraySetRecordInfo)
59 /* Have R8 data type? */
60 #define HAVE_OLEAUT32_R8 HAVE_FUNC(VarR8FromI1)
61 /* Have I8/UI8 data type? */
62 #define HAVE_OLEAUT32_I8 HAVE_FUNC(VarI8FromI1)
63 /* Have the decimal type? */
64 #define HAVE_OLEAUT32_DECIMAL HAVE_FUNC(VarDecAdd)
65 /* Have INT_PTR/UINT_PTR type? */
66 static BOOL HAVE_OLEAUT32_INT_PTR;
68 /* very old version? */
69 #define IS_ANCIENT (!HAVE_FUNC(VarI1FromI2))
71 #define START_REF_COUNT 1
72 #define RECORD_SIZE 64
73 #define RECORD_SIZE_FAIL 17
74 /************************************************************************
75 * Dummy IRecordInfo Implementation
77 typedef struct IRecordInfoImpl
79 const IRecordInfoVtbl *lpvtbl;
85 static const IRecordInfoVtbl IRecordInfoImpl_VTable;
87 static IRecordInfoImpl *IRecordInfoImpl_Construct(void)
91 rec = HeapAlloc(GetProcessHeap(), 0, sizeof(IRecordInfoImpl));
92 rec->lpvtbl = &IRecordInfoImpl_VTable;
93 rec->ref = START_REF_COUNT;
99 static ULONG CALLBACK IRecordInfoImpl_AddRef(IRecordInfo *iface)
101 IRecordInfoImpl* This=(IRecordInfoImpl*)iface;
102 return InterlockedIncrement(&This->ref);
105 static ULONG CALLBACK IRecordInfoImpl_Release(IRecordInfo *iface)
107 IRecordInfoImpl* This=(IRecordInfoImpl*)iface;
108 return InterlockedDecrement(&This->ref);
111 static BOOL fail_GetSize; /* Whether to fail the GetSize call */
113 static HRESULT CALLBACK IRecordInfoImpl_RecordClear(IRecordInfo *iface, PVOID pvExisting)
115 IRecordInfoImpl* This=(IRecordInfoImpl*)iface;
120 static HRESULT CALLBACK IRecordInfoImpl_GetSize(IRecordInfo *iface, ULONG* size)
122 IRecordInfoImpl* This=(IRecordInfoImpl*)iface;
131 static HRESULT CALLBACK IRecordInfoImpl_Dummy(IRecordInfo *iface)
133 trace("Called an unexpected IRecordInfo method - please report!\n");
134 /* Quit because we'll just crash anyway */
139 static const IRecordInfoVtbl IRecordInfoImpl_VTable =
141 (PVOID)IRecordInfoImpl_Dummy,
142 IRecordInfoImpl_AddRef,
143 IRecordInfoImpl_Release,
144 (PVOID)IRecordInfoImpl_Dummy,
145 IRecordInfoImpl_RecordClear,
146 (PVOID)IRecordInfoImpl_Dummy,
147 (PVOID)IRecordInfoImpl_Dummy,
148 (PVOID)IRecordInfoImpl_Dummy,
149 IRecordInfoImpl_GetSize,
150 (PVOID)IRecordInfoImpl_Dummy,
151 (PVOID)IRecordInfoImpl_Dummy,
152 (PVOID)IRecordInfoImpl_Dummy,
153 (PVOID)IRecordInfoImpl_Dummy,
154 (PVOID)IRecordInfoImpl_Dummy,
155 (PVOID)IRecordInfoImpl_Dummy,
156 (PVOID)IRecordInfoImpl_Dummy,
157 (PVOID)IRecordInfoImpl_Dummy,
158 (PVOID)IRecordInfoImpl_Dummy,
159 (PVOID)IRecordInfoImpl_Dummy
162 static DWORD SAFEARRAY_GetVTSize(VARTYPE vt)
167 case VT_UI1: return sizeof(BYTE);
170 case VT_UI2: return sizeof(SHORT);
174 case VT_ERROR: return sizeof(LONG);
176 if (HAVE_OLEAUT32_R8)
177 return sizeof(LONG64);
180 if (HAVE_OLEAUT32_I8)
181 return sizeof(LONG64);
184 case VT_UINT: return sizeof(INT);
187 if (HAVE_OLEAUT32_INT_PTR)
188 return sizeof(UINT_PTR);
190 case VT_CY: return sizeof(CY);
191 case VT_DATE: return sizeof(DATE);
192 case VT_BSTR: return sizeof(BSTR);
193 case VT_DISPATCH: return sizeof(LPDISPATCH);
194 case VT_VARIANT: return sizeof(VARIANT);
195 case VT_UNKNOWN: return sizeof(LPUNKNOWN);
197 if (HAVE_OLEAUT32_DECIMAL)
198 return sizeof(DECIMAL);
204 static void check_for_VT_INT_PTR(void)
206 /* Set a global flag if VT_INT_PTR is supported */
209 SAFEARRAYBOUND bound;
212 a = SafeArrayCreate(VT_INT_PTR, 1, &bound);
214 trace("VT_INT_PTR is supported\n");
215 HAVE_OLEAUT32_INT_PTR = TRUE;
219 trace("VT_INT_PTR is not supported\n");
220 HAVE_OLEAUT32_INT_PTR = FALSE;
224 #define VARTYPE_NOT_SUPPORTED 0
227 UINT elemsize; /* elementsize by VT */
228 UINT expflags; /* fFeatures from SafeArrayAllocDescriptorEx */
229 UINT addflags; /* additional fFeatures from SafeArrayCreate */
231 {VT_EMPTY, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
232 {VT_NULL, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
233 {VT_I2, 2, FADF_HAVEVARTYPE,0},
234 {VT_I4, 4, FADF_HAVEVARTYPE,0},
235 {VT_R4, 4, FADF_HAVEVARTYPE,0},
236 {VT_R8, 8, FADF_HAVEVARTYPE,0},
237 {VT_CY, 8, FADF_HAVEVARTYPE,0},
238 {VT_DATE, 8, FADF_HAVEVARTYPE,0},
239 {VT_BSTR, sizeof(BSTR), FADF_HAVEVARTYPE,FADF_BSTR},
240 {VT_DISPATCH, sizeof(LPDISPATCH), FADF_HAVEIID, FADF_DISPATCH},
241 {VT_ERROR, 4, FADF_HAVEVARTYPE,0},
242 {VT_BOOL, 2, FADF_HAVEVARTYPE,0},
243 {VT_VARIANT, sizeof(VARIANT), FADF_HAVEVARTYPE,FADF_VARIANT},
244 {VT_UNKNOWN, sizeof(LPUNKNOWN), FADF_HAVEIID, FADF_UNKNOWN},
245 {VT_DECIMAL, sizeof(DECIMAL), FADF_HAVEVARTYPE,0},
246 {15, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0}, /* no VT_xxx */
247 {VT_I1, 1, FADF_HAVEVARTYPE,0},
248 {VT_UI1, 1, FADF_HAVEVARTYPE,0},
249 {VT_UI2, 2, FADF_HAVEVARTYPE,0},
250 {VT_UI4, 4, FADF_HAVEVARTYPE,0},
251 {VT_I8, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
252 {VT_UI8, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
253 {VT_INT, sizeof(INT), FADF_HAVEVARTYPE,0},
254 {VT_UINT, sizeof(UINT), FADF_HAVEVARTYPE,0},
255 {VT_VOID, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
256 {VT_HRESULT, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
257 {VT_PTR, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
258 {VT_SAFEARRAY,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
259 {VT_CARRAY, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
260 {VT_USERDEFINED,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
261 {VT_LPSTR, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
262 {VT_LPWSTR, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
263 {VT_FILETIME, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
264 {VT_RECORD, VARTYPE_NOT_SUPPORTED,FADF_RECORD,0},
265 {VT_BLOB, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
266 {VT_STREAM, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
267 {VT_STORAGE, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
268 {VT_STREAMED_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
269 {VT_STORED_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
270 {VT_BLOB_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
271 {VT_CF, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
272 {VT_CLSID, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
275 static void test_safearray(void)
278 unsigned int i, diff;
281 SAFEARRAYBOUND bound, bounds[2];
287 unsigned char *ptr1, *ptr2;
289 hres = SafeArrayDestroy( NULL);
290 ok( hres == S_OK, "SafeArrayDestroy( NULL) returned 0x%x\n", hres);
294 a = SafeArrayCreate(-1, 1, &bound);
295 ok(NULL == a,"SAC(-1,1,[1,0]) not failed?\n");
299 a = SafeArrayCreate(VT_I4, 1, &bound);
300 ok(NULL != a,"SAC(VT_I4,1,[0,0]) failed.\n");
302 hres = SafeArrayGetLBound(a, 1, &l);
303 ok(hres == S_OK, "SAGLB of 0 size dimensioned array failed with %x\n",hres);
304 ok(l == 42, "SAGLB of 0 size dimensioned array failed to return 42, but returned %d\n",l);
305 hres = SafeArrayGetUBound(a, 1, &l);
306 ok(hres == S_OK, "SAGUB of 0 size dimensioned array failed with %x\n",hres);
307 ok(l == 41, "SAGUB of 0 size dimensioned array failed to return 41, but returned %d\n",l);
309 hres = SafeArrayAccessData(a, &data);
310 ok(hres == S_OK, "SafeArrayAccessData of 0 size dimensioned array failed with %x\n", hres);
311 SafeArrayUnaccessData(a);
314 hres = SafeArrayRedim(a, &bound);
315 ok(hres == S_OK,"SAR of a 0 elements dimension failed with hres %x\n", hres);
317 hres = SafeArrayRedim(a, &bound);
318 ok(hres == S_OK || hres == E_OUTOFMEMORY,
319 "SAR to a 0 elements dimension failed with hres %x\n", hres);
320 hres = SafeArrayDestroy(a);
321 ok(hres == S_OK,"SAD of 0 dim array faild with hres %x\n", hres);
323 SafeArrayAllocDescriptor(2, &a);
324 a->rgsabound[0].cElements = 2;
325 a->rgsabound[0].lLbound = 1;
326 a->rgsabound[1].cElements = 4;
327 a->rgsabound[1].lLbound = 1;
329 SafeArrayAllocData(a);
333 hres = SafeArrayPtrOfIndex(a, indices, (void **)&ptr1);
334 ok(hres == S_OK, "SAPOI failed with hres %x\n", hres);
335 SafeArrayAccessData(a, (void **)&ptr2);
336 ok(ptr1 - ptr2 == 14, "SAPOI got wrong ptr\n");
337 *(WORD *)ptr1 = 0x55aa;
338 SafeArrayUnaccessData(a);
340 bound.cElements = 10;
342 SafeArrayRedim(a, &bound);
344 SafeArrayPtrOfIndex(a, indices, (void **)&ptr1);
345 ok(*(WORD *)ptr1 == 0x55aa, "Data not preserved when resizing array\n");
347 bound.cElements = 10;
349 SafeArrayRedim(a, &bound);
350 SafeArrayPtrOfIndex(a, indices, (void **)&ptr1);
351 ok(*(WORD *)ptr1 == 0, "Expanded area not zero-initialized\n");
354 SafeArrayPtrOfIndex(a, indices, (void **)&ptr1);
355 ok(*(WORD *)ptr1 == 0x55aa, "Data not preserved when resizing array\n");
357 hres = SafeArrayDestroy(a);
358 ok(hres == S_OK,"SAD faild with hres %x\n", hres);
360 bounds[0].cElements = 0; bounds[0].lLbound = 1;
361 bounds[1].cElements = 2; bounds[1].lLbound = 23;
362 a = SafeArrayCreate(VT_I4,2,bounds);
363 ok(a != NULL,"SAC(VT_INT32,2,...) with 0 element dim failed.\n");
365 hres = SafeArrayDestroy(a);
366 ok(hres == S_OK,"SAD faild with hres %x\n", hres);
367 bounds[0].cElements = 1; bounds[0].lLbound = 1;
368 bounds[1].cElements = 0; bounds[1].lLbound = 23;
369 a = SafeArrayCreate(VT_I4,2,bounds);
370 ok(a != NULL,"SAC(VT_INT32,2,...) with 0 element dim failed.\n");
372 hres = SafeArrayDestroy(a);
373 ok(hres == S_OK,"SAD faild with hres %x\n", hres);
375 bounds[0].cElements = 42; bounds[0].lLbound = 1;
376 bounds[1].cElements = 2; bounds[1].lLbound = 23;
377 a = SafeArrayCreate(VT_I4,2,bounds);
378 ok(a != NULL,"SAC(VT_INT32,2,...) failed.\n");
380 hres = SafeArrayGetLBound (a, 0, &l);
381 ok (hres == DISP_E_BADINDEX, "SAGLB 0 failed with %x\n", hres);
382 hres = SafeArrayGetLBound (a, 1, &l);
383 ok (hres == S_OK, "SAGLB 1 failed with %x\n", hres);
384 ok (l == 1, "SAGLB 1 returned %d instead of 1\n", l);
385 hres = SafeArrayGetLBound (a, 2, &l);
386 ok (hres == S_OK, "SAGLB 2 failed with %x\n", hres);
387 ok (l == 23, "SAGLB 2 returned %d instead of 23\n", l);
388 hres = SafeArrayGetLBound (a, 3, &l);
389 ok (hres == DISP_E_BADINDEX, "SAGLB 3 failed with %x\n", hres);
391 hres = SafeArrayGetUBound (a, 0, &l);
392 ok (hres == DISP_E_BADINDEX, "SAGUB 0 failed with %x\n", hres);
393 hres = SafeArrayGetUBound (a, 1, &l);
394 ok (hres == S_OK, "SAGUB 1 failed with %x\n", hres);
395 ok (l == 42, "SAGUB 1 returned %d instead of 42\n", l);
396 hres = SafeArrayGetUBound (a, 2, &l);
397 ok (hres == S_OK, "SAGUB 2 failed with %x\n", hres);
398 ok (l == 24, "SAGUB 2 returned %d instead of 24\n", l);
399 hres = SafeArrayGetUBound (a, 3, &l);
400 ok (hres == DISP_E_BADINDEX, "SAGUB 3 failed with %x\n", hres);
402 i = SafeArrayGetDim(a);
403 ok(i == 2, "getdims of 2 din array returned %d\n",i);
407 hres = SafeArrayGetElement(a, indices, &i);
408 ok(DISP_E_BADINDEX == hres,"SAGE failed [0,23], hres 0x%x\n",hres);
412 hres = SafeArrayGetElement(a, indices, &i);
413 ok(DISP_E_BADINDEX == hres,"SAGE failed [1,22], hres 0x%x\n",hres);
417 hres = SafeArrayGetElement(a, indices, &i);
418 ok(S_OK == hres,"SAGE failed [1,23], hres 0x%x\n",hres);
422 hres = SafeArrayGetElement(a, indices, &i);
423 ok(DISP_E_BADINDEX == hres,"SAGE failed [1,24], hres 0x%x\n",hres);
427 hres = SafeArrayGetElement(a, indices, &i);
428 ok(S_OK == hres,"SAGE failed [42,23], hres 0x%x\n",hres);
430 hres = SafeArrayAccessData(a, (void**)&ptr1);
431 ok(S_OK == hres, "SAAD failed with 0x%x\n", hres);
435 hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
436 ok(S_OK == hres,"SAPOI failed [1,23], hres 0x%x\n",hres);
438 ok(diff == 8,"ptr difference is not 8, but %d (%p vs %p)\n", diff, ptr2, ptr1);
442 hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
443 ok(S_OK == hres,"SAPOI failed [5,24], hres 0x%x\n",hres);
445 ok(diff == 176,"ptr difference is not 176, but %d (%p vs %p)\n", diff, ptr2, ptr1);
449 hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
450 ok(S_OK == hres,"SAPOI failed [20,23], hres 0x%x\n",hres);
452 ok(diff == 76,"ptr difference is not 76, but %d (%p vs %p)\n", diff, ptr2, ptr1);
454 hres = SafeArrayUnaccessData(a);
455 ok(S_OK == hres, "SAUAD failed with 0x%x\n", hres);
457 hres = SafeArrayDestroy(a);
458 ok(hres == S_OK,"SAD faild with hres %x\n", hres);
460 for (i=0;i<sizeof(vttypes)/sizeof(vttypes[0]);i++) {
461 if ((i == VT_I8 || i == VT_UI8) && HAVE_OLEAUT32_I8)
463 vttypes[i].elemsize = sizeof(LONG64);
466 a = SafeArrayCreate(vttypes[i].vt, 1, &bound);
468 ok((!a && !vttypes[i].elemsize) ||
469 (a && vttypes[i].elemsize == a->cbElements) ||
470 (IS_ANCIENT && (vttypes[i].vt == VT_DECIMAL || vttypes[i].vt == VT_I1 ||
471 vttypes[i].vt == VT_UI2 || vttypes[i].vt == VT_UI4 || vttypes[i].vt == VT_INT ||
472 vttypes[i].vt == VT_UINT)),
473 "SAC(%d,1,[1,0]), %p result %d, expected %d\n",
474 vttypes[i].vt,a,(a?a->cbElements:0),vttypes[i].elemsize);
478 if (!HAVE_OLEAUT32_RECORD)
479 vttypes[i].expflags = 0;
480 ok(a->fFeatures == (vttypes[i].expflags | vttypes[i].addflags),
481 "SAC of %d returned feature flags %x, expected %x\n",
482 vttypes[i].vt, a->fFeatures,
483 vttypes[i].expflags|vttypes[i].addflags);
484 ok(SafeArrayGetElemsize(a) == vttypes[i].elemsize,
485 "SAGE for vt %d returned elemsize %d instead of expected %d\n",
486 vttypes[i].vt, SafeArrayGetElemsize(a),vttypes[i].elemsize);
491 if (pSafeArrayGetVartype)
493 hres = pSafeArrayGetVartype(a, &vt);
494 ok(hres == S_OK, "SAGVT of arra y with vt %d failed with %x\n", vttypes[i].vt, hres);
495 /* Windows prior to Vista returns VT_UNKNOWN instead of VT_DISPATCH */
496 ok(broken(vt == VT_UNKNOWN) || vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d\n", vttypes[i].vt, vt);
499 hres = SafeArrayCopy(a, &c);
500 ok(hres == S_OK, "failed to copy safearray of vt %d with hres %x\n", vttypes[i].vt, hres);
502 ok(vttypes[i].elemsize == c->cbElements,"copy of SAC(%d,1,[1,0]), result %d, expected %d\n",vttypes[i].vt,(c?c->cbElements:0),vttypes[i].elemsize
504 ok(c->fFeatures == (vttypes[i].expflags | vttypes[i].addflags),"SAC of %d returned feature flags %x, expected %x\n", vttypes[i].vt, c->fFeatures, vttypes[i].expflags|vttypes[i].addflags);
505 ok(SafeArrayGetElemsize(c) == vttypes[i].elemsize,"SAGE for vt %d returned elemsize %d instead of expected %d\n",vttypes[i].vt, SafeArrayGetElemsize(c),vttypes[i].elemsize);
507 if (pSafeArrayGetVartype) {
508 hres = pSafeArrayGetVartype(c, &vt);
509 ok(hres == S_OK, "SAGVT of array with vt %d failed with %x\n", vttypes[i].vt, hres);
510 /* Windows prior to Vista returns VT_UNKNOWN instead of VT_DISPATCH */
511 ok(broken(vt == VT_UNKNOWN) || vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d\n", vttypes[i].vt, vt);
514 if (pSafeArrayCopyData) {
515 hres = pSafeArrayCopyData(a, c);
516 ok(hres == S_OK, "failed to copy safearray data of vt %d with hres %x\n", vttypes[i].vt, hres);
518 hres = SafeArrayDestroyData(c);
519 ok(hres == S_OK,"SADD of copy of array with vt %d failed with hres %x\n", vttypes[i].vt, hres);
522 hres = SafeArrayDestroy(c);
523 ok(hres == S_OK,"SAD faild with hres %x\n", hres);
525 hres = SafeArrayDestroy(a);
526 ok(hres == S_OK,"SAD of array with vt %d failed with hres %x\n", vttypes[i].vt, hres);
529 /* Test conversion of type|VT_ARRAY <-> VT_BSTR */
531 bound.cElements = 10;
532 a = SafeArrayCreate(VT_UI1, 1, &bound);
533 ok(a != NULL, "SAC failed.\n");
534 ok(S_OK == SafeArrayAccessData(a, &data),"SACD failed\n");
535 memcpy(data,"Hello World\n",10);
536 ok(S_OK == SafeArrayUnaccessData(a),"SAUD failed\n");
537 V_VT(&v) = VT_ARRAY|VT_UI1;
539 hres = VariantChangeTypeEx(&v, &v, 0, 0, VT_BSTR);
540 ok(hres==S_OK, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR failed with %x\n",hres);
541 ok(V_VT(&v) == VT_BSTR,"CTE VT_ARRAY|VT_UI1 -> VT_BSTR did not return VT_BSTR, but %d.v\n",V_VT(&v));
542 ok(V_BSTR(&v)[0] == 0x6548,"First letter are not 'He', but %x\n", V_BSTR(&v)[0]);
545 /* check locking functions */
546 a = SafeArrayCreate(VT_I4, 1, &bound);
547 ok(a!=NULL,"SAC should not fail\n");
549 hres = SafeArrayAccessData(a, &data);
550 ok(hres == S_OK,"SAAD failed with hres %x\n",hres);
552 hres = SafeArrayDestroy(a);
553 ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy not failed with DISP_E_ARRAYISLOCKED, but with hres %x\n", hres);
555 hres = SafeArrayDestroyData(a);
556 ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy data not failed with DISP_E_ARRAYISLOCKED, but with hres %x\n", hres);
558 hres = SafeArrayDestroyDescriptor(a);
559 ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy descriptor not failed with DISP_E_ARRAYISLOCKED, but with hres %x\n", hres);
561 hres = SafeArrayUnaccessData(a);
562 ok(hres == S_OK,"SAUD failed after lock/destroy test\n");
564 hres = SafeArrayDestroy(a);
565 ok(hres == S_OK,"SAD failed after lock/destroy test\n");
567 /* Test if we need to destroy data before descriptor */
568 a = SafeArrayCreate(VT_I4, 1, &bound);
569 ok(a!=NULL,"SAC should not fail\n");
570 hres = SafeArrayDestroyDescriptor(a);
571 ok(hres == S_OK,"SADD with data in array failed with hres %x\n",hres);
574 /* init a small stack safearray */
575 if (pSafeArraySetIID) {
576 memset(&b, 0, sizeof(b));
578 memset(&iid, 0x42, sizeof(IID));
579 hres = pSafeArraySetIID(&b,&iid);
580 ok(hres == E_INVALIDARG,"SafeArraySetIID of non IID capable safearray did not return E_INVALIDARG, but %x\n",hres);
582 hres = SafeArrayAllocDescriptor(1,&a);
583 ok((a->fFeatures & FADF_HAVEIID) == 0,"newly allocated descriptor with SAAD should not have FADF_HAVEIID\n");
584 hres = pSafeArraySetIID(a,&iid);
585 ok(hres == E_INVALIDARG,"SafeArraySetIID of newly allocated descriptor with SAAD should return E_INVALIDARG, but %x\n",hres);
587 hres = SafeArrayDestroyDescriptor(a);
588 ok(hres == S_OK,"SADD failed with hres %x\n",hres);
591 if (!pSafeArrayAllocDescriptorEx)
594 for (i=0;i<sizeof(vttypes)/sizeof(vttypes[0]);i++) {
596 hres = pSafeArrayAllocDescriptorEx(vttypes[i].vt,1,&a);
597 ok(a->fFeatures == vttypes[i].expflags,"SAADE(%d) resulted with flags %x, expected %x\n", vttypes[i].vt, a->fFeatures, vttypes[i].expflags);
598 if (a->fFeatures & FADF_HAVEIID) {
599 hres = pSafeArrayGetIID(a, &iid);
600 ok(hres == S_OK,"SAGIID failed for vt %d with hres %x\n", vttypes[i].vt,hres);
601 switch (vttypes[i].vt) {
603 ok(IsEqualGUID(((GUID*)a)-1,&IID_IUnknown),"guid for VT_UNKNOWN is not IID_IUnknown\n");
604 ok(IsEqualGUID(&iid, &IID_IUnknown),"SAGIID returned wrong GUID for IUnknown\n");
607 ok(IsEqualGUID(((GUID*)a)-1,&IID_IDispatch),"guid for VT_UNKNOWN is not IID_IDispatch\n");
608 ok(IsEqualGUID(&iid, &IID_IDispatch),"SAGIID returned wrong GUID for IDispatch\n");
611 ok(FALSE,"unknown vt %d with FADF_HAVEIID\n",vttypes[i].vt);
615 hres = pSafeArrayGetIID(a, &iid);
616 ok(hres == E_INVALIDARG,"SAGIID did not fail for vt %d with hres %x\n", vttypes[i].vt,hres);
618 if (a->fFeatures & FADF_RECORD) {
619 ok(vttypes[i].vt == VT_RECORD,"FADF_RECORD for non record %d\n",vttypes[i].vt);
621 if (a->fFeatures & FADF_HAVEVARTYPE) {
622 ok(vttypes[i].vt == ((DWORD*)a)[-1], "FADF_HAVEVARTYPE set, but vt %d mismatch stored %d\n",vttypes[i].vt,((DWORD*)a)[-1]);
625 hres = pSafeArrayGetVartype(a, &vt);
626 ok(hres == S_OK, "SAGVT of array with vt %d failed with %x\n", vttypes[i].vt, hres);
628 if (vttypes[i].vt == VT_DISPATCH) {
629 /* Special case. Checked against Windows. */
630 ok(vt == VT_UNKNOWN, "SAGVT of array with VT_DISPATCH returned not VT_UNKNOWN, but %d\n", vt);
632 ok(vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d\n", vttypes[i].vt, vt);
635 if (a->fFeatures & FADF_HAVEIID) {
636 hres = pSafeArraySetIID(a, &IID_IStorage); /* random IID */
637 ok(hres == S_OK,"SASIID failed with FADF_HAVEIID set for vt %d with %x\n", vttypes[i].vt, hres);
638 hres = pSafeArrayGetIID(a, &iid);
639 ok(hres == S_OK,"SAGIID failed with FADF_HAVEIID set for vt %d with %x\n", vttypes[i].vt, hres);
640 ok(IsEqualGUID(&iid, &IID_IStorage),"returned iid is not IID_IStorage\n");
642 hres = pSafeArraySetIID(a, &IID_IStorage); /* random IID */
643 ok(hres == E_INVALIDARG,"SASIID did not failed with !FADF_HAVEIID set for vt %d with %x\n", vttypes[i].vt, hres);
645 hres = SafeArrayDestroyDescriptor(a);
646 ok(hres == S_OK,"SADD failed with hres %x\n",hres);
650 static void test_SafeArrayAllocDestroyDescriptor(void)
657 hres = SafeArrayAllocDescriptor(0, &sa);
658 ok(hres == E_INVALIDARG, "0 dimensions gave hres 0x%x\n", hres);
660 hres = SafeArrayAllocDescriptor(65536, &sa);
661 ok(IS_ANCIENT || hres == E_INVALIDARG,
662 "65536 dimensions gave hres 0x%x\n", hres);
666 /* Crashes on 95: XP & Wine return E_POINTER */
667 hres=SafeArrayAllocDescriptor(1, NULL);
668 ok(hres == E_POINTER,"NULL parm gave hres 0x%x\n", hres);
671 /* Test up to the dimension boundary case */
672 for (i = 5; i <= 65535; i += 30)
674 hres = SafeArrayAllocDescriptor(i, &sa);
675 ok(hres == S_OK, "%d dimensions failed; hres 0x%x\n", i, hres);
679 ok(SafeArrayGetDim(sa) == (UINT)i, "Dimension is %d; should be %d\n",
680 SafeArrayGetDim(sa), i);
682 hres = SafeArrayDestroyDescriptor(sa);
683 ok(hres == S_OK, "destroy failed; hres 0x%x\n", hres);
687 if (!pSafeArrayAllocDescriptorEx)
690 hres = pSafeArrayAllocDescriptorEx(VT_UI1, 0, &sa);
691 ok(hres == E_INVALIDARG, "0 dimensions gave hres 0x%x\n", hres);
693 hres = pSafeArrayAllocDescriptorEx(VT_UI1, 65536, &sa);
694 ok(hres == E_INVALIDARG, "65536 dimensions gave hres 0x%x\n", hres);
696 hres = pSafeArrayAllocDescriptorEx(VT_UI1, 1, NULL);
697 ok(hres == E_POINTER,"NULL parm gave hres 0x%x\n", hres);
699 hres = pSafeArrayAllocDescriptorEx(-1, 1, &sa);
700 ok(hres == S_OK, "VT = -1 gave hres 0x%x\n", hres);
702 sa->rgsabound[0].cElements = 0;
703 sa->rgsabound[0].lLbound = 1;
705 hres = SafeArrayAllocData(sa);
706 ok(hres == S_OK, "SafeArrayAllocData gave hres 0x%x\n", hres);
708 hres = SafeArrayDestroy(sa);
709 ok(hres == S_OK,"SafeArrayDestroy failed with hres %x\n",hres);
712 static void test_SafeArrayCreateLockDestroy(void)
714 SAFEARRAYBOUND sab[4];
720 #define NUM_DIMENSIONS (int)(sizeof(sab) / sizeof(sab[0]))
722 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
724 sab[dimension].lLbound = 0;
725 sab[dimension].cElements = 8;
729 /* This test crashes very early versions with no error checking...
730 sa = SafeArrayCreate(VT_UI1, 1, NULL);
731 ok(sa == NULL, "NULL bounds didn't fail\n");
733 sa = SafeArrayCreate(VT_UI1, 65536, sab);
734 ok(IS_ANCIENT || !sa, "Max bounds didn't fail\n");
736 memset(sab, 0, sizeof(sab));
738 /* Don't test 0 sized dimensions, as Windows has a bug which allows this */
740 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
741 sab[dimension].cElements = 8;
743 /* Test all VARTYPES in 1-4 dimensions */
744 for (dimension = 1; dimension < 4; dimension++)
746 for (vt = VT_EMPTY; vt < VT_CLSID; vt++)
748 DWORD dwLen = SAFEARRAY_GetVTSize(vt);
750 sa = SafeArrayCreate(vt, dimension, sab);
753 ok(sa || (IS_ANCIENT && (vt == VT_DECIMAL || vt == VT_I1 || vt == VT_UI2 ||
754 vt == VT_UI4 || vt == VT_INT || vt == VT_UINT)),
755 "VARTYPE %d (@%d dimensions) failed\n", vt, dimension);
757 ok(sa == NULL || vt == VT_R8,
758 "VARTYPE %d (@%d dimensions) succeeded!\n", vt, dimension);
762 ok(SafeArrayGetDim(sa) == (UINT)dimension,
763 "VARTYPE %d (@%d dimensions) cDims is %d, expected %d\n",
764 vt, dimension, SafeArrayGetDim(sa), dimension);
765 ok(SafeArrayGetElemsize(sa) == dwLen || vt == VT_R8,
766 "VARTYPE %d (@%d dimensions) cbElements is %d, expected %d\n",
767 vt, dimension, SafeArrayGetElemsize(sa), dwLen);
769 if (vt != VT_UNKNOWN && vt != VT_DISPATCH)
771 ok((sa->fFeatures & FADF_HAVEIID) == 0,
772 "Non interface type should not have FADF_HAVEIID\n");
773 if (pSafeArraySetIID)
775 hres = pSafeArraySetIID(sa, &IID_IUnknown);
776 ok(hres == E_INVALIDARG,
777 "Non interface type allowed SetIID(), hres %x\n", hres);
783 ok(IS_ANCIENT || sa->fFeatures & FADF_HAVEVARTYPE,
784 "Non interface type should have FADF_HAVEVARTYPE\n");
785 if (pSafeArrayGetVartype)
787 hres = pSafeArrayGetVartype(sa, &aVt);
788 ok(hres == S_OK && aVt == vt,
789 "Non interface type %d: bad type %d, hres %x\n", vt, aVt, hres);
795 ok(IS_ANCIENT || sa->fFeatures & FADF_HAVEIID,
796 "Interface type should have FADF_HAVEIID\n");
797 if (pSafeArraySetIID)
799 hres = pSafeArraySetIID(sa, &IID_IUnknown);
801 "Non interface type disallowed SetIID(), hres %x\n", hres);
803 ok((sa->fFeatures & FADF_HAVEVARTYPE) == 0,
804 "Interface type %d should not have FADF_HAVEVARTYPE\n", vt);
807 hres = SafeArrayLock(sa);
808 ok(hres == S_OK, "Lock VARTYPE %d (@%d dimensions) failed; hres 0x%x\n",
809 vt, dimension, hres);
813 hres = SafeArrayDestroy(sa);
814 ok(hres == DISP_E_ARRAYISLOCKED,"Destroy() got hres %x\n", hres);
816 hres = SafeArrayDestroyData(sa);
817 ok(hres == DISP_E_ARRAYISLOCKED,"DestroyData() got hres %x\n", hres);
819 hres = SafeArrayDestroyDescriptor(sa);
820 ok(hres == DISP_E_ARRAYISLOCKED,"DestroyDescriptor() got hres %x\n", hres);
822 hres = SafeArrayUnlock(sa);
823 ok(hres == S_OK, "Unlock VARTYPE %d (@%d dims) hres 0x%x\n",
824 vt, dimension, hres);
826 hres = SafeArrayDestroy(sa);
827 ok(hres == S_OK, "destroy VARTYPE %d (@%d dims) hres 0x%x\n",
828 vt, dimension, hres);
835 static void test_VectorCreateLockDestroy(void)
842 if (!pSafeArrayCreateVector)
844 sa = pSafeArrayCreateVector(VT_UI1, 0, 0);
845 ok(sa != NULL, "SACV with 0 elements failed.\n");
847 hres = SafeArrayDestroy(sa);
848 ok(hres == S_OK, "SafeArrayDestroy failed with hres %x\n",hres);
850 /* Test all VARTYPES in different lengths */
851 for (element = 1; element <= 101; element += 10)
853 for (vt = VT_EMPTY; vt < VT_CLSID; vt++)
855 DWORD dwLen = SAFEARRAY_GetVTSize(vt);
857 sa = pSafeArrayCreateVector(vt, 0, element);
860 ok(sa != NULL, "VARTYPE %d (@%d elements) failed\n", vt, element);
862 ok(sa == NULL, "VARTYPE %d (@%d elements) succeeded!\n", vt, element);
866 ok(SafeArrayGetDim(sa) == 1, "VARTYPE %d (@%d elements) cDims %d, not 1\n",
867 vt, element, SafeArrayGetDim(sa));
868 ok(SafeArrayGetElemsize(sa) == dwLen,
869 "VARTYPE %d (@%d elements) cbElements is %d, expected %d\n",
870 vt, element, SafeArrayGetElemsize(sa), dwLen);
872 hres = SafeArrayLock(sa);
873 ok(hres == S_OK, "Lock VARTYPE %d (@%d elements) failed; hres 0x%x\n",
878 hres = SafeArrayUnlock(sa);
879 ok(hres == S_OK, "Unlock VARTYPE %d (@%d elements) failed; hres 0x%x\n",
882 hres = SafeArrayDestroy(sa);
883 ok(hres == S_OK, "destroy VARTYPE %d (@%d elements) failed; hres 0x%x\n",
891 static void test_LockUnlock(void)
893 SAFEARRAYBOUND sab[4];
896 BOOL bVector = FALSE;
900 hres = SafeArrayLock(NULL);
901 ok(hres == E_INVALIDARG, "Lock NULL array hres 0x%x\n", hres);
902 hres = SafeArrayUnlock(NULL);
903 ok(hres == E_INVALIDARG, "Lock NULL array hres 0x%x\n", hres);
905 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
907 sab[dimension].lLbound = 0;
908 sab[dimension].cElements = 8;
911 sa = SafeArrayCreate(VT_UI1, NUM_DIMENSIONS, sab);
913 /* Test maximum locks */
914 test_LockUnlock_Vector:
919 hres = SafeArrayUnlock(sa);
920 ok (hres == E_UNEXPECTED, "Bad %sUnlock gave hres 0x%x\n",
921 bVector ? "vector " : "\n", hres);
923 while ((hres = SafeArrayLock(sa)) == S_OK)
925 ok (count == 65535 && hres == E_UNEXPECTED, "Lock %sfailed at %d; hres 0x%x\n",
926 bVector ? "vector " : "\n", count, hres);
928 if (count == 65535 && hres == E_UNEXPECTED)
930 while ((hres = SafeArrayUnlock(sa)) == S_OK)
932 ok (count == 0 && hres == E_UNEXPECTED, "Unlock %sfailed at %d; hres 0x%x\n",
933 bVector ? "vector " : "\n", count, hres);
936 SafeArrayDestroy(sa);
939 if (bVector == FALSE && pSafeArrayCreateVector)
941 /* Test again with a vector */
942 sa = pSafeArrayCreateVector(VT_UI1, 0, 100);
944 goto test_LockUnlock_Vector;
948 static void test_SafeArrayGetPutElement(void)
950 SAFEARRAYBOUND sab[4];
951 LONG indices[NUM_DIMENSIONS];
954 int value = 0, gotvalue, dimension;
955 unsigned int x,y,z,a;
957 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
959 sab[dimension].lLbound = dimension * 2 + 1;
960 sab[dimension].cElements = dimension * 3 + 1;
963 sa = SafeArrayCreate(VT_INT, NUM_DIMENSIONS, sab);
965 return; /* Some early versions can't handle > 3 dims */
967 ok(sa->cbElements == sizeof(value), "int size mismatch\n");
968 if (sa->cbElements != sizeof(value))
972 for (x = 0; x < NUM_DIMENSIONS; x++)
974 indices[0] = sab[0].lLbound;
975 indices[1] = sab[1].lLbound;
976 indices[2] = sab[2].lLbound;
977 indices[3] = sab[3].lLbound;
979 indices[x] = indices[x] - 1;
980 hres = SafeArrayPutElement(sa, indices, &value);
981 ok(hres == DISP_E_BADINDEX, "Put allowed too small index in dimension %d\n", x);
982 hres = SafeArrayGetElement(sa, indices, &value);
983 ok(hres == DISP_E_BADINDEX, "Get allowed too small index in dimension %d\n", x);
985 indices[x] = sab[x].lLbound + sab[x].cElements;
986 hres = SafeArrayPutElement(sa, indices, &value);
987 ok(hres == DISP_E_BADINDEX, "Put allowed too big index in dimension %d\n", x);
988 hres = SafeArrayGetElement(sa, indices, &value);
989 ok(hres == DISP_E_BADINDEX, "Get allowed too big index in dimension %d\n", x);
992 indices[0] = sab[0].lLbound;
993 indices[1] = sab[1].lLbound;
994 indices[2] = sab[2].lLbound;
995 indices[3] = sab[3].lLbound;
997 hres = SafeArrayPutElement(NULL, indices, &value);
998 ok(hres == E_INVALIDARG, "Put NULL array hres 0x%x\n", hres);
999 hres = SafeArrayGetElement(NULL, indices, &value);
1000 ok(hres == E_INVALIDARG, "Get NULL array hres 0x%x\n", hres);
1002 hres = SafeArrayPutElement(sa, NULL, &value);
1003 ok(hres == E_INVALIDARG, "Put NULL indices hres 0x%x\n", hres);
1004 hres = SafeArrayGetElement(sa, NULL, &value);
1005 ok(hres == E_INVALIDARG, "Get NULL indices hres 0x%x\n", hres);
1009 /* This is retarded. Windows checks every case of invalid parameters
1010 * except the following, which crashes. We ERR this in Wine.
1012 hres = SafeArrayPutElement(sa, indices, NULL);
1013 ok(hres == E_INVALIDARG, "Put NULL value hres 0x%x\n", hres);
1016 hres = SafeArrayGetElement(sa, indices, NULL);
1017 ok(hres == E_INVALIDARG, "Get NULL value hres 0x%x\n", hres);
1021 /* Make sure we can read and get back the correct values in 4 dimensions,
1022 * Each with a different size and lower bound.
1024 for (x = 0; x < sab[0].cElements; x++)
1026 indices[0] = sab[0].lLbound + x;
1027 for (y = 0; y < sab[1].cElements; y++)
1029 indices[1] = sab[1].lLbound + y;
1030 for (z = 0; z < sab[2].cElements; z++)
1032 indices[2] = sab[2].lLbound + z;
1033 for (a = 0; a < sab[3].cElements; a++)
1035 indices[3] = sab[3].lLbound + a;
1036 hres = SafeArrayPutElement(sa, indices, &value);
1037 ok(hres == S_OK, "Failed to put element at (%d,%d,%d,%d) hres 0x%x\n",
1047 for (x = 0; x < sab[0].cElements; x++)
1049 indices[0] = sab[0].lLbound + x;
1050 for (y = 0; y < sab[1].cElements; y++)
1052 indices[1] = sab[1].lLbound + y;
1053 for (z = 0; z < sab[2].cElements; z++)
1055 indices[2] = sab[2].lLbound + z;
1056 for (a = 0; a < sab[3].cElements; a++)
1058 indices[3] = sab[3].lLbound + a;
1059 gotvalue = value / 3;
1060 hres = SafeArrayGetElement(sa, indices, &gotvalue);
1061 ok(hres == S_OK, "Failed to get element at (%d,%d,%d,%d) hres 0x%x\n",
1064 ok(value == gotvalue, "Got value %d instead of %d at (%d,%d,%d,%d)\n",
1065 gotvalue, value, x, y, z, a);
1071 SafeArrayDestroy(sa);
1074 static void test_SafeArrayGetPutElement_BSTR(void)
1080 BSTR value = 0, gotvalue;
1081 const OLECHAR szTest[5] = { 'T','e','s','t','\0' };
1086 sa = SafeArrayCreate(VT_BSTR, 1, &sab);
1087 ok(sa != NULL, "BSTR test couldn't create array\n");
1091 ok(sa->cbElements == sizeof(BSTR), "BSTR size mismatch\n");
1092 if (sa->cbElements != sizeof(BSTR))
1095 indices[0] = sab.lLbound;
1096 value = SysAllocString(szTest);
1097 ok (value != NULL, "Expected non-NULL\n");
1098 hres = SafeArrayPutElement(sa, indices, value);
1099 ok(hres == S_OK, "Failed to put bstr element hres 0x%x\n", hres);
1101 hres = SafeArrayGetElement(sa, indices, &gotvalue);
1102 ok(hres == S_OK, "Failed to get bstr element at hres 0x%x\n", hres);
1104 ok(SysStringLen(value) == SysStringLen(gotvalue), "Got len %d instead of %d\n", SysStringLen(gotvalue), SysStringLen(value));
1105 SafeArrayDestroy(sa);
1106 SysFreeString(value);
1107 SysFreeString(gotvalue);
1110 static int tunk_xref = 0;
1111 static HRESULT WINAPI tunk_QueryInterface(LPUNKNOWN punk,REFIID riid, LPVOID *x) {
1114 static ULONG WINAPI tunk_AddRef(LPUNKNOWN punk) {
1118 static ULONG WINAPI tunk_Release(LPUNKNOWN punk) {
1122 static const IUnknownVtbl xtunk_vtbl = {
1123 tunk_QueryInterface,
1128 static struct xtunk_iface {
1129 const IUnknownVtbl *lpvtbl;
1133 static void test_SafeArrayGetPutElement_IUnknown(void)
1139 LPUNKNOWN value = 0, gotvalue;
1143 sa = SafeArrayCreate(VT_UNKNOWN, 1, &sab);
1144 ok(sa != NULL, "UNKNOWN test couldn't create array\n");
1148 ok(sa->cbElements == sizeof(LPUNKNOWN), "LPUNKNOWN size mismatch\n");
1149 if (sa->cbElements != sizeof(LPUNKNOWN))
1152 indices[0] = sab.lLbound;
1153 xtunk_iface.lpvtbl = &xtunk_vtbl;
1154 value = (LPUNKNOWN)&xtunk_iface;
1156 ok (value != NULL, "Expected non-NULL\n");
1157 hres = SafeArrayPutElement(sa, indices, value);
1158 ok(hres == S_OK, "Failed to put bstr element hres 0x%x\n", hres);
1159 ok(tunk_xref == 2,"Failed to increment refcount of iface.\n");
1161 hres = SafeArrayGetElement(sa, indices, &gotvalue);
1162 ok(tunk_xref == 3,"Failed to increment refcount of iface.\n");
1163 ok(hres == S_OK, "Failed to get bstr element at hres 0x%x\n", hres);
1165 ok(value == gotvalue, "Got %p instead of %p\n", gotvalue, value);
1166 SafeArrayDestroy(sa);
1167 ok(tunk_xref == 2,"Failed to decrement refcount of iface.\n");
1170 static void test_SafeArrayRedim_IUnknown(void)
1180 sa = SafeArrayCreate(VT_UNKNOWN, 1, &sab);
1181 ok(sa != NULL, "UNKNOWN test couldn't create array\n");
1185 ok(sa->cbElements == sizeof(LPUNKNOWN), "LPUNKNOWN size mismatch\n");
1186 if (sa->cbElements != sizeof(LPUNKNOWN))
1190 xtunk_iface.lpvtbl = &xtunk_vtbl;
1191 value = (LPUNKNOWN)&xtunk_iface;
1193 hres = SafeArrayPutElement(sa, indices, value);
1194 ok(hres == S_OK, "Failed to put IUnknown element hres 0x%x\n", hres);
1195 ok(tunk_xref == 2,"Failed to increment refcount of iface.\n");
1197 hres = SafeArrayRedim(sa, &sab);
1198 ok(hres == S_OK, "Failed to shrink array hres 0x%x\n", hres);
1199 ok(tunk_xref == 1, "Failed to decrement refcount\n");
1200 SafeArrayDestroy(sa);
1203 static void test_SafeArrayGetPutElement_VARIANT(void)
1209 VARIANT value, gotvalue;
1213 sa = SafeArrayCreate(VT_VARIANT, 1, &sab);
1214 ok(sa != NULL, "VARIANT test couldn't create array\n");
1218 ok(sa->cbElements == sizeof(VARIANT), "VARIANT size mismatch\n");
1219 if (sa->cbElements != sizeof(VARIANT))
1222 indices[0] = sab.lLbound;
1223 V_VT(&value) = VT_I4;
1224 V_I4(&value) = 0x42424242;
1225 hres = SafeArrayPutElement(sa, indices, &value);
1226 ok(hres == S_OK, "Failed to put Variant I4 element hres 0x%x\n", hres);
1228 V_VT(&gotvalue) = 0xdead;
1229 hres = SafeArrayGetElement(sa, indices, &gotvalue);
1230 ok(hres == S_OK, "Failed to get variant element at hres 0x%x\n", hres);
1232 V_VT(&gotvalue) = VT_EMPTY;
1233 hres = SafeArrayGetElement(sa, indices, &gotvalue);
1234 ok(hres == S_OK, "Failed to get variant element at hres 0x%x\n", hres);
1236 ok(V_VT(&value) == V_VT(&gotvalue), "Got type 0x%x instead of 0x%x\n", V_VT(&value), V_VT(&gotvalue));
1237 if (V_VT(&value) == V_VT(&gotvalue))
1238 ok(V_I4(&value) == V_I4(&gotvalue), "Got %d instead of %d\n", V_I4(&value), V_VT(&gotvalue));
1240 SafeArrayDestroy(sa);
1244 static void test_SafeArrayCopyData(void)
1246 SAFEARRAYBOUND sab[4];
1250 int dimension,size=1;
1252 if (!pSafeArrayCopyData)
1255 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
1257 sab[dimension].lLbound = dimension * 2 + 2;
1258 sab[dimension].cElements = dimension * 3 + 1;
1259 size *= sab[dimension].cElements;
1262 sa = SafeArrayCreate(VT_INT, NUM_DIMENSIONS, sab);
1263 ok(sa != NULL, "Copy test couldn't create array\n");
1264 sacopy = SafeArrayCreate(VT_INT, NUM_DIMENSIONS, sab);
1265 ok(sacopy != NULL, "Copy test couldn't create copy array\n");
1270 ok(sa->cbElements == sizeof(int), "int size mismatch\n");
1271 if (sa->cbElements != sizeof(int))
1274 /* Fill the source array with some data; it doesn't matter what */
1275 for (dimension = 0; dimension < size; dimension++)
1277 int* data = sa->pvData;
1278 data[dimension] = dimension;
1281 hres = pSafeArrayCopyData(sa, sacopy);
1282 ok(hres == S_OK, "copy data failed hres 0x%x\n", hres);
1285 ok(!memcmp(sa->pvData, sacopy->pvData, size * sizeof(int)), "compared different\n");
1289 hres = pSafeArrayCopyData(NULL, sacopy);
1290 ok(hres == E_INVALIDARG, "Null copy source hres 0x%x\n", hres);
1291 hres = pSafeArrayCopyData(sa, NULL);
1292 ok(hres == E_INVALIDARG, "Null copy hres 0x%x\n", hres);
1294 sacopy->rgsabound[0].cElements += 1;
1295 hres = pSafeArrayCopyData(sa, sacopy);
1296 ok(hres == E_INVALIDARG, "Bigger copy first dimension hres 0x%x\n", hres);
1298 sacopy->rgsabound[0].cElements -= 2;
1299 hres = pSafeArrayCopyData(sa, sacopy);
1300 ok(hres == E_INVALIDARG, "Smaller copy first dimension hres 0x%x\n", hres);
1301 sacopy->rgsabound[0].cElements += 1;
1303 sacopy->rgsabound[3].cElements += 1;
1304 hres = pSafeArrayCopyData(sa, sacopy);
1305 ok(hres == E_INVALIDARG, "Bigger copy last dimension hres 0x%x\n", hres);
1307 sacopy->rgsabound[3].cElements -= 2;
1308 hres = pSafeArrayCopyData(sa, sacopy);
1309 ok(hres == E_INVALIDARG, "Smaller copy last dimension hres 0x%x\n", hres);
1310 sacopy->rgsabound[3].cElements += 1;
1312 SafeArrayDestroy(sacopy);
1314 hres = pSafeArrayCopyData(sa, sacopy);
1315 ok(hres == E_INVALIDARG, "->Null copy hres 0x%x\n", hres);
1317 hres = SafeArrayCopy(sa, &sacopy);
1318 ok(hres == S_OK, "copy failed hres 0x%x\n", hres);
1321 ok(SafeArrayGetElemsize(sa) == SafeArrayGetElemsize(sacopy),"elemsize wrong\n");
1322 ok(SafeArrayGetDim(sa) == SafeArrayGetDim(sacopy),"dimensions wrong\n");
1323 ok(!memcmp(sa->pvData, sacopy->pvData, size * sizeof(int)), "compared different\n");
1326 SafeArrayDestroy(sa);
1329 static void test_SafeArrayCreateEx(void)
1331 IRecordInfoImpl* iRec;
1332 SAFEARRAYBOUND sab[4];
1337 if (!pSafeArrayCreateEx)
1340 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
1342 sab[dimension].lLbound = 0;
1343 sab[dimension].cElements = 8;
1347 sa = pSafeArrayCreateEx(VT_UI1, 1, NULL, NULL);
1348 ok(sa == NULL, "CreateEx NULL bounds didn't fail\n");
1350 /* test IID storage & defaulting */
1351 sa = pSafeArrayCreateEx(VT_DISPATCH, 1, sab, (PVOID)&IID_ITypeInfo);
1352 ok(sa != NULL, "CreateEx (ITypeInfo) failed\n");
1357 if (pSafeArrayGetIID)
1359 hres = pSafeArrayGetIID(sa, &guid);
1360 ok(hres == S_OK, "CreateEx (ITypeInfo) no IID hres 0x%x\n", hres);
1363 ok(IsEqualGUID(&guid, &IID_ITypeInfo), "CreateEx (ITypeInfo) bad IID\n");
1366 if (pSafeArraySetIID)
1368 hres = pSafeArraySetIID(sa, &IID_IUnknown);
1369 ok(hres == S_OK, "Failed to set IID, hres = %8x\n", hres);
1370 if (hres == S_OK && pSafeArrayGetIID)
1372 hres = pSafeArrayGetIID(sa, &guid);
1373 ok(hres == S_OK && IsEqualGUID(&guid, &IID_IUnknown), "Set bad IID\n");
1376 SafeArrayDestroy(sa);
1379 sa = pSafeArrayCreateEx(VT_DISPATCH, 1, sab, NULL);
1380 ok(sa != NULL, "CreateEx (NULL) failed\n");
1385 if (pSafeArrayGetIID)
1387 hres = pSafeArrayGetIID(sa, &guid);
1388 ok(hres == S_OK, "CreateEx (NULL) no IID hres 0x%x\n", hres);
1391 ok(IsEqualGUID(&guid, &IID_IDispatch), "CreateEx (NULL) bad IID\n");
1394 SafeArrayDestroy(sa);
1397 sa = pSafeArrayCreateEx(VT_UNKNOWN, 1, sab, NULL);
1398 ok(sa != NULL, "CreateEx (NULL-Unk) failed\n");
1403 if (pSafeArrayGetIID)
1405 hres = pSafeArrayGetIID(sa, &guid);
1406 ok(hres == S_OK, "CreateEx (NULL-Unk) no IID hres 0x%x\n", hres);
1409 ok(IsEqualGUID(&guid, &IID_IUnknown), "CreateEx (NULL-Unk) bad IID\n");
1412 SafeArrayDestroy(sa);
1415 /* VT_RECORD failure case */
1416 sa = pSafeArrayCreateEx(VT_RECORD, 1, sab, NULL);
1417 ok(sa == NULL, "CreateEx (NULL-Rec) succeded\n");
1419 iRec = IRecordInfoImpl_Construct();
1421 /* Win32 doesn't care if GetSize fails */
1422 fail_GetSize = TRUE;
1423 sa = pSafeArrayCreateEx(VT_RECORD, 1, sab, iRec);
1424 ok(sa != NULL, "CreateEx (Fail Size) failed\n");
1425 ok(iRec->ref == START_REF_COUNT + 1, "Wrong iRec refcount %d\n", iRec->ref);
1426 ok(iRec->sizeCalled == 1, "GetSize called %d times\n", iRec->sizeCalled);
1427 ok(iRec->clearCalled == 0, "Clear called %d times\n", iRec->clearCalled);
1430 ok(sa->cbElements == RECORD_SIZE_FAIL, "Altered size to %d\n", sa->cbElements);
1431 SafeArrayDestroy(sa);
1432 ok(iRec->clearCalled == sab[0].cElements, "Destroy->Clear called %d times\n", iRec->clearCalled);
1435 /* Test VT_RECORD array */
1436 fail_GetSize = FALSE;
1437 iRec->ref = START_REF_COUNT;
1438 iRec->sizeCalled = 0;
1439 iRec->clearCalled = 0;
1440 sa = pSafeArrayCreateEx(VT_RECORD, 1, sab, iRec);
1441 ok(sa != NULL, "CreateEx (Rec) failed\n");
1442 ok(iRec->ref == START_REF_COUNT + 1, "Wrong iRec refcount %d\n", iRec->ref);
1443 ok(iRec->sizeCalled == 1, "GetSize called %d times\n", iRec->sizeCalled);
1444 ok(iRec->clearCalled == 0, "Clear called %d times\n", iRec->clearCalled);
1445 if (sa && pSafeArrayGetRecordInfo)
1447 IRecordInfo* saRec = NULL;
1448 hres = pSafeArrayGetRecordInfo(sa, &saRec);
1450 ok(hres == S_OK,"GRI failed\n");
1451 ok(saRec == (IRecordInfo*)iRec,"Different saRec\n");
1452 ok(iRec->ref == START_REF_COUNT + 2, "Didn't AddRef %d\n", iRec->ref);
1453 if (iRec->ref == START_REF_COUNT + 2)
1454 IRecordInfo_Release(saRec);
1456 ok(sa->cbElements == RECORD_SIZE,"Elemsize is %d\n", sa->cbElements);
1458 SafeArrayDestroy(sa);
1459 ok(iRec->sizeCalled == 1, "Destroy->GetSize called %d times\n", iRec->sizeCalled);
1460 ok(iRec->clearCalled == sab[0].cElements, "Destroy->Clear called %d times\n", iRec->clearCalled);
1461 ok(iRec->ref == START_REF_COUNT, "Wrong iRec refcount %d\n", iRec->ref);
1465 static void test_SafeArrayClear(void)
1474 sa = SafeArrayCreate(VT_UI1, 1, &sab);
1475 ok(sa != NULL, "Create() failed.\n");
1479 /* Test clearing non-NULL variants containing arrays */
1480 V_VT(&v) = VT_ARRAY|VT_UI1;
1482 hres = VariantClear(&v);
1483 ok(hres == S_OK && V_VT(&v) == VT_EMPTY, "VariantClear: hres 0x%x, Type %d\n", hres, V_VT(&v));
1484 ok(V_ARRAY(&v) == sa, "VariantClear: Overwrote value\n");
1486 sa = SafeArrayCreate(VT_UI1, 1, &sab);
1487 ok(sa != NULL, "Create() failed.\n");
1491 V_VT(&v) = VT_SAFEARRAY;
1493 hres = VariantClear(&v);
1494 ok(hres == DISP_E_BADVARTYPE, "VariantClear: hres 0x%x\n", hres);
1496 V_VT(&v) = VT_SAFEARRAY|VT_BYREF;
1497 V_ARRAYREF(&v) = &sa;
1498 hres = VariantClear(&v);
1499 ok(hres == DISP_E_BADVARTYPE, "VariantClear: hres 0x%x\n", hres);
1501 SafeArrayDestroy(sa);
1504 static void test_SafeArrayCopy(void)
1507 SAFEARRAY *sa, *sa2;
1508 VARIANTARG vSrc, vDst;
1513 sa = SafeArrayCreate(VT_UI1, 1, &sab);
1514 ok(sa != NULL, "Create() failed.\n");
1518 /* Test copying non-NULL variants containing arrays */
1519 V_VT(&vSrc) = (VT_ARRAY|VT_BYREF|VT_UI1);
1520 V_ARRAYREF(&vSrc) = &sa;
1521 V_VT(&vDst) = VT_EMPTY;
1523 hres = VariantCopy(&vDst, &vSrc);
1524 ok(hres == S_OK && V_VT(&vDst) == (VT_ARRAY|VT_BYREF|VT_UI1),
1525 "VariantCopy: hres 0x%x, Type %d\n", hres, V_VT(&vDst));
1526 ok(V_ARRAYREF(&vDst) == &sa, "VariantClear: Performed deep copy\n");
1528 V_VT(&vSrc) = (VT_ARRAY|VT_UI1);
1529 V_ARRAY(&vSrc) = sa;
1530 V_VT(&vDst) = VT_EMPTY;
1532 hres = VariantCopy(&vDst, &vSrc);
1533 ok(hres == S_OK && V_VT(&vDst) == (VT_ARRAY|VT_UI1),
1534 "VariantCopy: hres 0x%x, Type %d\n", hres, V_VT(&vDst));
1535 ok(V_ARRAY(&vDst) != sa, "VariantClear: Performed shallow copy\n");
1537 SafeArrayDestroy(V_ARRAY(&vSrc));
1538 SafeArrayDestroy(V_ARRAY(&vDst));
1540 hres = SafeArrayAllocDescriptor(1, &sa);
1541 ok(hres == S_OK, "SafeArrayAllocDescriptor failed with error 0x%08x\n", hres);
1543 hres = SafeArrayCopy(sa, &sa2);
1544 ok(hres == E_INVALIDARG,
1545 "SafeArrayCopy with empty array should have failed with error E_INVALIDARG instead of 0x%08x\n",
1547 sa->cbElements = 16;
1548 hres = SafeArrayCopy(sa, &sa2);
1549 ok(hres == S_OK, "SafeArrayCopy failed with error 0x%08x\n", hres);
1551 SafeArrayDestroy(sa);
1554 #define MKARRAY(low,num,typ) sab.lLbound = low; sab.cElements = num; \
1555 sa = SafeArrayCreate(typ, 1, &sab); ok(sa != NULL, "Create() failed.\n"); \
1557 V_VT(&v) = VT_ARRAY|typ; V_ARRAY(&v) = sa; VariantInit(&v2)
1559 #define MKARRAYCONT(low,num,typ) sab.lLbound = low; sab.cElements = num; \
1560 sa = SafeArrayCreate(typ, 1, &sab); if (!sa) continue; \
1561 V_VT(&v) = VT_ARRAY|typ; V_ARRAY(&v) = sa; VariantInit(&v2)
1563 static void test_SafeArrayChangeTypeEx(void)
1565 static const char *szHello = "Hello World";
1572 /* VT_ARRAY|VT_UI1 -> VT_BSTR */
1573 MKARRAY(0,strlen(szHello)+1,VT_UI1);
1574 memcpy(sa->pvData, szHello, strlen(szHello)+1);
1576 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_BSTR);
1577 ok(hres == S_OK, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR failed with %x\n", hres);
1580 ok(V_VT(&v2) == VT_BSTR, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR did not return VT_BSTR, but %d.\n",V_VT(&v2));
1581 ok(strcmp((char*)V_BSTR(&v2),szHello) == 0,"Expected string '%s', got '%s'\n", szHello,
1582 (char*)V_BSTR(&v2));
1586 /* VT_VECTOR|VT_UI1 -> VT_BSTR */
1587 SafeArrayDestroy(sa);
1588 if (pSafeArrayCreateVector)
1590 sa = pSafeArrayCreateVector(VT_UI1, 0, strlen(szHello)+1);
1591 ok(sa != NULL, "CreateVector() failed.\n");
1595 memcpy(sa->pvData, szHello, strlen(szHello)+1);
1596 V_VT(&v) = VT_VECTOR|VT_UI1;
1600 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_BSTR);
1601 ok(hres == DISP_E_BADVARTYPE, "CTE VT_VECTOR|VT_UI1 returned %x\n", hres);
1603 /* (vector)VT_ARRAY|VT_UI1 -> VT_BSTR (In place) */
1604 V_VT(&v) = VT_ARRAY|VT_UI1;
1605 hres = VariantChangeTypeEx(&v, &v, 0, 0, VT_BSTR);
1606 ok(hres == S_OK, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR failed with %x\n", hres);
1609 ok(V_VT(&v) == VT_BSTR, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR did not return VT_BSTR, but %d.\n",V_VT(&v));
1610 ok(strcmp((char*)V_BSTR(&v),szHello) == 0,"Expected string '%s', got '%s'\n", szHello,
1616 /* To/from BSTR only works with arrays of VT_UI1 */
1617 for (vt = 0; vt <= VT_CLSID; vt++)
1622 MKARRAYCONT(0,1,vt);
1623 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_BSTR);
1624 ok(hres != S_OK, "CTE VT_ARRAY|VT %d->BSTR succeeded\n", vt);
1629 /* Can't change an array of one type into array of another type , even
1630 * if the other type is the same size
1632 if (pSafeArrayCreateVector)
1634 sa = pSafeArrayCreateVector(VT_UI1, 0, 1);
1635 ok(sa != NULL, "CreateVector() failed.\n");
1639 V_VT(&v) = VT_ARRAY|VT_UI1;
1641 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_ARRAY|VT_I1);
1642 ok(hres == DISP_E_TYPEMISMATCH, "CTE VT_ARRAY|VT_UI1->VT_ARRAY|VT_I1 returned %x\n", hres);
1644 /* But can change to the same array type */
1645 SafeArrayDestroy(sa);
1646 sa = pSafeArrayCreateVector(VT_UI1, 0, 1);
1647 ok(sa != NULL, "CreateVector() failed.\n");
1650 V_VT(&v) = VT_ARRAY|VT_UI1;
1652 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_ARRAY|VT_UI1);
1653 ok(hres == S_OK, "CTE VT_ARRAY|VT_UI1->VT_ARRAY|VT_UI1 returned %x\n", hres);
1654 SafeArrayDestroy(sa);
1658 MKARRAY(0,1,VT_UI1);
1659 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_NULL);
1660 ok(hres == DISP_E_TYPEMISMATCH, "CTE VT_ARRAY|VT_UI1 returned %x\n", hres);
1661 MKARRAY(0,1,VT_UI1);
1662 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_EMPTY);
1663 ok(hres == DISP_E_TYPEMISMATCH, "CTE VT_ARRAY|VT_UI1 returned %x\n", hres);
1667 static void test_SafeArrayDestroyData (void)
1672 int value = 0xdeadbeef;
1674 void HUGEP *temp_pvData;
1678 sa = SafeArrayCreate(VT_INT, 1, &sab);
1679 ok(sa != NULL, "Create() failed.\n");
1683 SafeArrayPutElement (sa, index, &value);
1685 /* SafeArrayDestroyData shouldn't free pvData if FADF_STATIC is set. */
1686 sa->fFeatures |= FADF_STATIC;
1687 temp_pvData = sa->pvData;
1688 hres = SafeArrayDestroyData(sa);
1689 ok(hres == S_OK, "SADData FADF_STATIC failed, error code %x.\n",hres);
1690 ok(sa->pvData == temp_pvData, "SADData FADF_STATIC: pvData=%p, expected %p (fFeatures = %d).\n",
1691 sa->pvData, temp_pvData, sa->fFeatures);
1692 SafeArrayGetElement (sa, index, &value);
1693 ok(value == 0, "Data not cleared after SADData\n");
1695 /* Clear FADF_STATIC, now really destroy the data. */
1696 sa->fFeatures ^= FADF_STATIC;
1697 hres = SafeArrayDestroyData(sa);
1698 ok(hres == S_OK, "SADData !FADF_STATIC failed, error code %x.\n",hres);
1699 ok(sa->pvData == NULL, "SADData !FADF_STATIC: pvData=%p, expected NULL.\n", sa->pvData);
1701 hres = SafeArrayDestroy(sa);
1702 ok(hres == S_OK, "SAD failed, error code %x.\n", hres);
1705 START_TEST(safearray)
1707 hOleaut32 = GetModuleHandleA("oleaut32.dll");
1709 GETPTR(SafeArrayAllocDescriptorEx);
1710 GETPTR(SafeArrayCopyData);
1711 GETPTR(SafeArrayGetIID);
1712 GETPTR(SafeArraySetIID);
1713 GETPTR(SafeArrayGetVartype);
1714 GETPTR(SafeArrayCreateEx);
1715 GETPTR(SafeArrayCreateVector);
1717 check_for_VT_INT_PTR();
1719 test_SafeArrayAllocDestroyDescriptor();
1720 test_SafeArrayCreateLockDestroy();
1721 test_VectorCreateLockDestroy();
1723 test_SafeArrayChangeTypeEx();
1724 test_SafeArrayCopy();
1725 test_SafeArrayClear();
1726 test_SafeArrayCreateEx();
1727 test_SafeArrayCopyData();
1728 test_SafeArrayDestroyData();
1729 test_SafeArrayGetPutElement();
1730 test_SafeArrayGetPutElement_BSTR();
1731 test_SafeArrayGetPutElement_IUnknown();
1732 test_SafeArrayRedim_IUnknown();
1733 test_SafeArrayGetPutElement_VARIANT();