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
29 #include "wine/test.h"
42 static HMODULE hOleaut32;
44 static HRESULT (WINAPI *pSafeArrayAllocDescriptorEx)(VARTYPE,UINT,SAFEARRAY**);
45 static HRESULT (WINAPI *pSafeArrayCopyData)(SAFEARRAY*,SAFEARRAY*);
46 static HRESULT (WINAPI *pSafeArrayGetIID)(SAFEARRAY*,GUID*);
47 static HRESULT (WINAPI *pSafeArraySetIID)(SAFEARRAY*,REFGUID);
48 static HRESULT (WINAPI *pSafeArrayGetVartype)(SAFEARRAY*,VARTYPE*);
49 static HRESULT (WINAPI *pSafeArrayGetRecordInfo)(SAFEARRAY*,IRecordInfo**);
50 static SAFEARRAY* (WINAPI *pSafeArrayCreateEx)(VARTYPE,UINT,SAFEARRAYBOUND*,LPVOID);
51 static SAFEARRAY* (WINAPI *pSafeArrayCreateVector)(VARTYPE,LONG,ULONG);
53 #define GETPTR(func) p##func = (void*)GetProcAddress(hOleaut32, #func)
55 /* Is a given function exported from oleaut32? */
56 #define HAVE_FUNC(func) ((void*)GetProcAddress(hOleaut32, #func) != NULL)
58 /* Have IRecordInfo data type? */
59 #define HAVE_OLEAUT32_RECORD HAVE_FUNC(SafeArraySetRecordInfo)
60 /* Have R8 data type? */
61 #define HAVE_OLEAUT32_R8 HAVE_FUNC(VarR8FromI1)
62 /* Have I8/UI8 data type? */
63 #define HAVE_OLEAUT32_I8 HAVE_FUNC(VarI8FromI1)
64 /* Have the decimal type? */
65 #define HAVE_OLEAUT32_DECIMAL HAVE_FUNC(VarDecAdd)
66 /* Have INT_PTR/UINT_PTR type? */
67 static BOOL HAVE_OLEAUT32_INT_PTR;
69 /* very old version? */
70 #define IS_ANCIENT (!HAVE_FUNC(VarI1FromI2))
72 #define START_REF_COUNT 1
73 #define RECORD_SIZE 64
74 #define RECORD_SIZE_FAIL 17
75 /************************************************************************
76 * Dummy IRecordInfo Implementation
78 typedef struct IRecordInfoImpl
80 IRecordInfo IRecordInfo_iface;
86 static const IRecordInfoVtbl IRecordInfoImpl_VTable;
88 static inline IRecordInfoImpl *impl_from_IRecordInfo(IRecordInfo *iface)
90 return CONTAINING_RECORD(iface, IRecordInfoImpl, IRecordInfo_iface);
93 static IRecordInfoImpl *IRecordInfoImpl_Construct(void)
97 rec = HeapAlloc(GetProcessHeap(), 0, sizeof(IRecordInfoImpl));
98 rec->IRecordInfo_iface.lpVtbl = &IRecordInfoImpl_VTable;
99 rec->ref = START_REF_COUNT;
100 rec->clearCalled = 0;
105 static ULONG CALLBACK IRecordInfoImpl_AddRef(IRecordInfo *iface)
107 IRecordInfoImpl* This = impl_from_IRecordInfo(iface);
108 return InterlockedIncrement(&This->ref);
111 static ULONG CALLBACK IRecordInfoImpl_Release(IRecordInfo *iface)
113 IRecordInfoImpl* This = impl_from_IRecordInfo(iface);
114 return InterlockedDecrement(&This->ref);
117 static BOOL fail_GetSize; /* Whether to fail the GetSize call */
119 static HRESULT CALLBACK IRecordInfoImpl_RecordClear(IRecordInfo *iface, PVOID pvExisting)
121 IRecordInfoImpl* This = impl_from_IRecordInfo(iface);
126 static HRESULT CALLBACK IRecordInfoImpl_GetSize(IRecordInfo *iface, ULONG* size)
128 IRecordInfoImpl* This = impl_from_IRecordInfo(iface);
137 static HRESULT CALLBACK IRecordInfoImpl_Dummy(IRecordInfo *iface)
139 trace("Called an unexpected IRecordInfo method - please report!\n");
140 /* Quit because we'll just crash anyway */
145 static const IRecordInfoVtbl IRecordInfoImpl_VTable =
147 (PVOID)IRecordInfoImpl_Dummy,
148 IRecordInfoImpl_AddRef,
149 IRecordInfoImpl_Release,
150 (PVOID)IRecordInfoImpl_Dummy,
151 IRecordInfoImpl_RecordClear,
152 (PVOID)IRecordInfoImpl_Dummy,
153 (PVOID)IRecordInfoImpl_Dummy,
154 (PVOID)IRecordInfoImpl_Dummy,
155 IRecordInfoImpl_GetSize,
156 (PVOID)IRecordInfoImpl_Dummy,
157 (PVOID)IRecordInfoImpl_Dummy,
158 (PVOID)IRecordInfoImpl_Dummy,
159 (PVOID)IRecordInfoImpl_Dummy,
160 (PVOID)IRecordInfoImpl_Dummy,
161 (PVOID)IRecordInfoImpl_Dummy,
162 (PVOID)IRecordInfoImpl_Dummy,
163 (PVOID)IRecordInfoImpl_Dummy,
164 (PVOID)IRecordInfoImpl_Dummy,
165 (PVOID)IRecordInfoImpl_Dummy
168 static DWORD SAFEARRAY_GetVTSize(VARTYPE vt)
173 case VT_UI1: return sizeof(BYTE);
176 case VT_UI2: return sizeof(SHORT);
180 case VT_ERROR: return sizeof(LONG);
182 if (HAVE_OLEAUT32_R8)
183 return sizeof(LONG64);
186 if (HAVE_OLEAUT32_I8)
187 return sizeof(LONG64);
190 case VT_UINT: return sizeof(INT);
193 if (HAVE_OLEAUT32_INT_PTR)
194 return sizeof(UINT_PTR);
196 case VT_CY: return sizeof(CY);
197 case VT_DATE: return sizeof(DATE);
198 case VT_BSTR: return sizeof(BSTR);
199 case VT_DISPATCH: return sizeof(LPDISPATCH);
200 case VT_VARIANT: return sizeof(VARIANT);
201 case VT_UNKNOWN: return sizeof(LPUNKNOWN);
203 if (HAVE_OLEAUT32_DECIMAL)
204 return sizeof(DECIMAL);
210 static void check_for_VT_INT_PTR(void)
212 /* Set a global flag if VT_INT_PTR is supported */
215 SAFEARRAYBOUND bound;
218 a = SafeArrayCreate(VT_INT_PTR, 1, &bound);
220 trace("VT_INT_PTR is supported\n");
221 HAVE_OLEAUT32_INT_PTR = TRUE;
225 trace("VT_INT_PTR is not supported\n");
226 HAVE_OLEAUT32_INT_PTR = FALSE;
230 #define VARTYPE_NOT_SUPPORTED 0
233 UINT elemsize; /* elementsize by VT */
234 UINT expflags; /* fFeatures from SafeArrayAllocDescriptorEx */
235 UINT addflags; /* additional fFeatures from SafeArrayCreate */
237 {VT_EMPTY, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
238 {VT_NULL, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
239 {VT_I2, 2, FADF_HAVEVARTYPE,0},
240 {VT_I4, 4, FADF_HAVEVARTYPE,0},
241 {VT_R4, 4, FADF_HAVEVARTYPE,0},
242 {VT_R8, 8, FADF_HAVEVARTYPE,0},
243 {VT_CY, 8, FADF_HAVEVARTYPE,0},
244 {VT_DATE, 8, FADF_HAVEVARTYPE,0},
245 {VT_BSTR, sizeof(BSTR), FADF_HAVEVARTYPE,FADF_BSTR},
246 {VT_DISPATCH, sizeof(LPDISPATCH), FADF_HAVEIID, FADF_DISPATCH},
247 {VT_ERROR, 4, FADF_HAVEVARTYPE,0},
248 {VT_BOOL, 2, FADF_HAVEVARTYPE,0},
249 {VT_VARIANT, sizeof(VARIANT), FADF_HAVEVARTYPE,FADF_VARIANT},
250 {VT_UNKNOWN, sizeof(LPUNKNOWN), FADF_HAVEIID, FADF_UNKNOWN},
251 {VT_DECIMAL, sizeof(DECIMAL), FADF_HAVEVARTYPE,0},
252 {15, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0}, /* no VT_xxx */
253 {VT_I1, 1, FADF_HAVEVARTYPE,0},
254 {VT_UI1, 1, FADF_HAVEVARTYPE,0},
255 {VT_UI2, 2, FADF_HAVEVARTYPE,0},
256 {VT_UI4, 4, FADF_HAVEVARTYPE,0},
257 {VT_I8, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
258 {VT_UI8, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
259 {VT_INT, sizeof(INT), FADF_HAVEVARTYPE,0},
260 {VT_UINT, sizeof(UINT), FADF_HAVEVARTYPE,0},
261 {VT_VOID, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
262 {VT_HRESULT, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
263 {VT_PTR, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
264 {VT_SAFEARRAY,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
265 {VT_CARRAY, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
266 {VT_USERDEFINED,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
267 {VT_LPSTR, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
268 {VT_LPWSTR, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
269 {VT_FILETIME, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
270 {VT_RECORD, VARTYPE_NOT_SUPPORTED,FADF_RECORD,0},
271 {VT_BLOB, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
272 {VT_STREAM, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
273 {VT_STORAGE, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
274 {VT_STREAMED_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
275 {VT_STORED_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
276 {VT_BLOB_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
277 {VT_CF, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
278 {VT_CLSID, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
281 static void test_safearray(void)
284 unsigned int i, diff;
287 SAFEARRAYBOUND bound, bounds[2];
293 unsigned char *ptr1, *ptr2;
295 hres = SafeArrayDestroy( NULL);
296 ok( hres == S_OK, "SafeArrayDestroy( NULL) returned 0x%x\n", hres);
300 a = SafeArrayCreate(-1, 1, &bound);
301 ok(NULL == a,"SAC(-1,1,[1,0]) not failed?\n");
305 a = SafeArrayCreate(VT_I4, 1, &bound);
306 ok(NULL != a,"SAC(VT_I4,1,[0,0]) failed.\n");
308 hres = SafeArrayGetLBound(a, 1, &l);
309 ok(hres == S_OK, "SAGLB of 0 size dimensioned array failed with %x\n",hres);
310 ok(l == 42, "SAGLB of 0 size dimensioned array failed to return 42, but returned %d\n",l);
311 hres = SafeArrayGetUBound(a, 1, &l);
312 ok(hres == S_OK, "SAGUB of 0 size dimensioned array failed with %x\n",hres);
313 ok(l == 41, "SAGUB of 0 size dimensioned array failed to return 41, but returned %d\n",l);
315 hres = SafeArrayAccessData(a, &data);
316 ok(hres == S_OK, "SafeArrayAccessData of 0 size dimensioned array failed with %x\n", hres);
317 SafeArrayUnaccessData(a);
320 hres = SafeArrayRedim(a, &bound);
321 ok(hres == S_OK,"SAR of a 0 elements dimension failed with hres %x\n", hres);
323 hres = SafeArrayRedim(a, &bound);
324 ok(hres == S_OK || hres == E_OUTOFMEMORY,
325 "SAR to a 0 elements dimension failed with hres %x\n", hres);
326 hres = SafeArrayDestroy(a);
327 ok(hres == S_OK,"SAD of 0 dim array faild with hres %x\n", hres);
329 SafeArrayAllocDescriptor(2, &a);
330 a->rgsabound[0].cElements = 2;
331 a->rgsabound[0].lLbound = 1;
332 a->rgsabound[1].cElements = 4;
333 a->rgsabound[1].lLbound = 1;
335 SafeArrayAllocData(a);
339 hres = SafeArrayPtrOfIndex(a, indices, (void **)&ptr1);
340 ok(hres == S_OK, "SAPOI failed with hres %x\n", hres);
341 SafeArrayAccessData(a, (void **)&ptr2);
342 ok(ptr1 - ptr2 == 14, "SAPOI got wrong ptr\n");
343 *(WORD *)ptr1 = 0x55aa;
344 SafeArrayUnaccessData(a);
346 bound.cElements = 10;
348 SafeArrayRedim(a, &bound);
350 SafeArrayPtrOfIndex(a, indices, (void **)&ptr1);
351 ok(*(WORD *)ptr1 == 0x55aa, "Data not preserved when resizing array\n");
353 bound.cElements = 10;
355 SafeArrayRedim(a, &bound);
356 SafeArrayPtrOfIndex(a, indices, (void **)&ptr1);
357 ok(*(WORD *)ptr1 == 0, "Expanded area not zero-initialized\n");
360 SafeArrayPtrOfIndex(a, indices, (void **)&ptr1);
361 ok(*(WORD *)ptr1 == 0x55aa, "Data not preserved when resizing array\n");
363 hres = SafeArrayDestroy(a);
364 ok(hres == S_OK,"SAD faild with hres %x\n", hres);
366 bounds[0].cElements = 0; bounds[0].lLbound = 1;
367 bounds[1].cElements = 2; bounds[1].lLbound = 23;
368 a = SafeArrayCreate(VT_I4,2,bounds);
369 ok(a != NULL,"SAC(VT_INT32,2,...) with 0 element dim failed.\n");
371 hres = SafeArrayDestroy(a);
372 ok(hres == S_OK,"SAD faild with hres %x\n", hres);
373 bounds[0].cElements = 1; bounds[0].lLbound = 1;
374 bounds[1].cElements = 0; bounds[1].lLbound = 23;
375 a = SafeArrayCreate(VT_I4,2,bounds);
376 ok(a != NULL,"SAC(VT_INT32,2,...) with 0 element dim failed.\n");
378 hres = SafeArrayDestroy(a);
379 ok(hres == S_OK,"SAD faild with hres %x\n", hres);
381 bounds[0].cElements = 42; bounds[0].lLbound = 1;
382 bounds[1].cElements = 2; bounds[1].lLbound = 23;
383 a = SafeArrayCreate(VT_I4,2,bounds);
384 ok(a != NULL,"SAC(VT_INT32,2,...) failed.\n");
386 hres = SafeArrayGetLBound (a, 0, &l);
387 ok (hres == DISP_E_BADINDEX, "SAGLB 0 failed with %x\n", hres);
388 hres = SafeArrayGetLBound (a, 1, &l);
389 ok (hres == S_OK, "SAGLB 1 failed with %x\n", hres);
390 ok (l == 1, "SAGLB 1 returned %d instead of 1\n", l);
391 hres = SafeArrayGetLBound (a, 2, &l);
392 ok (hres == S_OK, "SAGLB 2 failed with %x\n", hres);
393 ok (l == 23, "SAGLB 2 returned %d instead of 23\n", l);
394 hres = SafeArrayGetLBound (a, 3, &l);
395 ok (hres == DISP_E_BADINDEX, "SAGLB 3 failed with %x\n", hres);
397 hres = SafeArrayGetUBound (a, 0, &l);
398 ok (hres == DISP_E_BADINDEX, "SAGUB 0 failed with %x\n", hres);
399 hres = SafeArrayGetUBound (a, 1, &l);
400 ok (hres == S_OK, "SAGUB 1 failed with %x\n", hres);
401 ok (l == 42, "SAGUB 1 returned %d instead of 42\n", l);
402 hres = SafeArrayGetUBound (a, 2, &l);
403 ok (hres == S_OK, "SAGUB 2 failed with %x\n", hres);
404 ok (l == 24, "SAGUB 2 returned %d instead of 24\n", l);
405 hres = SafeArrayGetUBound (a, 3, &l);
406 ok (hres == DISP_E_BADINDEX, "SAGUB 3 failed with %x\n", hres);
408 i = SafeArrayGetDim(a);
409 ok(i == 2, "getdims of 2 din array returned %d\n",i);
413 hres = SafeArrayGetElement(a, indices, &i);
414 ok(DISP_E_BADINDEX == hres,"SAGE failed [0,23], hres 0x%x\n",hres);
418 hres = SafeArrayGetElement(a, indices, &i);
419 ok(DISP_E_BADINDEX == hres,"SAGE failed [1,22], hres 0x%x\n",hres);
423 hres = SafeArrayGetElement(a, indices, &i);
424 ok(S_OK == hres,"SAGE failed [1,23], hres 0x%x\n",hres);
428 hres = SafeArrayGetElement(a, indices, &i);
429 ok(DISP_E_BADINDEX == hres,"SAGE failed [1,24], hres 0x%x\n",hres);
433 hres = SafeArrayGetElement(a, indices, &i);
434 ok(S_OK == hres,"SAGE failed [42,23], hres 0x%x\n",hres);
436 hres = SafeArrayAccessData(a, (void**)&ptr1);
437 ok(S_OK == hres, "SAAD failed with 0x%x\n", hres);
441 hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
442 ok(S_OK == hres,"SAPOI failed [1,23], hres 0x%x\n",hres);
444 ok(diff == 8,"ptr difference is not 8, but %d (%p vs %p)\n", diff, ptr2, ptr1);
448 hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
449 ok(S_OK == hres,"SAPOI failed [5,24], hres 0x%x\n",hres);
451 ok(diff == 176,"ptr difference is not 176, but %d (%p vs %p)\n", diff, ptr2, ptr1);
455 hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
456 ok(S_OK == hres,"SAPOI failed [20,23], hres 0x%x\n",hres);
458 ok(diff == 76,"ptr difference is not 76, but %d (%p vs %p)\n", diff, ptr2, ptr1);
460 hres = SafeArrayUnaccessData(a);
461 ok(S_OK == hres, "SAUAD failed with 0x%x\n", hres);
463 hres = SafeArrayDestroy(a);
464 ok(hres == S_OK,"SAD faild with hres %x\n", hres);
466 for (i=0;i<sizeof(vttypes)/sizeof(vttypes[0]);i++) {
467 if ((i == VT_I8 || i == VT_UI8) && HAVE_OLEAUT32_I8)
469 vttypes[i].elemsize = sizeof(LONG64);
472 a = SafeArrayCreate(vttypes[i].vt, 1, &bound);
474 ok((!a && !vttypes[i].elemsize) ||
475 (a && vttypes[i].elemsize == a->cbElements) ||
476 (IS_ANCIENT && (vttypes[i].vt == VT_DECIMAL || vttypes[i].vt == VT_I1 ||
477 vttypes[i].vt == VT_UI2 || vttypes[i].vt == VT_UI4 || vttypes[i].vt == VT_INT ||
478 vttypes[i].vt == VT_UINT)),
479 "SAC(%d,1,[1,0]), %p result %d, expected %d\n",
480 vttypes[i].vt,a,(a?a->cbElements:0),vttypes[i].elemsize);
484 if (!HAVE_OLEAUT32_RECORD)
485 vttypes[i].expflags = 0;
486 ok(a->fFeatures == (vttypes[i].expflags | vttypes[i].addflags),
487 "SAC of %d returned feature flags %x, expected %x\n",
488 vttypes[i].vt, a->fFeatures,
489 vttypes[i].expflags|vttypes[i].addflags);
490 ok(SafeArrayGetElemsize(a) == vttypes[i].elemsize,
491 "SAGE for vt %d returned elemsize %d instead of expected %d\n",
492 vttypes[i].vt, SafeArrayGetElemsize(a),vttypes[i].elemsize);
497 if (pSafeArrayGetVartype)
499 hres = pSafeArrayGetVartype(a, &vt);
500 ok(hres == S_OK, "SAGVT of arra y with vt %d failed with %x\n", vttypes[i].vt, hres);
501 /* Windows prior to Vista returns VT_UNKNOWN instead of VT_DISPATCH */
502 ok(broken(vt == VT_UNKNOWN) || vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d\n", vttypes[i].vt, vt);
505 hres = SafeArrayCopy(a, &c);
506 ok(hres == S_OK, "failed to copy safearray of vt %d with hres %x\n", vttypes[i].vt, hres);
508 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
510 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);
511 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);
513 if (pSafeArrayGetVartype) {
514 hres = pSafeArrayGetVartype(c, &vt);
515 ok(hres == S_OK, "SAGVT of array with vt %d failed with %x\n", vttypes[i].vt, hres);
516 /* Windows prior to Vista returns VT_UNKNOWN instead of VT_DISPATCH */
517 ok(broken(vt == VT_UNKNOWN) || vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d\n", vttypes[i].vt, vt);
520 if (pSafeArrayCopyData) {
521 hres = pSafeArrayCopyData(a, c);
522 ok(hres == S_OK, "failed to copy safearray data of vt %d with hres %x\n", vttypes[i].vt, hres);
524 hres = SafeArrayDestroyData(c);
525 ok(hres == S_OK,"SADD of copy of array with vt %d failed with hres %x\n", vttypes[i].vt, hres);
528 hres = SafeArrayDestroy(c);
529 ok(hres == S_OK,"SAD faild with hres %x\n", hres);
531 hres = SafeArrayDestroy(a);
532 ok(hres == S_OK,"SAD of array with vt %d failed with hres %x\n", vttypes[i].vt, hres);
535 /* Test conversion of type|VT_ARRAY <-> VT_BSTR */
537 bound.cElements = 10;
538 a = SafeArrayCreate(VT_UI1, 1, &bound);
539 ok(a != NULL, "SAC failed.\n");
540 ok(S_OK == SafeArrayAccessData(a, &data),"SACD failed\n");
541 memcpy(data,"Hello World\n",10);
542 ok(S_OK == SafeArrayUnaccessData(a),"SAUD failed\n");
543 V_VT(&v) = VT_ARRAY|VT_UI1;
545 hres = VariantChangeTypeEx(&v, &v, 0, 0, VT_BSTR);
546 ok(hres==S_OK, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR failed with %x\n",hres);
547 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));
548 ok(V_BSTR(&v)[0] == 0x6548,"First letter are not 'He', but %x\n", V_BSTR(&v)[0]);
551 /* check locking functions */
552 a = SafeArrayCreate(VT_I4, 1, &bound);
553 ok(a!=NULL,"SAC should not fail\n");
555 hres = SafeArrayAccessData(a, &data);
556 ok(hres == S_OK,"SAAD failed with hres %x\n",hres);
558 hres = SafeArrayDestroy(a);
559 ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy not failed with DISP_E_ARRAYISLOCKED, but with hres %x\n", hres);
561 hres = SafeArrayDestroyData(a);
562 ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy data not failed with DISP_E_ARRAYISLOCKED, but with hres %x\n", hres);
564 hres = SafeArrayDestroyDescriptor(a);
565 ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy descriptor not failed with DISP_E_ARRAYISLOCKED, but with hres %x\n", hres);
567 hres = SafeArrayUnaccessData(a);
568 ok(hres == S_OK,"SAUD failed after lock/destroy test\n");
570 hres = SafeArrayDestroy(a);
571 ok(hres == S_OK,"SAD failed after lock/destroy test\n");
573 /* Test if we need to destroy data before descriptor */
574 a = SafeArrayCreate(VT_I4, 1, &bound);
575 ok(a!=NULL,"SAC should not fail\n");
576 hres = SafeArrayDestroyDescriptor(a);
577 ok(hres == S_OK,"SADD with data in array failed with hres %x\n",hres);
580 /* init a small stack safearray */
581 if (pSafeArraySetIID) {
582 memset(&b, 0, sizeof(b));
584 memset(&iid, 0x42, sizeof(IID));
585 hres = pSafeArraySetIID(&b,&iid);
586 ok(hres == E_INVALIDARG,"SafeArraySetIID of non IID capable safearray did not return E_INVALIDARG, but %x\n",hres);
588 hres = SafeArrayAllocDescriptor(1,&a);
589 ok((a->fFeatures & FADF_HAVEIID) == 0,"newly allocated descriptor with SAAD should not have FADF_HAVEIID\n");
590 hres = pSafeArraySetIID(a,&iid);
591 ok(hres == E_INVALIDARG,"SafeArraySetIID of newly allocated descriptor with SAAD should return E_INVALIDARG, but %x\n",hres);
593 hres = SafeArrayDestroyDescriptor(a);
594 ok(hres == S_OK,"SADD failed with hres %x\n",hres);
597 if (!pSafeArrayAllocDescriptorEx)
600 for (i=0;i<sizeof(vttypes)/sizeof(vttypes[0]);i++) {
602 hres = pSafeArrayAllocDescriptorEx(vttypes[i].vt,1,&a);
603 ok(a->fFeatures == vttypes[i].expflags,"SAADE(%d) resulted with flags %x, expected %x\n", vttypes[i].vt, a->fFeatures, vttypes[i].expflags);
604 if (a->fFeatures & FADF_HAVEIID) {
605 hres = pSafeArrayGetIID(a, &iid);
606 ok(hres == S_OK,"SAGIID failed for vt %d with hres %x\n", vttypes[i].vt,hres);
607 switch (vttypes[i].vt) {
609 ok(IsEqualGUID(((GUID*)a)-1,&IID_IUnknown),"guid for VT_UNKNOWN is not IID_IUnknown\n");
610 ok(IsEqualGUID(&iid, &IID_IUnknown),"SAGIID returned wrong GUID for IUnknown\n");
613 ok(IsEqualGUID(((GUID*)a)-1,&IID_IDispatch),"guid for VT_UNKNOWN is not IID_IDispatch\n");
614 ok(IsEqualGUID(&iid, &IID_IDispatch),"SAGIID returned wrong GUID for IDispatch\n");
617 ok(FALSE,"unknown vt %d with FADF_HAVEIID\n",vttypes[i].vt);
621 hres = pSafeArrayGetIID(a, &iid);
622 ok(hres == E_INVALIDARG,"SAGIID did not fail for vt %d with hres %x\n", vttypes[i].vt,hres);
624 if (a->fFeatures & FADF_RECORD) {
625 ok(vttypes[i].vt == VT_RECORD,"FADF_RECORD for non record %d\n",vttypes[i].vt);
627 if (a->fFeatures & FADF_HAVEVARTYPE) {
628 ok(vttypes[i].vt == ((DWORD*)a)[-1], "FADF_HAVEVARTYPE set, but vt %d mismatch stored %d\n",vttypes[i].vt,((DWORD*)a)[-1]);
631 hres = pSafeArrayGetVartype(a, &vt);
632 ok(hres == S_OK, "SAGVT of array with vt %d failed with %x\n", vttypes[i].vt, hres);
634 if (vttypes[i].vt == VT_DISPATCH) {
635 /* Special case. Checked against Windows. */
636 ok(vt == VT_UNKNOWN, "SAGVT of array with VT_DISPATCH returned not VT_UNKNOWN, but %d\n", vt);
638 ok(vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d\n", vttypes[i].vt, vt);
641 if (a->fFeatures & FADF_HAVEIID) {
642 hres = pSafeArraySetIID(a, &IID_IStorage); /* random IID */
643 ok(hres == S_OK,"SASIID failed with FADF_HAVEIID set for vt %d with %x\n", vttypes[i].vt, hres);
644 hres = pSafeArrayGetIID(a, &iid);
645 ok(hres == S_OK,"SAGIID failed with FADF_HAVEIID set for vt %d with %x\n", vttypes[i].vt, hres);
646 ok(IsEqualGUID(&iid, &IID_IStorage),"returned iid is not IID_IStorage\n");
648 hres = pSafeArraySetIID(a, &IID_IStorage); /* random IID */
649 ok(hres == E_INVALIDARG,"SASIID did not failed with !FADF_HAVEIID set for vt %d with %x\n", vttypes[i].vt, hres);
651 hres = SafeArrayDestroyDescriptor(a);
652 ok(hres == S_OK,"SADD failed with hres %x\n",hres);
656 static void test_SafeArrayAllocDestroyDescriptor(void)
663 hres = SafeArrayAllocDescriptor(0, &sa);
664 ok(hres == E_INVALIDARG, "0 dimensions gave hres 0x%x\n", hres);
666 hres = SafeArrayAllocDescriptor(65536, &sa);
667 ok(IS_ANCIENT || hres == E_INVALIDARG,
668 "65536 dimensions gave hres 0x%x\n", hres);
672 /* Crashes on 95: XP & Wine return E_POINTER */
673 hres=SafeArrayAllocDescriptor(1, NULL);
674 ok(hres == E_POINTER,"NULL parm gave hres 0x%x\n", hres);
677 /* Test up to the dimension boundary case */
678 for (i = 5; i <= 65535; i += 30)
680 hres = SafeArrayAllocDescriptor(i, &sa);
681 ok(hres == S_OK, "%d dimensions failed; hres 0x%x\n", i, hres);
685 ok(SafeArrayGetDim(sa) == (UINT)i, "Dimension is %d; should be %d\n",
686 SafeArrayGetDim(sa), i);
688 hres = SafeArrayDestroyDescriptor(sa);
689 ok(hres == S_OK, "destroy failed; hres 0x%x\n", hres);
693 if (!pSafeArrayAllocDescriptorEx)
696 hres = pSafeArrayAllocDescriptorEx(VT_UI1, 0, &sa);
697 ok(hres == E_INVALIDARG, "0 dimensions gave hres 0x%x\n", hres);
699 hres = pSafeArrayAllocDescriptorEx(VT_UI1, 65536, &sa);
700 ok(hres == E_INVALIDARG, "65536 dimensions gave hres 0x%x\n", hres);
702 hres = pSafeArrayAllocDescriptorEx(VT_UI1, 1, NULL);
703 ok(hres == E_POINTER,"NULL parm gave hres 0x%x\n", hres);
705 hres = pSafeArrayAllocDescriptorEx(-1, 1, &sa);
706 ok(hres == S_OK, "VT = -1 gave hres 0x%x\n", hres);
708 sa->rgsabound[0].cElements = 0;
709 sa->rgsabound[0].lLbound = 1;
711 hres = SafeArrayAllocData(sa);
712 ok(hres == S_OK, "SafeArrayAllocData gave hres 0x%x\n", hres);
714 hres = SafeArrayDestroy(sa);
715 ok(hres == S_OK,"SafeArrayDestroy failed with hres %x\n",hres);
718 static void test_SafeArrayCreateLockDestroy(void)
720 SAFEARRAYBOUND sab[4];
726 #define NUM_DIMENSIONS (int)(sizeof(sab) / sizeof(sab[0]))
728 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
730 sab[dimension].lLbound = 0;
731 sab[dimension].cElements = 8;
735 /* This test crashes very early versions with no error checking...
736 sa = SafeArrayCreate(VT_UI1, 1, NULL);
737 ok(sa == NULL, "NULL bounds didn't fail\n");
739 sa = SafeArrayCreate(VT_UI1, 65536, sab);
740 ok(IS_ANCIENT || !sa, "Max bounds didn't fail\n");
742 memset(sab, 0, sizeof(sab));
744 /* Don't test 0 sized dimensions, as Windows has a bug which allows this */
746 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
747 sab[dimension].cElements = 8;
749 /* Test all VARTYPES in 1-4 dimensions */
750 for (dimension = 1; dimension < 4; dimension++)
752 for (vt = VT_EMPTY; vt < VT_CLSID; vt++)
754 DWORD dwLen = SAFEARRAY_GetVTSize(vt);
756 sa = SafeArrayCreate(vt, dimension, sab);
759 ok(sa || (IS_ANCIENT && (vt == VT_DECIMAL || vt == VT_I1 || vt == VT_UI2 ||
760 vt == VT_UI4 || vt == VT_INT || vt == VT_UINT)),
761 "VARTYPE %d (@%d dimensions) failed\n", vt, dimension);
763 ok(sa == NULL || vt == VT_R8,
764 "VARTYPE %d (@%d dimensions) succeeded!\n", vt, dimension);
768 ok(SafeArrayGetDim(sa) == (UINT)dimension,
769 "VARTYPE %d (@%d dimensions) cDims is %d, expected %d\n",
770 vt, dimension, SafeArrayGetDim(sa), dimension);
771 ok(SafeArrayGetElemsize(sa) == dwLen || vt == VT_R8,
772 "VARTYPE %d (@%d dimensions) cbElements is %d, expected %d\n",
773 vt, dimension, SafeArrayGetElemsize(sa), dwLen);
775 if (vt != VT_UNKNOWN && vt != VT_DISPATCH)
777 ok((sa->fFeatures & FADF_HAVEIID) == 0,
778 "Non interface type should not have FADF_HAVEIID\n");
779 if (pSafeArraySetIID)
781 hres = pSafeArraySetIID(sa, &IID_IUnknown);
782 ok(hres == E_INVALIDARG,
783 "Non interface type allowed SetIID(), hres %x\n", hres);
789 ok(IS_ANCIENT || sa->fFeatures & FADF_HAVEVARTYPE,
790 "Non interface type should have FADF_HAVEVARTYPE\n");
791 if (pSafeArrayGetVartype)
793 hres = pSafeArrayGetVartype(sa, &aVt);
794 ok(hres == S_OK && aVt == vt,
795 "Non interface type %d: bad type %d, hres %x\n", vt, aVt, hres);
801 ok(IS_ANCIENT || sa->fFeatures & FADF_HAVEIID,
802 "Interface type should have FADF_HAVEIID\n");
803 if (pSafeArraySetIID)
805 hres = pSafeArraySetIID(sa, &IID_IUnknown);
807 "Non interface type disallowed SetIID(), hres %x\n", hres);
809 ok((sa->fFeatures & FADF_HAVEVARTYPE) == 0,
810 "Interface type %d should not have FADF_HAVEVARTYPE\n", vt);
813 hres = SafeArrayLock(sa);
814 ok(hres == S_OK, "Lock VARTYPE %d (@%d dimensions) failed; hres 0x%x\n",
815 vt, dimension, hres);
819 hres = SafeArrayDestroy(sa);
820 ok(hres == DISP_E_ARRAYISLOCKED,"Destroy() got hres %x\n", hres);
822 hres = SafeArrayDestroyData(sa);
823 ok(hres == DISP_E_ARRAYISLOCKED,"DestroyData() got hres %x\n", hres);
825 hres = SafeArrayDestroyDescriptor(sa);
826 ok(hres == DISP_E_ARRAYISLOCKED,"DestroyDescriptor() got hres %x\n", hres);
828 hres = SafeArrayUnlock(sa);
829 ok(hres == S_OK, "Unlock VARTYPE %d (@%d dims) hres 0x%x\n",
830 vt, dimension, hres);
832 hres = SafeArrayDestroy(sa);
833 ok(hres == S_OK, "destroy VARTYPE %d (@%d dims) hres 0x%x\n",
834 vt, dimension, hres);
841 static void test_VectorCreateLockDestroy(void)
848 if (!pSafeArrayCreateVector)
850 sa = pSafeArrayCreateVector(VT_UI1, 0, 0);
851 ok(sa != NULL, "SACV with 0 elements failed.\n");
853 hres = SafeArrayDestroy(sa);
854 ok(hres == S_OK, "SafeArrayDestroy failed with hres %x\n",hres);
856 /* Test all VARTYPES in different lengths */
857 for (element = 1; element <= 101; element += 10)
859 for (vt = VT_EMPTY; vt < VT_CLSID; vt++)
861 DWORD dwLen = SAFEARRAY_GetVTSize(vt);
863 sa = pSafeArrayCreateVector(vt, 0, element);
866 ok(sa != NULL, "VARTYPE %d (@%d elements) failed\n", vt, element);
868 ok(sa == NULL, "VARTYPE %d (@%d elements) succeeded!\n", vt, element);
872 ok(SafeArrayGetDim(sa) == 1, "VARTYPE %d (@%d elements) cDims %d, not 1\n",
873 vt, element, SafeArrayGetDim(sa));
874 ok(SafeArrayGetElemsize(sa) == dwLen,
875 "VARTYPE %d (@%d elements) cbElements is %d, expected %d\n",
876 vt, element, SafeArrayGetElemsize(sa), dwLen);
878 hres = SafeArrayLock(sa);
879 ok(hres == S_OK, "Lock VARTYPE %d (@%d elements) failed; hres 0x%x\n",
884 hres = SafeArrayUnlock(sa);
885 ok(hres == S_OK, "Unlock VARTYPE %d (@%d elements) failed; hres 0x%x\n",
888 hres = SafeArrayDestroy(sa);
889 ok(hres == S_OK, "destroy VARTYPE %d (@%d elements) failed; hres 0x%x\n",
897 static void test_LockUnlock(void)
899 SAFEARRAYBOUND sab[4];
902 BOOL bVector = FALSE;
906 hres = SafeArrayLock(NULL);
907 ok(hres == E_INVALIDARG, "Lock NULL array hres 0x%x\n", hres);
908 hres = SafeArrayUnlock(NULL);
909 ok(hres == E_INVALIDARG, "Lock NULL array hres 0x%x\n", hres);
911 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
913 sab[dimension].lLbound = 0;
914 sab[dimension].cElements = 8;
917 sa = SafeArrayCreate(VT_UI1, NUM_DIMENSIONS, sab);
919 /* Test maximum locks */
920 test_LockUnlock_Vector:
925 hres = SafeArrayUnlock(sa);
926 ok (hres == E_UNEXPECTED, "Bad %sUnlock gave hres 0x%x\n",
927 bVector ? "vector " : "\n", hres);
929 while ((hres = SafeArrayLock(sa)) == S_OK)
931 ok (count == 65535 && hres == E_UNEXPECTED, "Lock %sfailed at %d; hres 0x%x\n",
932 bVector ? "vector " : "\n", count, hres);
934 if (count == 65535 && hres == E_UNEXPECTED)
936 while ((hres = SafeArrayUnlock(sa)) == S_OK)
938 ok (count == 0 && hres == E_UNEXPECTED, "Unlock %sfailed at %d; hres 0x%x\n",
939 bVector ? "vector " : "\n", count, hres);
942 SafeArrayDestroy(sa);
945 if (bVector == FALSE && pSafeArrayCreateVector)
947 /* Test again with a vector */
948 sa = pSafeArrayCreateVector(VT_UI1, 0, 100);
950 goto test_LockUnlock_Vector;
954 static void test_SafeArrayGetPutElement(void)
956 SAFEARRAYBOUND sab[4];
957 LONG indices[NUM_DIMENSIONS];
960 int value = 0, gotvalue, dimension;
961 unsigned int x,y,z,a;
963 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
965 sab[dimension].lLbound = dimension * 2 + 1;
966 sab[dimension].cElements = dimension * 3 + 1;
969 sa = SafeArrayCreate(VT_INT, NUM_DIMENSIONS, sab);
971 return; /* Some early versions can't handle > 3 dims */
973 ok(sa->cbElements == sizeof(value), "int size mismatch\n");
974 if (sa->cbElements != sizeof(value))
978 for (x = 0; x < NUM_DIMENSIONS; x++)
980 indices[0] = sab[0].lLbound;
981 indices[1] = sab[1].lLbound;
982 indices[2] = sab[2].lLbound;
983 indices[3] = sab[3].lLbound;
985 indices[x] = indices[x] - 1;
986 hres = SafeArrayPutElement(sa, indices, &value);
987 ok(hres == DISP_E_BADINDEX, "Put allowed too small index in dimension %d\n", x);
988 hres = SafeArrayGetElement(sa, indices, &value);
989 ok(hres == DISP_E_BADINDEX, "Get allowed too small index in dimension %d\n", x);
991 indices[x] = sab[x].lLbound + sab[x].cElements;
992 hres = SafeArrayPutElement(sa, indices, &value);
993 ok(hres == DISP_E_BADINDEX, "Put allowed too big index in dimension %d\n", x);
994 hres = SafeArrayGetElement(sa, indices, &value);
995 ok(hres == DISP_E_BADINDEX, "Get allowed too big index in dimension %d\n", x);
998 indices[0] = sab[0].lLbound;
999 indices[1] = sab[1].lLbound;
1000 indices[2] = sab[2].lLbound;
1001 indices[3] = sab[3].lLbound;
1003 hres = SafeArrayPutElement(NULL, indices, &value);
1004 ok(hres == E_INVALIDARG, "Put NULL array hres 0x%x\n", hres);
1005 hres = SafeArrayGetElement(NULL, indices, &value);
1006 ok(hres == E_INVALIDARG, "Get NULL array hres 0x%x\n", hres);
1008 hres = SafeArrayPutElement(sa, NULL, &value);
1009 ok(hres == E_INVALIDARG, "Put NULL indices hres 0x%x\n", hres);
1010 hres = SafeArrayGetElement(sa, NULL, &value);
1011 ok(hres == E_INVALIDARG, "Get NULL indices hres 0x%x\n", hres);
1015 /* This is retarded. Windows checks every case of invalid parameters
1016 * except the following, which crashes. We ERR this in Wine.
1018 hres = SafeArrayPutElement(sa, indices, NULL);
1019 ok(hres == E_INVALIDARG, "Put NULL value hres 0x%x\n", hres);
1022 hres = SafeArrayGetElement(sa, indices, NULL);
1023 ok(hres == E_INVALIDARG, "Get NULL value hres 0x%x\n", hres);
1027 /* Make sure we can read and get back the correct values in 4 dimensions,
1028 * Each with a different size and lower bound.
1030 for (x = 0; x < sab[0].cElements; x++)
1032 indices[0] = sab[0].lLbound + x;
1033 for (y = 0; y < sab[1].cElements; y++)
1035 indices[1] = sab[1].lLbound + y;
1036 for (z = 0; z < sab[2].cElements; z++)
1038 indices[2] = sab[2].lLbound + z;
1039 for (a = 0; a < sab[3].cElements; a++)
1041 indices[3] = sab[3].lLbound + a;
1042 hres = SafeArrayPutElement(sa, indices, &value);
1043 ok(hres == S_OK, "Failed to put element at (%d,%d,%d,%d) hres 0x%x\n",
1053 for (x = 0; x < sab[0].cElements; x++)
1055 indices[0] = sab[0].lLbound + x;
1056 for (y = 0; y < sab[1].cElements; y++)
1058 indices[1] = sab[1].lLbound + y;
1059 for (z = 0; z < sab[2].cElements; z++)
1061 indices[2] = sab[2].lLbound + z;
1062 for (a = 0; a < sab[3].cElements; a++)
1064 indices[3] = sab[3].lLbound + a;
1065 gotvalue = value / 3;
1066 hres = SafeArrayGetElement(sa, indices, &gotvalue);
1067 ok(hres == S_OK, "Failed to get element at (%d,%d,%d,%d) hres 0x%x\n",
1070 ok(value == gotvalue, "Got value %d instead of %d at (%d,%d,%d,%d)\n",
1071 gotvalue, value, x, y, z, a);
1077 SafeArrayDestroy(sa);
1080 static void test_SafeArrayGetPutElement_BSTR(void)
1086 BSTR value = 0, gotvalue;
1087 const OLECHAR szTest[5] = { 'T','e','s','t','\0' };
1092 sa = SafeArrayCreate(VT_BSTR, 1, &sab);
1093 ok(sa != NULL, "BSTR test couldn't create array\n");
1097 ok(sa->cbElements == sizeof(BSTR), "BSTR size mismatch\n");
1098 if (sa->cbElements != sizeof(BSTR))
1101 indices[0] = sab.lLbound;
1102 value = SysAllocString(szTest);
1103 ok (value != NULL, "Expected non-NULL\n");
1104 hres = SafeArrayPutElement(sa, indices, value);
1105 ok(hres == S_OK, "Failed to put bstr element hres 0x%x\n", hres);
1107 hres = SafeArrayGetElement(sa, indices, &gotvalue);
1108 ok(hres == S_OK, "Failed to get bstr element at hres 0x%x\n", hres);
1110 ok(SysStringLen(value) == SysStringLen(gotvalue), "Got len %d instead of %d\n", SysStringLen(gotvalue), SysStringLen(value));
1111 SafeArrayDestroy(sa);
1112 SysFreeString(value);
1113 SysFreeString(gotvalue);
1116 static int tunk_xref = 0;
1117 static HRESULT WINAPI tunk_QueryInterface(LPUNKNOWN punk,REFIID riid, LPVOID *x) {
1120 static ULONG WINAPI tunk_AddRef(LPUNKNOWN punk) {
1124 static ULONG WINAPI tunk_Release(LPUNKNOWN punk) {
1128 static const IUnknownVtbl xtunk_vtbl = {
1129 tunk_QueryInterface,
1134 static struct xtunk_iface {
1135 const IUnknownVtbl *lpvtbl;
1139 static void test_SafeArrayGetPutElement_IUnknown(void)
1145 LPUNKNOWN value = 0, gotvalue;
1149 sa = SafeArrayCreate(VT_UNKNOWN, 1, &sab);
1150 ok(sa != NULL, "UNKNOWN test couldn't create array\n");
1154 ok(sa->cbElements == sizeof(LPUNKNOWN), "LPUNKNOWN size mismatch\n");
1155 if (sa->cbElements != sizeof(LPUNKNOWN))
1158 indices[0] = sab.lLbound;
1159 xtunk_iface.lpvtbl = &xtunk_vtbl;
1160 value = (LPUNKNOWN)&xtunk_iface;
1162 ok (value != NULL, "Expected non-NULL\n");
1163 hres = SafeArrayPutElement(sa, indices, value);
1164 ok(hres == S_OK, "Failed to put bstr element hres 0x%x\n", hres);
1165 ok(tunk_xref == 2,"Failed to increment refcount of iface.\n");
1167 hres = SafeArrayGetElement(sa, indices, &gotvalue);
1168 ok(tunk_xref == 3,"Failed to increment refcount of iface.\n");
1169 ok(hres == S_OK, "Failed to get bstr element at hres 0x%x\n", hres);
1171 ok(value == gotvalue, "Got %p instead of %p\n", gotvalue, value);
1172 SafeArrayDestroy(sa);
1173 ok(tunk_xref == 2,"Failed to decrement refcount of iface.\n");
1176 static void test_SafeArrayRedim_IUnknown(void)
1186 sa = SafeArrayCreate(VT_UNKNOWN, 1, &sab);
1187 ok(sa != NULL, "UNKNOWN test couldn't create array\n");
1191 ok(sa->cbElements == sizeof(LPUNKNOWN), "LPUNKNOWN size mismatch\n");
1192 if (sa->cbElements != sizeof(LPUNKNOWN))
1196 xtunk_iface.lpvtbl = &xtunk_vtbl;
1197 value = (LPUNKNOWN)&xtunk_iface;
1199 hres = SafeArrayPutElement(sa, indices, value);
1200 ok(hres == S_OK, "Failed to put IUnknown element hres 0x%x\n", hres);
1201 ok(tunk_xref == 2,"Failed to increment refcount of iface.\n");
1203 hres = SafeArrayRedim(sa, &sab);
1204 ok(hres == S_OK, "Failed to shrink array hres 0x%x\n", hres);
1205 ok(tunk_xref == 1, "Failed to decrement refcount\n");
1206 SafeArrayDestroy(sa);
1209 static void test_SafeArrayGetPutElement_VARIANT(void)
1215 VARIANT value, gotvalue;
1219 sa = SafeArrayCreate(VT_VARIANT, 1, &sab);
1220 ok(sa != NULL, "VARIANT test couldn't create array\n");
1224 ok(sa->cbElements == sizeof(VARIANT), "VARIANT size mismatch\n");
1225 if (sa->cbElements != sizeof(VARIANT))
1228 indices[0] = sab.lLbound;
1229 V_VT(&value) = VT_I4;
1230 V_I4(&value) = 0x42424242;
1231 hres = SafeArrayPutElement(sa, indices, &value);
1232 ok(hres == S_OK, "Failed to put Variant I4 element hres 0x%x\n", hres);
1234 V_VT(&gotvalue) = 0xdead;
1235 hres = SafeArrayGetElement(sa, indices, &gotvalue);
1236 ok(hres == S_OK, "Failed to get variant element at hres 0x%x\n", hres);
1238 V_VT(&gotvalue) = VT_EMPTY;
1239 hres = SafeArrayGetElement(sa, indices, &gotvalue);
1240 ok(hres == S_OK, "Failed to get variant element at hres 0x%x\n", hres);
1242 ok(V_VT(&value) == V_VT(&gotvalue), "Got type 0x%x instead of 0x%x\n", V_VT(&value), V_VT(&gotvalue));
1243 if (V_VT(&value) == V_VT(&gotvalue))
1244 ok(V_I4(&value) == V_I4(&gotvalue), "Got %d instead of %d\n", V_I4(&value), V_VT(&gotvalue));
1246 SafeArrayDestroy(sa);
1250 static void test_SafeArrayCopyData(void)
1252 SAFEARRAYBOUND sab[4];
1256 int dimension,size=1;
1258 if (!pSafeArrayCopyData)
1261 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
1263 sab[dimension].lLbound = dimension * 2 + 2;
1264 sab[dimension].cElements = dimension * 3 + 1;
1265 size *= sab[dimension].cElements;
1268 sa = SafeArrayCreate(VT_INT, NUM_DIMENSIONS, sab);
1269 ok(sa != NULL, "Copy test couldn't create array\n");
1270 sacopy = SafeArrayCreate(VT_INT, NUM_DIMENSIONS, sab);
1271 ok(sacopy != NULL, "Copy test couldn't create copy array\n");
1276 ok(sa->cbElements == sizeof(int), "int size mismatch\n");
1277 if (sa->cbElements != sizeof(int))
1280 /* Fill the source array with some data; it doesn't matter what */
1281 for (dimension = 0; dimension < size; dimension++)
1283 int* data = sa->pvData;
1284 data[dimension] = dimension;
1287 hres = pSafeArrayCopyData(sa, sacopy);
1288 ok(hres == S_OK, "copy data failed hres 0x%x\n", hres);
1291 ok(!memcmp(sa->pvData, sacopy->pvData, size * sizeof(int)), "compared different\n");
1295 hres = pSafeArrayCopyData(NULL, sacopy);
1296 ok(hres == E_INVALIDARG, "Null copy source hres 0x%x\n", hres);
1297 hres = pSafeArrayCopyData(sa, NULL);
1298 ok(hres == E_INVALIDARG, "Null copy hres 0x%x\n", hres);
1300 sacopy->rgsabound[0].cElements += 1;
1301 hres = pSafeArrayCopyData(sa, sacopy);
1302 ok(hres == E_INVALIDARG, "Bigger copy first dimension hres 0x%x\n", hres);
1304 sacopy->rgsabound[0].cElements -= 2;
1305 hres = pSafeArrayCopyData(sa, sacopy);
1306 ok(hres == E_INVALIDARG, "Smaller copy first dimension hres 0x%x\n", hres);
1307 sacopy->rgsabound[0].cElements += 1;
1309 sacopy->rgsabound[3].cElements += 1;
1310 hres = pSafeArrayCopyData(sa, sacopy);
1311 ok(hres == E_INVALIDARG, "Bigger copy last dimension hres 0x%x\n", hres);
1313 sacopy->rgsabound[3].cElements -= 2;
1314 hres = pSafeArrayCopyData(sa, sacopy);
1315 ok(hres == E_INVALIDARG, "Smaller copy last dimension hres 0x%x\n", hres);
1316 sacopy->rgsabound[3].cElements += 1;
1318 SafeArrayDestroy(sacopy);
1320 hres = pSafeArrayCopyData(sa, sacopy);
1321 ok(hres == E_INVALIDARG, "->Null copy hres 0x%x\n", hres);
1323 hres = SafeArrayCopy(sa, &sacopy);
1324 ok(hres == S_OK, "copy failed hres 0x%x\n", hres);
1327 ok(SafeArrayGetElemsize(sa) == SafeArrayGetElemsize(sacopy),"elemsize wrong\n");
1328 ok(SafeArrayGetDim(sa) == SafeArrayGetDim(sacopy),"dimensions wrong\n");
1329 ok(!memcmp(sa->pvData, sacopy->pvData, size * sizeof(int)), "compared different\n");
1330 SafeArrayDestroy(sacopy);
1333 SafeArrayDestroy(sa);
1336 static void test_SafeArrayCreateEx(void)
1338 IRecordInfoImpl* iRec;
1339 SAFEARRAYBOUND sab[4];
1344 if (!pSafeArrayCreateEx)
1347 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
1349 sab[dimension].lLbound = 0;
1350 sab[dimension].cElements = 8;
1354 sa = pSafeArrayCreateEx(VT_UI1, 1, NULL, NULL);
1355 ok(sa == NULL, "CreateEx NULL bounds didn't fail\n");
1357 /* test IID storage & defaulting */
1358 sa = pSafeArrayCreateEx(VT_DISPATCH, 1, sab, (PVOID)&IID_ITypeInfo);
1359 ok(sa != NULL, "CreateEx (ITypeInfo) failed\n");
1364 if (pSafeArrayGetIID)
1366 hres = pSafeArrayGetIID(sa, &guid);
1367 ok(hres == S_OK, "CreateEx (ITypeInfo) no IID hres 0x%x\n", hres);
1370 ok(IsEqualGUID(&guid, &IID_ITypeInfo), "CreateEx (ITypeInfo) bad IID\n");
1373 if (pSafeArraySetIID)
1375 hres = pSafeArraySetIID(sa, &IID_IUnknown);
1376 ok(hres == S_OK, "Failed to set IID, hres = %8x\n", hres);
1377 if (hres == S_OK && pSafeArrayGetIID)
1379 hres = pSafeArrayGetIID(sa, &guid);
1380 ok(hres == S_OK && IsEqualGUID(&guid, &IID_IUnknown), "Set bad IID\n");
1383 SafeArrayDestroy(sa);
1386 sa = pSafeArrayCreateEx(VT_DISPATCH, 1, sab, NULL);
1387 ok(sa != NULL, "CreateEx (NULL) failed\n");
1392 if (pSafeArrayGetIID)
1394 hres = pSafeArrayGetIID(sa, &guid);
1395 ok(hres == S_OK, "CreateEx (NULL) no IID hres 0x%x\n", hres);
1398 ok(IsEqualGUID(&guid, &IID_IDispatch), "CreateEx (NULL) bad IID\n");
1401 SafeArrayDestroy(sa);
1404 sa = pSafeArrayCreateEx(VT_UNKNOWN, 1, sab, NULL);
1405 ok(sa != NULL, "CreateEx (NULL-Unk) failed\n");
1410 if (pSafeArrayGetIID)
1412 hres = pSafeArrayGetIID(sa, &guid);
1413 ok(hres == S_OK, "CreateEx (NULL-Unk) no IID hres 0x%x\n", hres);
1416 ok(IsEqualGUID(&guid, &IID_IUnknown), "CreateEx (NULL-Unk) bad IID\n");
1419 SafeArrayDestroy(sa);
1422 /* VT_RECORD failure case */
1423 sa = pSafeArrayCreateEx(VT_RECORD, 1, sab, NULL);
1424 ok(sa == NULL, "CreateEx (NULL-Rec) succeded\n");
1426 iRec = IRecordInfoImpl_Construct();
1428 /* Win32 doesn't care if GetSize fails */
1429 fail_GetSize = TRUE;
1430 sa = pSafeArrayCreateEx(VT_RECORD, 1, sab, iRec);
1431 ok(sa != NULL, "CreateEx (Fail Size) failed\n");
1432 ok(iRec->ref == START_REF_COUNT + 1, "Wrong iRec refcount %d\n", iRec->ref);
1433 ok(iRec->sizeCalled == 1, "GetSize called %d times\n", iRec->sizeCalled);
1434 ok(iRec->clearCalled == 0, "Clear called %d times\n", iRec->clearCalled);
1437 ok(sa->cbElements == RECORD_SIZE_FAIL, "Altered size to %d\n", sa->cbElements);
1438 SafeArrayDestroy(sa);
1439 ok(iRec->clearCalled == sab[0].cElements, "Destroy->Clear called %d times\n", iRec->clearCalled);
1442 /* Test VT_RECORD array */
1443 fail_GetSize = FALSE;
1444 iRec->ref = START_REF_COUNT;
1445 iRec->sizeCalled = 0;
1446 iRec->clearCalled = 0;
1447 sa = pSafeArrayCreateEx(VT_RECORD, 1, sab, iRec);
1448 ok(sa != NULL, "CreateEx (Rec) failed\n");
1449 ok(iRec->ref == START_REF_COUNT + 1, "Wrong iRec refcount %d\n", iRec->ref);
1450 ok(iRec->sizeCalled == 1, "GetSize called %d times\n", iRec->sizeCalled);
1451 ok(iRec->clearCalled == 0, "Clear called %d times\n", iRec->clearCalled);
1452 if (sa && pSafeArrayGetRecordInfo)
1454 IRecordInfo* saRec = NULL;
1455 hres = pSafeArrayGetRecordInfo(sa, &saRec);
1457 ok(hres == S_OK,"GRI failed\n");
1458 ok(saRec == &iRec->IRecordInfo_iface, "Different saRec\n");
1459 ok(iRec->ref == START_REF_COUNT + 2, "Didn't AddRef %d\n", iRec->ref);
1460 if (iRec->ref == START_REF_COUNT + 2)
1461 IRecordInfo_Release(saRec);
1463 ok(sa->cbElements == RECORD_SIZE,"Elemsize is %d\n", sa->cbElements);
1465 SafeArrayDestroy(sa);
1466 ok(iRec->sizeCalled == 1, "Destroy->GetSize called %d times\n", iRec->sizeCalled);
1467 ok(iRec->clearCalled == sab[0].cElements, "Destroy->Clear called %d times\n", iRec->clearCalled);
1468 ok(iRec->ref == START_REF_COUNT, "Wrong iRec refcount %d\n", iRec->ref);
1472 static void test_SafeArrayClear(void)
1481 sa = SafeArrayCreate(VT_UI1, 1, &sab);
1482 ok(sa != NULL, "Create() failed.\n");
1486 /* Test clearing non-NULL variants containing arrays */
1487 V_VT(&v) = VT_ARRAY|VT_UI1;
1489 hres = VariantClear(&v);
1490 ok(hres == S_OK && V_VT(&v) == VT_EMPTY, "VariantClear: hres 0x%x, Type %d\n", hres, V_VT(&v));
1491 ok(V_ARRAY(&v) == sa, "VariantClear: Overwrote value\n");
1493 sa = SafeArrayCreate(VT_UI1, 1, &sab);
1494 ok(sa != NULL, "Create() failed.\n");
1498 V_VT(&v) = VT_SAFEARRAY;
1500 hres = VariantClear(&v);
1501 ok(hres == DISP_E_BADVARTYPE, "VariantClear: hres 0x%x\n", hres);
1503 V_VT(&v) = VT_SAFEARRAY|VT_BYREF;
1504 V_ARRAYREF(&v) = &sa;
1505 hres = VariantClear(&v);
1506 ok(hres == DISP_E_BADVARTYPE, "VariantClear: hres 0x%x\n", hres);
1508 SafeArrayDestroy(sa);
1511 static void test_SafeArrayCopy(void)
1514 SAFEARRAY *sa, *sa2;
1515 VARIANTARG vSrc, vDst;
1520 sa = SafeArrayCreate(VT_UI1, 1, &sab);
1521 ok(sa != NULL, "Create() failed.\n");
1525 /* Test copying non-NULL variants containing arrays */
1526 V_VT(&vSrc) = (VT_ARRAY|VT_BYREF|VT_UI1);
1527 V_ARRAYREF(&vSrc) = &sa;
1528 V_VT(&vDst) = VT_EMPTY;
1530 hres = VariantCopy(&vDst, &vSrc);
1531 ok(hres == S_OK && V_VT(&vDst) == (VT_ARRAY|VT_BYREF|VT_UI1),
1532 "VariantCopy: hres 0x%x, Type %d\n", hres, V_VT(&vDst));
1533 ok(V_ARRAYREF(&vDst) == &sa, "VariantClear: Performed deep copy\n");
1535 V_VT(&vSrc) = (VT_ARRAY|VT_UI1);
1536 V_ARRAY(&vSrc) = sa;
1537 V_VT(&vDst) = VT_EMPTY;
1539 hres = VariantCopy(&vDst, &vSrc);
1540 ok(hres == S_OK && V_VT(&vDst) == (VT_ARRAY|VT_UI1),
1541 "VariantCopy: hres 0x%x, Type %d\n", hres, V_VT(&vDst));
1542 ok(V_ARRAY(&vDst) != sa, "VariantClear: Performed shallow copy\n");
1544 SafeArrayDestroy(V_ARRAY(&vSrc));
1545 SafeArrayDestroy(V_ARRAY(&vDst));
1547 hres = SafeArrayAllocDescriptor(1, &sa);
1548 ok(hres == S_OK, "SafeArrayAllocDescriptor failed with error 0x%08x\n", hres);
1550 hres = SafeArrayCopy(sa, &sa2);
1551 ok(hres == E_INVALIDARG,
1552 "SafeArrayCopy with empty array should have failed with error E_INVALIDARG instead of 0x%08x\n",
1554 sa->cbElements = 16;
1555 hres = SafeArrayCopy(sa, &sa2);
1556 ok(hres == S_OK, "SafeArrayCopy failed with error 0x%08x\n", hres);
1558 SafeArrayDestroy(sa2);
1559 SafeArrayDestroy(sa);
1562 #define MKARRAY(low,num,typ) sab.lLbound = low; sab.cElements = num; \
1563 sa = SafeArrayCreate(typ, 1, &sab); ok(sa != NULL, "Create() failed.\n"); \
1565 V_VT(&v) = VT_ARRAY|typ; V_ARRAY(&v) = sa; VariantInit(&v2)
1567 #define MKARRAYCONT(low,num,typ) sab.lLbound = low; sab.cElements = num; \
1568 sa = SafeArrayCreate(typ, 1, &sab); if (!sa) continue; \
1569 V_VT(&v) = VT_ARRAY|typ; V_ARRAY(&v) = sa; VariantInit(&v2)
1571 static void test_SafeArrayChangeTypeEx(void)
1573 static const char *szHello = "Hello World";
1580 /* VT_ARRAY|VT_UI1 -> VT_BSTR */
1581 MKARRAY(0,strlen(szHello)+1,VT_UI1);
1582 memcpy(sa->pvData, szHello, strlen(szHello)+1);
1584 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_BSTR);
1585 ok(hres == S_OK, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR failed with %x\n", hres);
1588 ok(V_VT(&v2) == VT_BSTR, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR did not return VT_BSTR, but %d.\n",V_VT(&v2));
1589 ok(strcmp((char*)V_BSTR(&v2),szHello) == 0,"Expected string '%s', got '%s'\n", szHello,
1590 (char*)V_BSTR(&v2));
1594 /* VT_VECTOR|VT_UI1 -> VT_BSTR */
1595 SafeArrayDestroy(sa);
1596 if (pSafeArrayCreateVector)
1598 sa = pSafeArrayCreateVector(VT_UI1, 0, strlen(szHello)+1);
1599 ok(sa != NULL, "CreateVector() failed.\n");
1603 memcpy(sa->pvData, szHello, strlen(szHello)+1);
1604 V_VT(&v) = VT_VECTOR|VT_UI1;
1608 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_BSTR);
1609 ok(hres == DISP_E_BADVARTYPE, "CTE VT_VECTOR|VT_UI1 returned %x\n", hres);
1611 /* (vector)VT_ARRAY|VT_UI1 -> VT_BSTR (In place) */
1612 V_VT(&v) = VT_ARRAY|VT_UI1;
1613 hres = VariantChangeTypeEx(&v, &v, 0, 0, VT_BSTR);
1614 ok(hres == S_OK, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR failed with %x\n", hres);
1617 ok(V_VT(&v) == VT_BSTR, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR did not return VT_BSTR, but %d.\n",V_VT(&v));
1618 ok(strcmp((char*)V_BSTR(&v),szHello) == 0,"Expected string '%s', got '%s'\n", szHello,
1624 /* To/from BSTR only works with arrays of VT_UI1 */
1625 for (vt = 0; vt <= VT_CLSID; vt++)
1630 MKARRAYCONT(0,1,vt);
1631 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_BSTR);
1632 ok(hres != S_OK, "CTE VT_ARRAY|VT %d->BSTR succeeded\n", vt);
1637 /* Can't change an array of one type into array of another type , even
1638 * if the other type is the same size
1640 if (pSafeArrayCreateVector)
1642 sa = pSafeArrayCreateVector(VT_UI1, 0, 1);
1643 ok(sa != NULL, "CreateVector() failed.\n");
1647 V_VT(&v) = VT_ARRAY|VT_UI1;
1649 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_ARRAY|VT_I1);
1650 ok(hres == DISP_E_TYPEMISMATCH, "CTE VT_ARRAY|VT_UI1->VT_ARRAY|VT_I1 returned %x\n", hres);
1652 /* But can change to the same array type */
1653 SafeArrayDestroy(sa);
1654 sa = pSafeArrayCreateVector(VT_UI1, 0, 1);
1655 ok(sa != NULL, "CreateVector() failed.\n");
1658 V_VT(&v) = VT_ARRAY|VT_UI1;
1660 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_ARRAY|VT_UI1);
1661 ok(hres == S_OK, "CTE VT_ARRAY|VT_UI1->VT_ARRAY|VT_UI1 returned %x\n", hres);
1662 SafeArrayDestroy(sa);
1667 MKARRAY(0,1,VT_UI1);
1668 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_NULL);
1669 ok(hres == DISP_E_TYPEMISMATCH, "CTE VT_ARRAY|VT_UI1 returned %x\n", hres);
1671 MKARRAY(0,1,VT_UI1);
1672 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_EMPTY);
1673 ok(hres == DISP_E_TYPEMISMATCH, "CTE VT_ARRAY|VT_UI1 returned %x\n", hres);
1678 static void test_SafeArrayDestroyData (void)
1683 int value = 0xdeadbeef;
1685 void HUGEP *temp_pvData;
1689 sa = SafeArrayCreate(VT_INT, 1, &sab);
1690 ok(sa != NULL, "Create() failed.\n");
1694 SafeArrayPutElement (sa, index, &value);
1696 /* SafeArrayDestroyData shouldn't free pvData if FADF_STATIC is set. */
1697 sa->fFeatures |= FADF_STATIC;
1698 temp_pvData = sa->pvData;
1699 hres = SafeArrayDestroyData(sa);
1700 ok(hres == S_OK, "SADData FADF_STATIC failed, error code %x.\n",hres);
1701 ok(sa->pvData == temp_pvData, "SADData FADF_STATIC: pvData=%p, expected %p (fFeatures = %d).\n",
1702 sa->pvData, temp_pvData, sa->fFeatures);
1703 SafeArrayGetElement (sa, index, &value);
1704 ok(value == 0, "Data not cleared after SADData\n");
1706 /* Clear FADF_STATIC, now really destroy the data. */
1707 sa->fFeatures ^= FADF_STATIC;
1708 hres = SafeArrayDestroyData(sa);
1709 ok(hres == S_OK, "SADData !FADF_STATIC failed, error code %x.\n",hres);
1710 ok(sa->pvData == NULL, "SADData !FADF_STATIC: pvData=%p, expected NULL.\n", sa->pvData);
1712 hres = SafeArrayDestroy(sa);
1713 ok(hres == S_OK, "SAD failed, error code %x.\n", hres);
1716 START_TEST(safearray)
1718 hOleaut32 = GetModuleHandleA("oleaut32.dll");
1720 GETPTR(SafeArrayAllocDescriptorEx);
1721 GETPTR(SafeArrayCopyData);
1722 GETPTR(SafeArrayGetIID);
1723 GETPTR(SafeArraySetIID);
1724 GETPTR(SafeArrayGetVartype);
1725 GETPTR(SafeArrayCreateEx);
1726 GETPTR(SafeArrayCreateVector);
1728 check_for_VT_INT_PTR();
1730 test_SafeArrayAllocDestroyDescriptor();
1731 test_SafeArrayCreateLockDestroy();
1732 test_VectorCreateLockDestroy();
1734 test_SafeArrayChangeTypeEx();
1735 test_SafeArrayCopy();
1736 test_SafeArrayClear();
1737 test_SafeArrayCreateEx();
1738 test_SafeArrayCopyData();
1739 test_SafeArrayDestroyData();
1740 test_SafeArrayGetPutElement();
1741 test_SafeArrayGetPutElement_BSTR();
1742 test_SafeArrayGetPutElement_IUnknown();
1743 test_SafeArrayRedim_IUnknown();
1744 test_SafeArrayGetPutElement_VARIANT();