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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/test.h"
43 static HMODULE hOleaut32;
45 static HRESULT (WINAPI *pSafeArrayAllocDescriptorEx)(VARTYPE,UINT,SAFEARRAY**);
46 static HRESULT (WINAPI *pSafeArrayCopyData)(SAFEARRAY*,SAFEARRAY*);
47 static HRESULT (WINAPI *pSafeArrayGetIID)(SAFEARRAY*,GUID*);
48 static HRESULT (WINAPI *pSafeArraySetIID)(SAFEARRAY*,REFGUID);
49 static HRESULT (WINAPI *pSafeArrayGetVartype)(SAFEARRAY*,VARTYPE*);
50 static HRESULT (WINAPI *pSafeArrayGetRecordInfo)(SAFEARRAY*,IRecordInfo**);
51 static SAFEARRAY* (WINAPI *pSafeArrayCreateEx)(VARTYPE,UINT,SAFEARRAYBOUND*,LPVOID);
52 static SAFEARRAY* (WINAPI *pSafeArrayCreateVector)(VARTYPE,LONG,ULONG);
54 #define GETPTR(func) p##func = (void*)GetProcAddress(hOleaut32, #func)
56 /* Is a given function exported from oleaut32? */
57 #define HAVE_FUNC(func) ((void*)GetProcAddress(hOleaut32, #func) != NULL)
59 /* Have IRecordInfo data type? */
60 #define HAVE_OLEAUT32_RECORD HAVE_FUNC(SafeArraySetRecordInfo)
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 /* very old version? */
66 #define IS_ANCIENT (!HAVE_FUNC(VarI1FromI2))
68 #define START_REF_COUNT 1
69 #define RECORD_SIZE 64
70 #define RECORD_SIZE_FAIL 17
71 /************************************************************************
72 * Dummy IRecordInfo Implementation
74 typedef struct IRecordInfoImpl
76 IRecordInfoVtbl *lpvtbl;
82 static IRecordInfoVtbl IRecordInfoImpl_VTable;
84 static IRecordInfoImpl *IRecordInfoImpl_Construct()
88 rec = HeapAlloc(GetProcessHeap(), 0, sizeof(IRecordInfoImpl));
89 rec->lpvtbl = &IRecordInfoImpl_VTable;
90 rec->ref = START_REF_COUNT;
96 static ULONG CALLBACK IRecordInfoImpl_AddRef(IRecordInfo *iface)
98 IRecordInfoImpl* This=(IRecordInfoImpl*)iface;
99 return InterlockedIncrement(&This->ref);
102 static ULONG CALLBACK IRecordInfoImpl_Release(IRecordInfo *iface)
104 IRecordInfoImpl* This=(IRecordInfoImpl*)iface;
105 return InterlockedDecrement(&This->ref);
108 static BOOL fail_GetSize; /* Whether to fail the GetSize call */
110 static HRESULT CALLBACK IRecordInfoImpl_RecordClear(IRecordInfo *iface, PVOID pvExisting)
112 IRecordInfoImpl* This=(IRecordInfoImpl*)iface;
117 static HRESULT CALLBACK IRecordInfoImpl_GetSize(IRecordInfo *iface, ULONG* size)
119 IRecordInfoImpl* This=(IRecordInfoImpl*)iface;
128 static HRESULT CALLBACK IRecordInfoImpl_Dummy(IRecordInfo *iface)
130 trace("Called an unexpected IRecordInfo method - please report!\n");
131 /* Quit because we'll just crash anyway */
136 static IRecordInfoVtbl IRecordInfoImpl_VTable =
138 (PVOID)IRecordInfoImpl_Dummy,
139 IRecordInfoImpl_AddRef,
140 IRecordInfoImpl_Release,
141 (PVOID)IRecordInfoImpl_Dummy,
142 IRecordInfoImpl_RecordClear,
143 (PVOID)IRecordInfoImpl_Dummy,
144 (PVOID)IRecordInfoImpl_Dummy,
145 (PVOID)IRecordInfoImpl_Dummy,
146 (PVOID)IRecordInfoImpl_GetSize,
147 (PVOID)IRecordInfoImpl_Dummy,
148 (PVOID)IRecordInfoImpl_Dummy,
149 (PVOID)IRecordInfoImpl_Dummy,
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
159 static DWORD SAFEARRAY_GetVTSize(VARTYPE vt)
164 case VT_UI1: return sizeof(BYTE);
167 case VT_UI2: return sizeof(SHORT);
171 case VT_ERROR: return sizeof(LONG);
175 if (HAVE_OLEAUT32_I8)
176 return sizeof(LONG64);
179 case VT_UINT: return sizeof(INT);
181 case VT_UINT_PTR: return sizeof(UINT_PTR);
182 case VT_CY: return sizeof(CY);
183 case VT_DATE: return sizeof(DATE);
184 case VT_BSTR: return sizeof(BSTR);
185 case VT_DISPATCH: return sizeof(LPDISPATCH);
186 case VT_VARIANT: return sizeof(VARIANT);
187 case VT_UNKNOWN: return sizeof(LPUNKNOWN);
189 if (HAVE_OLEAUT32_DECIMAL)
190 return sizeof(DECIMAL);
196 #define VARTYPE_NOT_SUPPORTED 0
199 UINT elemsize; /* elementsize by VT */
200 UINT expflags; /* fFeatures from SafeArrayAllocDescriptorEx */
201 UINT addflags; /* additional fFeatures from SafeArrayCreate */
203 {VT_EMPTY, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
204 {VT_NULL, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
205 {VT_I2, 2, FADF_HAVEVARTYPE,0},
206 {VT_I4, 4, FADF_HAVEVARTYPE,0},
207 {VT_R4, 4, FADF_HAVEVARTYPE,0},
208 {VT_R8, 8, FADF_HAVEVARTYPE,0},
209 {VT_CY, 8, FADF_HAVEVARTYPE,0},
210 {VT_DATE, 8, FADF_HAVEVARTYPE,0},
211 {VT_BSTR, sizeof(BSTR), FADF_HAVEVARTYPE,FADF_BSTR},
212 {VT_DISPATCH, sizeof(LPDISPATCH), FADF_HAVEIID, FADF_DISPATCH},
213 {VT_ERROR, 4, FADF_HAVEVARTYPE,0},
214 {VT_BOOL, 2, FADF_HAVEVARTYPE,0},
215 {VT_VARIANT, sizeof(VARIANT), FADF_HAVEVARTYPE,FADF_VARIANT},
216 {VT_UNKNOWN, sizeof(LPUNKNOWN), FADF_HAVEIID, FADF_UNKNOWN},
217 {VT_DECIMAL, sizeof(DECIMAL), FADF_HAVEVARTYPE,0},
218 {15, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0}, /* no VT_xxx */
219 {VT_I1, 1, FADF_HAVEVARTYPE,0},
220 {VT_UI1, 1, FADF_HAVEVARTYPE,0},
221 {VT_UI2, 2, FADF_HAVEVARTYPE,0},
222 {VT_UI4, 4, FADF_HAVEVARTYPE,0},
223 {VT_I8, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
224 {VT_UI8, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
225 {VT_INT, sizeof(INT), FADF_HAVEVARTYPE,0},
226 {VT_UINT, sizeof(UINT), FADF_HAVEVARTYPE,0},
227 {VT_VOID, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
228 {VT_HRESULT, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
229 {VT_PTR, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
230 {VT_SAFEARRAY,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
231 {VT_CARRAY, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
232 {VT_USERDEFINED,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
233 {VT_LPSTR, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
234 {VT_LPWSTR, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
235 {VT_FILETIME, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
236 {VT_RECORD, VARTYPE_NOT_SUPPORTED,FADF_RECORD,0},
237 {VT_BLOB, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
238 {VT_STREAM, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
239 {VT_STORAGE, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
240 {VT_STREAMED_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
241 {VT_STORED_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
242 {VT_BLOB_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
243 {VT_CF, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
244 {VT_CLSID, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
247 static void test_safearray(void)
253 SAFEARRAYBOUND bound, bounds[2];
259 unsigned char *ptr1, *ptr2;
261 hres = SafeArrayDestroy( NULL);
262 ok( hres == S_OK, "SafeArrayDestroy( NULL) returned 0x%lx\n", hres);
266 a = SafeArrayCreate(-1, 1, &bound);
267 ok(NULL == a,"SAC(-1,1,[1,0]) not failed?\n");
271 a = SafeArrayCreate(VT_I4, 1, &bound);
272 ok(NULL != a,"SAC(VT_I4,1,[0,0]) failed.\n");
274 hres = SafeArrayGetLBound(a, 1, &l);
275 ok(hres == S_OK, "SAGLB of 0 size dimensioned array failed with %lx\n",hres);
276 ok(l == 42, "SAGLB of 0 size dimensioned array failed to return 42, but returned %ld\n",l);
277 hres = SafeArrayGetUBound(a, 1, &l);
278 ok(hres == S_OK, "SAGUB of 0 size dimensioned array failed with %lx\n",hres);
279 ok(l == 41, "SAGUB of 0 size dimensioned array failed to return 41, but returned %ld\n",l);
281 hres = SafeArrayAccessData(a, &data);
282 ok(hres == S_OK, "SafeArrayAccessData of 0 size dimensioned array failed with %lx\n", hres);
283 SafeArrayUnaccessData(a);
286 hres = SafeArrayRedim(a, &bound);
287 ok(hres == S_OK,"SAR of a 0 elements dimension failed with hres %lx\n", hres);
289 hres = SafeArrayRedim(a, &bound);
290 ok(hres == S_OK || hres == E_OUTOFMEMORY,
291 "SAR to a 0 elements dimension failed with hres %lx\n", hres);
292 hres = SafeArrayDestroy(a);
293 ok(hres == S_OK,"SAD of 0 dim array faild with hres %lx\n", hres);
295 bounds[0].cElements = 0; bounds[0].lLbound = 1;
296 bounds[1].cElements = 2; bounds[1].lLbound = 23;
297 a = SafeArrayCreate(VT_I4,2,bounds);
298 ok(a != NULL,"SAC(VT_INT32,2,...) with 0 element dim failed.\n");
299 bounds[0].cElements = 1; bounds[0].lLbound = 1;
300 bounds[1].cElements = 0; bounds[1].lLbound = 23;
301 a = SafeArrayCreate(VT_I4,2,bounds);
302 ok(a != NULL,"SAC(VT_INT32,2,...) with 0 element dim failed.\n");
304 bounds[0].cElements = 42; bounds[0].lLbound = 1;
305 bounds[1].cElements = 2; bounds[1].lLbound = 23;
306 a = SafeArrayCreate(VT_I4,2,bounds);
307 ok(a != NULL,"SAC(VT_INT32,2,...) failed.\n");
309 hres = SafeArrayGetLBound (a, 0, &l);
310 ok (hres == DISP_E_BADINDEX, "SAGLB 0 failed with %lx\n", hres);
311 hres = SafeArrayGetLBound (a, 1, &l);
312 ok (hres == S_OK, "SAGLB 1 failed with %lx\n", hres);
313 ok (l == 1, "SAGLB 1 returned %ld instead of 1\n", l);
314 hres = SafeArrayGetLBound (a, 2, &l);
315 ok (hres == S_OK, "SAGLB 2 failed with %lx\n", hres);
316 ok (l == 23, "SAGLB 2 returned %ld instead of 23\n", l);
317 hres = SafeArrayGetLBound (a, 3, &l);
318 ok (hres == DISP_E_BADINDEX, "SAGLB 3 failed with %lx\n", hres);
320 hres = SafeArrayGetUBound (a, 0, &l);
321 ok (hres == DISP_E_BADINDEX, "SAGUB 0 failed with %lx\n", hres);
322 hres = SafeArrayGetUBound (a, 1, &l);
323 ok (hres == S_OK, "SAGUB 1 failed with %lx\n", hres);
324 ok (l == 42, "SAGUB 1 returned %ld instead of 42\n", l);
325 hres = SafeArrayGetUBound (a, 2, &l);
326 ok (hres == S_OK, "SAGUB 2 failed with %lx\n", hres);
327 ok (l == 24, "SAGUB 2 returned %ld instead of 24\n", l);
328 hres = SafeArrayGetUBound (a, 3, &l);
329 ok (hres == DISP_E_BADINDEX, "SAGUB 3 failed with %lx\n", hres);
331 i = SafeArrayGetDim(a);
332 ok(i == 2, "getdims of 2 din array returned %d\n",i);
336 hres = SafeArrayGetElement(a, indices, &i);
337 ok(DISP_E_BADINDEX == hres,"SAGE failed [0,23], hres 0x%lx\n",hres);
341 hres = SafeArrayGetElement(a, indices, &i);
342 ok(DISP_E_BADINDEX == hres,"SAGE failed [1,22], hres 0x%lx\n",hres);
346 hres = SafeArrayGetElement(a, indices, &i);
347 ok(S_OK == hres,"SAGE failed [1,23], hres 0x%lx\n",hres);
351 hres = SafeArrayGetElement(a, indices, &i);
352 ok(DISP_E_BADINDEX == hres,"SAGE failed [1,24], hres 0x%lx\n",hres);
356 hres = SafeArrayGetElement(a, indices, &i);
357 ok(S_OK == hres,"SAGE failed [42,23], hres 0x%lx\n",hres);
359 hres = SafeArrayAccessData(a, (void**)&ptr1);
360 ok(S_OK == hres, "SAAD failed with 0x%lx\n", hres);
364 hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
365 ok(S_OK == hres,"SAPOI failed [1,23], hres 0x%lx\n",hres);
366 ok(ptr2 - ptr1 == 8,"ptr difference is not 8, but %d (%p vs %p)\n", ptr2-ptr1, ptr2, ptr1);
370 hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
371 ok(S_OK == hres,"SAPOI failed [5,24], hres 0x%lx\n",hres);
372 ok(ptr2 - ptr1 == 176,"ptr difference is not 176, but %d (%p vs %p)\n", ptr2-ptr1, ptr2, ptr1);
376 hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
377 ok(S_OK == hres,"SAPOI failed [20,23], hres 0x%lx\n",hres);
378 ok(ptr2 - ptr1 == 76,"ptr difference is not 76, but %d (%p vs %p)\n", ptr2-ptr1, ptr2, ptr1);
380 hres = SafeArrayUnaccessData(a);
381 ok(S_OK == hres, "SAUAD failed with 0x%lx\n", hres);
383 for (i=0;i<sizeof(vttypes)/sizeof(vttypes[0]);i++) {
384 if ((i == VT_I8 || i == VT_UI8) && HAVE_OLEAUT32_I8)
386 vttypes[i].elemsize = sizeof(LONG64);
389 a = SafeArrayCreate(vttypes[i].vt, 1, &bound);
391 ok((!a && !vttypes[i].elemsize) ||
392 (a && vttypes[i].elemsize == a->cbElements) ||
393 (IS_ANCIENT && (vttypes[i].vt == VT_DECIMAL || vttypes[i].vt == VT_I1 ||
394 vttypes[i].vt == VT_UI2 || vttypes[i].vt == VT_UI4 || vttypes[i].vt == VT_INT ||
395 vttypes[i].vt == VT_UINT)),
396 "SAC(%d,1,[1,0]), %p result %ld, expected %d\n",
397 vttypes[i].vt,a,(a?a->cbElements:0),vttypes[i].elemsize);
401 if (!HAVE_OLEAUT32_RECORD)
402 vttypes[i].expflags = 0;
403 ok(a->fFeatures == (vttypes[i].expflags | vttypes[i].addflags),
404 "SAC of %d returned feature flags %x, expected %x\n",
405 vttypes[i].vt, a->fFeatures,
406 vttypes[i].expflags|vttypes[i].addflags);
407 ok(SafeArrayGetElemsize(a) == vttypes[i].elemsize,
408 "SAGE for vt %d returned elemsize %d instead of expected %d\n",
409 vttypes[i].vt, SafeArrayGetElemsize(a),vttypes[i].elemsize);
414 if (pSafeArrayGetVartype)
416 hres = pSafeArrayGetVartype(a, &vt);
417 ok(hres == S_OK, "SAGVT of arra y with vt %d failed with %lx\n", vttypes[i].vt, hres);
418 if (vttypes[i].vt == VT_DISPATCH) {
419 /* Special case. Checked against Windows. */
420 ok(vt == VT_UNKNOWN, "SAGVT of a rray with VT_DISPATCH returned not VT_UNKNOWN, but %d\n", vt);
422 ok(vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d\n", vttypes[i].vt, vt);
426 hres = SafeArrayCopy(a, &c);
427 ok(hres == S_OK, "failed to copy safearray of vt %d with hres %lx\n", vttypes[i].vt, hres);
429 ok(vttypes[i].elemsize == c->cbElements,"copy of SAC(%d,1,[1,0]), result %ld, expected %d\n",vttypes[i].vt,(c?c->cbElements:0),vttypes[i].elemsize
431 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);
432 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);
434 if (pSafeArrayGetVartype) {
435 hres = pSafeArrayGetVartype(c, &vt);
436 ok(hres == S_OK, "SAGVT of array with vt %d failed with %lx\n", vttypes[i].vt, hres);
437 if (vttypes[i].vt == VT_DISPATCH) {
438 /* Special case. Checked against Windows. */
439 ok(vt == VT_UNKNOWN, "SAGVT of array with VT_DISPATCH returned not VT_UNKNOWN, but %d\n", vt);
441 ok(vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d\n", vttypes[i].vt, vt);
445 if (pSafeArrayCopyData) {
446 hres = pSafeArrayCopyData(a, c);
447 ok(hres == S_OK, "failed to copy safearray data of vt %d with hres %lx\n", vttypes[i].vt, hres);
449 hres = SafeArrayDestroyData(c);
450 ok(hres == S_OK,"SADD of copy of array with vt %d failed with hres %lx\n", vttypes[i].vt, hres);
453 hres = SafeArrayDestroy(a);
454 ok(hres == S_OK,"SAD of array with vt %d failed with hres %lx\n", vttypes[i].vt, hres);
457 /* Test conversion of type|VT_ARRAY <-> VT_BSTR */
459 bound.cElements = 10;
460 a = SafeArrayCreate(VT_UI1, 1, &bound);
461 ok(a != NULL, "SAC failed.\n");
462 ok(S_OK == SafeArrayAccessData(a, &data),"SACD failed\n");
463 memcpy(data,"Hello World\n",10);
464 ok(S_OK == SafeArrayUnaccessData(a),"SAUD failed\n");
465 V_VT(&v) = VT_ARRAY|VT_UI1;
467 hres = VariantChangeTypeEx(&v, &v, 0, 0, VT_BSTR);
468 ok(hres==S_OK, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR failed with %lx\n",hres);
469 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));
470 ok(V_BSTR(&v)[0] == 0x6548,"First letter are not 'He', but %x\n", V_BSTR(&v)[0]);
472 /* check locking functions */
473 a = SafeArrayCreate(VT_I4, 1, &bound);
474 ok(a!=NULL,"SAC should not fail\n");
476 hres = SafeArrayAccessData(a, &data);
477 ok(hres == S_OK,"SAAD failed with hres %lx\n",hres);
479 hres = SafeArrayDestroy(a);
480 ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy not failed with DISP_E_ARRAYISLOCKED, but with hres %lx\n", hres);
482 hres = SafeArrayDestroyData(a);
483 ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy data not failed with DISP_E_ARRAYISLOCKED, but with hres %lx\n", hres);
485 hres = SafeArrayDestroyDescriptor(a);
486 ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy descriptor not failed with DISP_E_ARRAYISLOCKED, but with hres %lx\n", hres);
488 hres = SafeArrayUnaccessData(a);
489 ok(hres == S_OK,"SAUD failed after lock/destroy test\n");
491 hres = SafeArrayDestroy(a);
492 ok(hres == S_OK,"SAD failed after lock/destroy test\n");
494 /* Test if we need to destroy data before descriptor */
495 a = SafeArrayCreate(VT_I4, 1, &bound);
496 ok(a!=NULL,"SAC should not fail\n");
497 hres = SafeArrayDestroyDescriptor(a);
498 ok(hres == S_OK,"SADD with data in array failed with hres %lx\n",hres);
502 /* init a small stack safearray */
503 if (pSafeArraySetIID) {
504 memset(&b, 0, sizeof(b));
506 memset(&iid, 0x42, sizeof(IID));
507 hres = pSafeArraySetIID(&b,&iid);
508 ok(hres == E_INVALIDARG,"SafeArraySetIID of non IID capable safearray did not return E_INVALIDARG, but %lx\n",hres);
510 hres = SafeArrayAllocDescriptor(1,&a);
511 ok((a->fFeatures & FADF_HAVEIID) == 0,"newly allocated descriptor with SAAD should not have FADF_HAVEIID\n");
512 hres = pSafeArraySetIID(a,&iid);
513 ok(hres == E_INVALIDARG,"SafeArraySetIID of newly allocated descriptor with SAAD should return E_INVALIDARG, but %lx\n",hres);
516 if (!pSafeArrayAllocDescriptorEx)
519 for (i=0;i<sizeof(vttypes)/sizeof(vttypes[0]);i++) {
521 hres = pSafeArrayAllocDescriptorEx(vttypes[i].vt,1,&a);
522 ok(a->fFeatures == vttypes[i].expflags,"SAADE(%d) resulted with flags %x, expected %x\n", vttypes[i].vt, a->fFeatures, vttypes[i].expflags);
523 if (a->fFeatures & FADF_HAVEIID) {
524 hres = pSafeArrayGetIID(a, &iid);
525 ok(hres == S_OK,"SAGIID failed for vt %d with hres %lx\n", vttypes[i].vt,hres);
526 switch (vttypes[i].vt) {
528 ok(IsEqualGUID(((GUID*)a)-1,&IID_IUnknown),"guid for VT_UNKNOWN is not IID_IUnknown\n");
529 ok(IsEqualGUID(&iid, &IID_IUnknown),"SAGIID returned wrong GUID for IUnknown\n");
532 ok(IsEqualGUID(((GUID*)a)-1,&IID_IDispatch),"guid for VT_UNKNOWN is not IID_IDispatch\n");
533 ok(IsEqualGUID(&iid, &IID_IDispatch),"SAGIID returned wrong GUID for IDispatch\n");
536 ok(FALSE,"unknown vt %d with FADF_HAVEIID\n",vttypes[i].vt);
540 hres = pSafeArrayGetIID(a, &iid);
541 ok(hres == E_INVALIDARG,"SAGIID did not fail for vt %d with hres %lx\n", vttypes[i].vt,hres);
543 if (a->fFeatures & FADF_RECORD) {
544 ok(vttypes[i].vt == VT_RECORD,"FADF_RECORD for non record %d\n",vttypes[i].vt);
546 if (a->fFeatures & FADF_HAVEVARTYPE) {
547 ok(vttypes[i].vt == ((DWORD*)a)[-1], "FADF_HAVEVARTYPE set, but vt %d mismatch stored %ld\n",vttypes[i].vt,((DWORD*)a)[-1]);
550 hres = pSafeArrayGetVartype(a, &vt);
551 ok(hres == S_OK, "SAGVT of array with vt %d failed with %lx\n", vttypes[i].vt, hres);
553 if (vttypes[i].vt == VT_DISPATCH) {
554 /* Special case. Checked against Windows. */
555 ok(vt == VT_UNKNOWN, "SAGVT of array with VT_DISPATCH returned not VT_UNKNOWN, but %d\n", vt);
557 ok(vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d\n", vttypes[i].vt, vt);
560 if (a->fFeatures & FADF_HAVEIID) {
561 hres = pSafeArraySetIID(a, &IID_IStorage); /* random IID */
562 ok(hres == S_OK,"SASIID failed with FADF_HAVEIID set for vt %d with %lx\n", vttypes[i].vt, hres);
563 hres = pSafeArrayGetIID(a, &iid);
564 ok(hres == S_OK,"SAGIID failed with FADF_HAVEIID set for vt %d with %lx\n", vttypes[i].vt, hres);
565 ok(IsEqualGUID(&iid, &IID_IStorage),"returned iid is not IID_IStorage\n");
567 hres = pSafeArraySetIID(a, &IID_IStorage); /* random IID */
568 ok(hres == E_INVALIDARG,"SASIID did not failed with !FADF_HAVEIID set for vt %d with %lx\n", vttypes[i].vt, hres);
570 hres = SafeArrayDestroyDescriptor(a);
571 ok(hres == S_OK,"SADD failed with hres %lx\n",hres);
575 static void test_SafeArrayAllocDestroyDescriptor(void)
582 hres = SafeArrayAllocDescriptor(0, &sa);
583 ok(hres == E_INVALIDARG, "0 dimensions gave hres 0x%lx\n", hres);
585 hres = SafeArrayAllocDescriptor(65536, &sa);
586 ok(IS_ANCIENT || hres == E_INVALIDARG,
587 "65536 dimensions gave hres 0x%lx\n", hres);
590 /* Crashes on 95: XP & Wine return E_POINTER */
591 hres=SafeArrayAllocDescriptor(1, NULL);
592 ok(hres == E_POINTER,"NULL parm gave hres 0x%lx\n", hres);
595 /* Test up to the dimension boundary case */
596 for (i = 5; i <= 65535; i += 30)
598 hres = SafeArrayAllocDescriptor(i, &sa);
599 ok(hres == S_OK, "%d dimensions failed; hres 0x%lx\n", i, hres);
603 ok(SafeArrayGetDim(sa) == (UINT)i, "Dimension is %d; should be %d\n",
604 SafeArrayGetDim(sa), i);
606 hres = SafeArrayDestroyDescriptor(sa);
607 ok(hres == S_OK, "destroy failed; hres 0x%lx\n", hres);
611 if (!pSafeArrayAllocDescriptorEx)
614 hres = pSafeArrayAllocDescriptorEx(VT_UI1, 0, &sa);
615 ok(hres == E_INVALIDARG, "0 dimensions gave hres 0x%lx\n", hres);
617 hres = pSafeArrayAllocDescriptorEx(VT_UI1, 65536, &sa);
618 ok(hres == E_INVALIDARG, "65536 dimensions gave hres 0x%lx\n", hres);
620 hres = pSafeArrayAllocDescriptorEx(VT_UI1, 1, NULL);
621 ok(hres == E_POINTER,"NULL parm gave hres 0x%lx\n", hres);
623 hres = pSafeArrayAllocDescriptorEx(-1, 1, &sa);
624 ok(hres == S_OK, "VT = -1 gave hres 0x%lx\n", hres);
626 sa->rgsabound[0].cElements = 0;
627 sa->rgsabound[0].lLbound = 1;
629 hres = SafeArrayAllocData(sa);
632 ok(hres == S_OK, "SafeArrayAllocData gave hres 0x%lx\n", hres);
636 static void test_SafeArrayCreateLockDestroy(void)
638 SAFEARRAYBOUND sab[4];
644 #define NUM_DIMENSIONS (int)(sizeof(sab) / sizeof(sab[0]))
646 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
648 sab[dimension].lLbound = 0;
649 sab[dimension].cElements = 8;
653 /* This test crashes very early versions with no error checking...
654 sa = SafeArrayCreate(VT_UI1, 1, NULL);
655 ok(sa == NULL, "NULL bounds didn't fail\n");
657 sa = SafeArrayCreate(VT_UI1, 65536, sab);
658 ok(IS_ANCIENT || !sa, "Max bounds didn't fail\n");
660 memset(sab, 0, sizeof(sab));
662 /* Don't test 0 sized dimensions, as Windows has a bug which allows this */
664 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
665 sab[dimension].cElements = 8;
667 /* Test all VARTYPES in 1-4 dimensions */
668 for (dimension = 1; dimension < 4; dimension++)
670 for (vt = VT_EMPTY; vt < VT_CLSID; vt++)
672 DWORD dwLen = SAFEARRAY_GetVTSize(vt);
674 sa = SafeArrayCreate(vt, dimension, sab);
677 ok(sa || (IS_ANCIENT && (vt == VT_DECIMAL || vt == VT_I1 || vt == VT_UI2 ||
678 vt == VT_UI4 || vt == VT_INT || vt == VT_UINT || vt == VT_UINT_PTR ||
680 "VARTYPE %d (@%d dimensions) failed\n", vt, dimension);
682 ok(sa == NULL || vt == VT_R8,
683 "VARTYPE %d (@%d dimensions) succeeded!\n", vt, dimension);
687 ok(SafeArrayGetDim(sa) == (UINT)dimension,
688 "VARTYPE %d (@%d dimensions) cDims is %d, expected %d\n",
689 vt, dimension, SafeArrayGetDim(sa), dimension);
690 ok(SafeArrayGetElemsize(sa) == dwLen || vt == VT_R8,
691 "VARTYPE %d (@%d dimensions) cbElements is %d, expected %ld\n",
692 vt, dimension, SafeArrayGetElemsize(sa), dwLen);
694 if (vt != VT_UNKNOWN && vt != VT_DISPATCH)
696 ok((sa->fFeatures & FADF_HAVEIID) == 0,
697 "Non interface type should not have FADF_HAVEIID\n");
698 if (pSafeArraySetIID)
700 hres = pSafeArraySetIID(sa, &IID_IUnknown);
701 ok(hres == E_INVALIDARG,
702 "Non interface type allowed SetIID(), hres %lx\n", hres);
708 ok(IS_ANCIENT || sa->fFeatures & FADF_HAVEVARTYPE,
709 "Non interface type should have FADF_HAVEVARTYPE\n");
710 if (pSafeArrayGetVartype)
712 hres = pSafeArrayGetVartype(sa, &aVt);
713 ok(hres == S_OK && aVt == vt,
714 "Non interface type %d: bad type %d, hres %lx\n", vt, aVt, hres);
720 ok(IS_ANCIENT || sa->fFeatures & FADF_HAVEIID,
721 "Interface type should have FADF_HAVEIID\n");
722 if (pSafeArraySetIID)
724 hres = pSafeArraySetIID(sa, &IID_IUnknown);
726 "Non interface type disallowed SetIID(), hres %lx\n", hres);
728 ok((sa->fFeatures & FADF_HAVEVARTYPE) == 0,
729 "Interface type %d should not have FADF_HAVEVARTYPE\n", vt);
732 hres = SafeArrayLock(sa);
733 ok(hres == S_OK, "Lock VARTYPE %d (@%d dimensions) failed; hres 0x%lx\n",
734 vt, dimension, hres);
738 hres = SafeArrayDestroy(sa);
739 ok(hres == DISP_E_ARRAYISLOCKED,"Destroy() got hres %lx\n", hres);
741 hres = SafeArrayDestroyData(sa);
742 ok(hres == DISP_E_ARRAYISLOCKED,"DestroyData() got hres %lx\n", hres);
744 hres = SafeArrayDestroyDescriptor(sa);
745 ok(hres == DISP_E_ARRAYISLOCKED,"DestroyDescriptor() got hres %lx\n", hres);
747 hres = SafeArrayUnlock(sa);
748 ok(hres == S_OK, "Unlock VARTYPE %d (@%d dims) hres 0x%lx\n",
749 vt, dimension, hres);
751 hres = SafeArrayDestroyDescriptor(sa);
752 ok(hres == S_OK, "destroy VARTYPE %d (@%d dims) hres 0x%lx\n",
753 vt, dimension, hres);
760 static void test_VectorCreateLockDestroy(void)
767 if (!pSafeArrayCreateVector)
769 sa = pSafeArrayCreateVector(VT_UI1, 0, 0);
770 ok(sa != NULL, "SACV with 0 elements failed.\n");
772 /* Test all VARTYPES in different lengths */
773 for (element = 1; element <= 101; element += 10)
775 for (vt = VT_EMPTY; vt < VT_CLSID; vt++)
777 DWORD dwLen = SAFEARRAY_GetVTSize(vt);
779 sa = pSafeArrayCreateVector(vt, 0, element);
783 /* win2000 has a bug where U/INT_PTR aren't accepted */
784 ok(sa != NULL || vt == VT_INT_PTR || vt == VT_UINT_PTR,
785 "VARTYPE %d (@%d elements) failed\n", vt, element);
788 ok(sa == NULL, "VARTYPE %d (@%d elements) succeeded!\n", vt, element);
792 ok(SafeArrayGetDim(sa) == 1, "VARTYPE %d (@%d elements) cDims %d, not 1\n",
793 vt, element, SafeArrayGetDim(sa));
794 ok(SafeArrayGetElemsize(sa) == dwLen,
795 "VARTYPE %d (@%d elements) cbElements is %d, expected %ld\n",
796 vt, element, SafeArrayGetElemsize(sa), dwLen);
798 hres = SafeArrayLock(sa);
799 ok(hres == S_OK, "Lock VARTYPE %d (@%d elements) failed; hres 0x%lx\n",
804 hres = SafeArrayUnlock(sa);
805 ok(hres == S_OK, "Unlock VARTYPE %d (@%d elements) failed; hres 0x%lx\n",
808 hres = SafeArrayDestroyDescriptor(sa);
809 ok(hres == S_OK, "destroy VARTYPE %d (@%d elements) failed; hres 0x%lx\n",
817 static void test_LockUnlock(void)
819 SAFEARRAYBOUND sab[4];
822 BOOL bVector = FALSE;
826 hres = SafeArrayLock(NULL);
827 ok(hres == E_INVALIDARG, "Lock NULL array hres 0x%lx\n", hres);
828 hres = SafeArrayUnlock(NULL);
829 ok(hres == E_INVALIDARG, "Lock NULL array hres 0x%lx\n", hres);
831 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
833 sab[dimension].lLbound = 0;
834 sab[dimension].cElements = 8;
837 sa = SafeArrayCreate(VT_UI1, NUM_DIMENSIONS, sab);
839 /* Test maximum locks */
840 test_LockUnlock_Vector:
845 hres = SafeArrayUnlock(sa);
846 ok (hres == E_UNEXPECTED, "Bad %sUnlock gave hres 0x%lx\n",
847 bVector ? "vector " : "\n", hres);
849 while ((hres = SafeArrayLock(sa)) == S_OK)
851 ok (count == 65535 && hres == E_UNEXPECTED, "Lock %sfailed at %d; hres 0x%lx\n",
852 bVector ? "vector " : "\n", count, hres);
854 if (count == 65535 && hres == E_UNEXPECTED)
856 while ((hres = SafeArrayUnlock(sa)) == S_OK)
858 ok (count == 0 && hres == E_UNEXPECTED, "Unlock %sfailed at %d; hres 0x%lx\n",
859 bVector ? "vector " : "\n", count, hres);
862 SafeArrayDestroy(sa);
865 if (bVector == FALSE && pSafeArrayCreateVector)
867 /* Test again with a vector */
868 sa = pSafeArrayCreateVector(VT_UI1, 0, 100);
870 goto test_LockUnlock_Vector;
874 static void test_SafeArrayGetPutElement(void)
876 SAFEARRAYBOUND sab[4];
877 LONG indices[NUM_DIMENSIONS];
880 int value = 0, gotvalue, dimension;
881 unsigned int x,y,z,a;
883 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
885 sab[dimension].lLbound = dimension * 2 + 1;
886 sab[dimension].cElements = dimension * 3 + 1;
889 sa = SafeArrayCreate(VT_INT, NUM_DIMENSIONS, sab);
891 return; /* Some early versions can't handle > 3 dims */
893 ok(sa->cbElements == sizeof(value), "int size mismatch\n");
894 if (sa->cbElements != sizeof(value))
898 for (x = 0; x < NUM_DIMENSIONS; x++)
900 indices[0] = sab[0].lLbound;
901 indices[1] = sab[1].lLbound;
902 indices[2] = sab[2].lLbound;
903 indices[3] = sab[3].lLbound;
905 indices[x] = indices[x] - 1;
906 hres = SafeArrayPutElement(sa, indices, &value);
907 ok(hres == DISP_E_BADINDEX, "Put allowed too small index in dimension %d\n", x);
908 hres = SafeArrayGetElement(sa, indices, &value);
909 ok(hres == DISP_E_BADINDEX, "Get allowed too small index in dimension %d\n", x);
911 indices[x] = sab[x].lLbound + sab[x].cElements;
912 hres = SafeArrayPutElement(sa, indices, &value);
913 ok(hres == DISP_E_BADINDEX, "Put allowed too big index in dimension %d\n", x);
914 hres = SafeArrayGetElement(sa, indices, &value);
915 ok(hres == DISP_E_BADINDEX, "Get allowed too big index in dimension %d\n", x);
918 indices[0] = sab[0].lLbound;
919 indices[1] = sab[1].lLbound;
920 indices[2] = sab[2].lLbound;
921 indices[3] = sab[3].lLbound;
923 hres = SafeArrayPutElement(NULL, indices, &value);
924 ok(hres == E_INVALIDARG, "Put NULL array hres 0x%lx\n", hres);
925 hres = SafeArrayGetElement(NULL, indices, &value);
926 ok(hres == E_INVALIDARG, "Get NULL array hres 0x%lx\n", hres);
928 hres = SafeArrayPutElement(sa, NULL, &value);
929 ok(hres == E_INVALIDARG, "Put NULL indices hres 0x%lx\n", hres);
930 hres = SafeArrayGetElement(sa, NULL, &value);
931 ok(hres == E_INVALIDARG, "Get NULL indices hres 0x%lx\n", hres);
934 /* This is retarded. Windows checks every case of invalid parameters
935 * except the following, which crashes. We ERR this in Wine.
937 hres = SafeArrayPutElement(sa, indices, NULL);
938 ok(hres == E_INVALIDARG, "Put NULL value hres 0x%lx\n", hres);
941 hres = SafeArrayGetElement(sa, indices, NULL);
942 ok(hres == E_INVALIDARG, "Get NULL value hres 0x%lx\n", hres);
946 /* Make sure we can read and get back the correct values in 4 dimensions,
947 * Each with a different size and lower bound.
949 for (x = 0; x < sab[0].cElements; x++)
951 indices[0] = sab[0].lLbound + x;
952 for (y = 0; y < sab[1].cElements; y++)
954 indices[1] = sab[1].lLbound + y;
955 for (z = 0; z < sab[2].cElements; z++)
957 indices[2] = sab[2].lLbound + z;
958 for (a = 0; a < sab[3].cElements; a++)
960 indices[3] = sab[3].lLbound + a;
961 hres = SafeArrayPutElement(sa, indices, &value);
962 ok(hres == S_OK, "Failed to put element at (%d,%d,%d,%d) hres 0x%lx\n",
972 for (x = 0; x < sab[0].cElements; x++)
974 indices[0] = sab[0].lLbound + x;
975 for (y = 0; y < sab[1].cElements; y++)
977 indices[1] = sab[1].lLbound + y;
978 for (z = 0; z < sab[2].cElements; z++)
980 indices[2] = sab[2].lLbound + z;
981 for (a = 0; a < sab[3].cElements; a++)
983 indices[3] = sab[3].lLbound + a;
984 gotvalue = value / 3;
985 hres = SafeArrayGetElement(sa, indices, &gotvalue);
986 ok(hres == S_OK, "Failed to get element at (%d,%d,%d,%d) hres 0x%lx\n",
989 ok(value == gotvalue, "Got value %d instead of %d at (%d,%d,%d,%d)\n",
990 gotvalue, value, x, y, z, a);
996 SafeArrayDestroy(sa);
999 static void test_SafeArrayGetPutElement_BSTR(void)
1005 BSTR value = 0, gotvalue;
1006 const OLECHAR szTest[5] = { 'T','e','s','t','\0' };
1011 sa = SafeArrayCreate(VT_BSTR, 1, &sab);
1012 ok(sa != NULL, "BSTR test couldn't create array\n");
1016 ok(sa->cbElements == sizeof(BSTR), "BSTR size mismatch\n");
1017 if (sa->cbElements != sizeof(BSTR))
1020 indices[0] = sab.lLbound;
1021 value = SysAllocString(szTest);
1022 ok (value != NULL, "Expected non-NULL\n");
1023 hres = SafeArrayPutElement(sa, indices, value);
1024 ok(hres == S_OK, "Failed to put bstr element hres 0x%lx\n", hres);
1026 hres = SafeArrayGetElement(sa, indices, &gotvalue);
1027 ok(hres == S_OK, "Failed to get bstr element at hres 0x%lx\n", hres);
1029 ok(SysStringLen(value) == SysStringLen(gotvalue), "Got len %d instead of %d\n", SysStringLen(gotvalue), SysStringLen(value));
1030 SafeArrayDestroy(sa);
1033 static int tunk_xref = 0;
1034 static HRESULT WINAPI tunk_QueryInterface(LPUNKNOWN punk,REFIID riid, LPVOID *x) {
1037 static ULONG WINAPI tunk_AddRef(LPUNKNOWN punk) {
1041 static ULONG WINAPI tunk_Release(LPUNKNOWN punk) {
1045 static IUnknownVtbl xtunk_vtbl = {
1046 tunk_QueryInterface,
1051 static struct xtunk_iface {
1052 IUnknownVtbl *lpvtbl;
1056 static void test_SafeArrayGetPutElement_IUnknown(void)
1062 LPUNKNOWN value = 0, gotvalue;
1066 sa = SafeArrayCreate(VT_UNKNOWN, 1, &sab);
1067 ok(sa != NULL, "UNKNOWN test couldn't create array\n");
1071 ok(sa->cbElements == sizeof(LPUNKNOWN), "LPUNKNOWN size mismatch\n");
1072 if (sa->cbElements != sizeof(LPUNKNOWN))
1075 indices[0] = sab.lLbound;
1076 xtunk_iface.lpvtbl = &xtunk_vtbl;
1077 value = (LPUNKNOWN)&xtunk_iface;
1079 ok (value != NULL, "Expected non-NULL\n");
1080 hres = SafeArrayPutElement(sa, indices, value);
1081 ok(hres == S_OK, "Failed to put bstr element hres 0x%lx\n", hres);
1082 ok(tunk_xref == 2,"Failed to increment refcount of iface.\n");
1084 hres = SafeArrayGetElement(sa, indices, &gotvalue);
1085 ok(tunk_xref == 3,"Failed to increment refcount of iface.\n");
1086 ok(hres == S_OK, "Failed to get bstr element at hres 0x%lx\n", hres);
1088 ok(value == gotvalue, "Got %p instead of %p\n", gotvalue, value);
1089 SafeArrayDestroy(sa);
1090 ok(tunk_xref == 2,"Failed to decrement refcount of iface.\n");
1093 static void test_SafeArrayGetPutElement_VARIANT(void)
1099 VARIANT value, gotvalue;
1103 sa = SafeArrayCreate(VT_VARIANT, 1, &sab);
1104 ok(sa != NULL, "VARIANT test couldn't create array\n");
1108 ok(sa->cbElements == sizeof(VARIANT), "VARIANT size mismatch\n");
1109 if (sa->cbElements != sizeof(VARIANT))
1112 indices[0] = sab.lLbound;
1113 V_VT(&value) = VT_I4;
1114 V_I4(&value) = 0x42424242;
1115 hres = SafeArrayPutElement(sa, indices, &value);
1116 ok(hres == S_OK, "Failed to put Variant I4 element hres 0x%lx\n", hres);
1118 V_VT(&gotvalue) = 0xdead;
1119 hres = SafeArrayGetElement(sa, indices, &gotvalue);
1120 ok(hres == S_OK, "Failed to get variant element at hres 0x%lx\n", hres);
1122 V_VT(&gotvalue) = VT_EMPTY;
1123 hres = SafeArrayGetElement(sa, indices, &gotvalue);
1124 ok(hres == S_OK, "Failed to get variant element at hres 0x%lx\n", hres);
1126 ok(V_VT(&value) == V_VT(&gotvalue), "Got type 0x%x instead of 0x%x\n", V_VT(&value), V_VT(&gotvalue));
1127 if (V_VT(&value) == V_VT(&gotvalue))
1128 ok(V_I4(&value) == V_I4(&gotvalue), "Got %ld instead of %d\n", V_I4(&value), V_VT(&gotvalue));
1130 SafeArrayDestroy(sa);
1134 static void test_SafeArrayCopyData(void)
1136 SAFEARRAYBOUND sab[4];
1140 int dimension,size=1;
1142 if (!pSafeArrayCopyData)
1145 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
1147 sab[dimension].lLbound = dimension * 2 + 2;
1148 sab[dimension].cElements = dimension * 3 + 1;
1149 size *= sab[dimension].cElements;
1152 sa = SafeArrayCreate(VT_INT, NUM_DIMENSIONS, sab);
1153 ok(sa != NULL, "Copy test couldn't create array\n");
1154 sacopy = SafeArrayCreate(VT_INT, NUM_DIMENSIONS, sab);
1155 ok(sacopy != NULL, "Copy test couldn't create copy array\n");
1160 ok(sa->cbElements == sizeof(int), "int size mismatch\n");
1161 if (sa->cbElements != sizeof(int))
1164 /* Fill the source array with some data; it doesn't matter what */
1165 for (dimension = 0; dimension < size; dimension++)
1167 int* data = (int*)sa->pvData;
1168 data[dimension] = dimension;
1171 hres = pSafeArrayCopyData(sa, sacopy);
1172 ok(hres == S_OK, "copy data failed hres 0x%lx\n", hres);
1175 ok(!memcmp(sa->pvData, sacopy->pvData, size * sizeof(int)), "compared different\n");
1179 hres = pSafeArrayCopyData(NULL, sacopy);
1180 ok(hres == E_INVALIDARG, "Null copy source hres 0x%lx\n", hres);
1181 hres = pSafeArrayCopyData(sa, NULL);
1182 ok(hres == E_INVALIDARG, "Null copy hres 0x%lx\n", hres);
1184 sacopy->rgsabound[0].cElements += 1;
1185 hres = pSafeArrayCopyData(sa, sacopy);
1186 ok(hres == E_INVALIDARG, "Bigger copy first dimension hres 0x%lx\n", hres);
1188 sacopy->rgsabound[0].cElements -= 2;
1189 hres = pSafeArrayCopyData(sa, sacopy);
1190 ok(hres == E_INVALIDARG, "Smaller copy first dimension hres 0x%lx\n", hres);
1191 sacopy->rgsabound[0].cElements += 1;
1193 sacopy->rgsabound[3].cElements += 1;
1194 hres = pSafeArrayCopyData(sa, sacopy);
1195 ok(hres == E_INVALIDARG, "Bigger copy last dimension hres 0x%lx\n", hres);
1197 sacopy->rgsabound[3].cElements -= 2;
1198 hres = pSafeArrayCopyData(sa, sacopy);
1199 ok(hres == E_INVALIDARG, "Smaller copy last dimension hres 0x%lx\n", hres);
1200 sacopy->rgsabound[3].cElements += 1;
1202 SafeArrayDestroy(sacopy);
1204 hres = pSafeArrayCopyData(sa, sacopy);
1205 ok(hres == E_INVALIDARG, "->Null copy hres 0x%lx\n", hres);
1207 hres = SafeArrayCopy(sa, &sacopy);
1208 ok(hres == S_OK, "copy failed hres 0x%lx\n", hres);
1211 ok(SafeArrayGetElemsize(sa) == SafeArrayGetElemsize(sacopy),"elemsize wrong\n");
1212 ok(SafeArrayGetDim(sa) == SafeArrayGetDim(sacopy),"dimensions wrong\n");
1213 ok(!memcmp(sa->pvData, sacopy->pvData, size * sizeof(int)), "compared different\n");
1216 SafeArrayDestroy(sa);
1219 static void test_SafeArrayCreateEx(void)
1221 IRecordInfoImpl* iRec;
1222 SAFEARRAYBOUND sab[4];
1227 if (!pSafeArrayCreateEx)
1230 for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
1232 sab[dimension].lLbound = 0;
1233 sab[dimension].cElements = 8;
1237 sa = pSafeArrayCreateEx(VT_UI1, 1, NULL, NULL);
1238 ok(sa == NULL, "CreateEx NULL bounds didn't fail\n");
1240 /* test IID storage & defaulting */
1241 sa = pSafeArrayCreateEx(VT_DISPATCH, 1, sab, (PVOID)&IID_ITypeInfo);
1242 ok(sa != NULL, "CreateEx (ITypeInfo) failed\n");
1247 if (pSafeArrayGetIID)
1249 hres = pSafeArrayGetIID(sa, &guid);
1250 ok(hres == S_OK, "CreateEx (ITypeInfo) no IID hres 0x%lx\n", hres);
1253 ok(IsEqualGUID(&guid, &IID_ITypeInfo), "CreateEx (ITypeInfo) bad IID\n");
1256 if (pSafeArraySetIID)
1258 hres = pSafeArraySetIID(sa, &IID_IUnknown);
1259 ok(hres == S_OK, "Failed to set IID, hres = %8lx\n", hres);
1260 if (hres == S_OK && pSafeArrayGetIID)
1262 hres = pSafeArrayGetIID(sa, &guid);
1263 ok(hres == S_OK && IsEqualGUID(&guid, &IID_IUnknown), "Set bad IID\n");
1266 SafeArrayDestroy(sa);
1269 sa = pSafeArrayCreateEx(VT_DISPATCH, 1, sab, NULL);
1270 ok(sa != NULL, "CreateEx (NULL) failed\n");
1275 if (pSafeArrayGetIID)
1277 hres = pSafeArrayGetIID(sa, &guid);
1278 ok(hres == S_OK, "CreateEx (NULL) no IID hres 0x%lx\n", hres);
1281 ok(IsEqualGUID(&guid, &IID_IDispatch), "CreateEx (NULL) bad IID\n");
1284 SafeArrayDestroy(sa);
1287 sa = pSafeArrayCreateEx(VT_UNKNOWN, 1, sab, NULL);
1288 ok(sa != NULL, "CreateEx (NULL-Unk) failed\n");
1293 if (pSafeArrayGetIID)
1295 hres = pSafeArrayGetIID(sa, &guid);
1296 ok(hres == S_OK, "CreateEx (NULL-Unk) no IID hres 0x%lx\n", hres);
1299 ok(IsEqualGUID(&guid, &IID_IUnknown), "CreateEx (NULL-Unk) bad IID\n");
1302 SafeArrayDestroy(sa);
1305 /* VT_RECORD failure case */
1306 sa = pSafeArrayCreateEx(VT_RECORD, 1, sab, NULL);
1307 ok(sa == NULL, "CreateEx (NULL-Rec) succeded\n");
1309 iRec = IRecordInfoImpl_Construct();
1311 /* Win32 doesn't care if GetSize fails */
1312 fail_GetSize = TRUE;
1313 sa = pSafeArrayCreateEx(VT_RECORD, 1, sab, (LPVOID)iRec);
1314 ok(sa != NULL, "CreateEx (Fail Size) failed\n");
1315 ok(iRec->ref == START_REF_COUNT + 1, "Wrong iRec refcount %ld\n", iRec->ref);
1316 ok(iRec->sizeCalled == 1, "GetSize called %ld times\n", iRec->sizeCalled);
1317 ok(iRec->clearCalled == 0, "Clear called %ld times\n", iRec->clearCalled);
1320 ok(sa->cbElements == RECORD_SIZE_FAIL, "Altered size to %ld\n", sa->cbElements);
1321 SafeArrayDestroy(sa);
1322 ok(iRec->clearCalled == sab[0].cElements, "Destroy->Clear called %ld times\n", iRec->clearCalled);
1325 /* Test VT_RECORD array */
1326 fail_GetSize = FALSE;
1327 iRec->ref = START_REF_COUNT;
1328 iRec->sizeCalled = 0;
1329 iRec->clearCalled = 0;
1330 sa = pSafeArrayCreateEx(VT_RECORD, 1, sab, (LPVOID)iRec);
1331 ok(sa != NULL, "CreateEx (Rec) failed\n");
1332 ok(iRec->ref == START_REF_COUNT + 1, "Wrong iRec refcount %ld\n", iRec->ref);
1333 ok(iRec->sizeCalled == 1, "GetSize called %ld times\n", iRec->sizeCalled);
1334 ok(iRec->clearCalled == 0, "Clear called %ld times\n", iRec->clearCalled);
1335 if (sa && pSafeArrayGetRecordInfo)
1337 IRecordInfo* saRec = NULL;
1338 hres = pSafeArrayGetRecordInfo(sa, &saRec);
1340 ok(hres == S_OK,"GRI failed\n");
1341 ok(saRec == (IRecordInfo*)iRec,"Different saRec\n");
1342 ok(iRec->ref == START_REF_COUNT + 2, "Didn't AddRef %ld\n", iRec->ref);
1343 if (iRec->ref == START_REF_COUNT + 2)
1344 IRecordInfo_Release(saRec);
1346 ok(sa->cbElements == RECORD_SIZE,"Elemsize is %ld\n", sa->cbElements);
1348 SafeArrayDestroy(sa);
1349 ok(iRec->sizeCalled == 1, "Destroy->GetSize called %ld times\n", iRec->sizeCalled);
1350 ok(iRec->clearCalled == sab[0].cElements, "Destroy->Clear called %ld times\n", iRec->clearCalled);
1351 ok(iRec->ref == START_REF_COUNT, "Wrong iRec refcount %ld\n", iRec->ref);
1355 static void test_SafeArrayClear(void)
1364 sa = SafeArrayCreate(VT_UI1, 1, &sab);
1365 ok(sa != NULL, "Create() failed.\n");
1369 /* Test clearing non-NULL variants containing arrays */
1370 V_VT(&v) = VT_ARRAY|VT_UI1;
1372 hres = VariantClear(&v);
1373 ok(hres == S_OK && V_VT(&v) == VT_EMPTY, "VariantClear: hres 0x%lx, Type %d\n", hres, V_VT(&v));
1374 ok(V_ARRAY(&v) == sa, "VariantClear: Overwrote value\n");
1376 sa = SafeArrayCreate(VT_UI1, 1, &sab);
1377 ok(sa != NULL, "Create() failed.\n");
1381 V_VT(&v) = VT_SAFEARRAY;
1383 hres = VariantClear(&v);
1384 ok(hres == DISP_E_BADVARTYPE, "VariantClear: hres 0x%lx\n", hres);
1386 V_VT(&v) = VT_SAFEARRAY|VT_BYREF;
1387 V_ARRAYREF(&v) = &sa;
1388 hres = VariantClear(&v);
1389 ok(hres == DISP_E_BADVARTYPE, "VariantClear: hres 0x%lx\n", hres);
1391 SafeArrayDestroy(sa);
1394 static void test_SafeArrayCopy(void)
1398 VARIANTARG vSrc, vDst;
1403 sa = SafeArrayCreate(VT_UI1, 1, &sab);
1404 ok(sa != NULL, "Create() failed.\n");
1408 /* Test copying non-NULL variants containing arrays */
1409 V_VT(&vSrc) = (VT_ARRAY|VT_BYREF|VT_UI1);
1410 V_ARRAYREF(&vSrc) = &sa;
1411 V_VT(&vDst) = VT_EMPTY;
1413 hres = VariantCopy(&vDst, &vSrc);
1414 ok(hres == S_OK && V_VT(&vDst) == (VT_ARRAY|VT_BYREF|VT_UI1),
1415 "VariantCopy: hres 0x%lx, Type %d\n", hres, V_VT(&vDst));
1416 ok(V_ARRAYREF(&vDst) == &sa, "VariantClear: Performed deep copy\n");
1418 V_VT(&vSrc) = (VT_ARRAY|VT_UI1);
1419 V_ARRAY(&vSrc) = sa;
1420 V_VT(&vDst) = VT_EMPTY;
1422 hres = VariantCopy(&vDst, &vSrc);
1423 ok(hres == S_OK && V_VT(&vDst) == (VT_ARRAY|VT_UI1),
1424 "VariantCopy: hres 0x%lx, Type %d\n", hres, V_VT(&vDst));
1425 ok(V_ARRAY(&vDst) != sa, "VariantClear: Performed shallow copy\n");
1427 SafeArrayDestroy(V_ARRAY(&vSrc));
1428 SafeArrayDestroy(V_ARRAY(&vDst));
1431 #define MKARRAY(low,num,typ) sab.lLbound = low; sab.cElements = num; \
1432 sa = SafeArrayCreate(typ, 1, &sab); ok(sa != NULL, "Create() failed.\n"); \
1434 V_VT(&v) = VT_ARRAY|typ; V_ARRAY(&v) = sa; VariantInit(&v2)
1436 #define MKARRAYCONT(low,num,typ) sab.lLbound = low; sab.cElements = num; \
1437 sa = SafeArrayCreate(typ, 1, &sab); if (!sa) continue; \
1438 V_VT(&v) = VT_ARRAY|typ; V_ARRAY(&v) = sa; VariantInit(&v2)
1440 static void test_SafeArrayChangeTypeEx(void)
1442 static const char *szHello = "Hello World";
1449 /* VT_ARRAY|VT_UI1 -> VT_BSTR */
1450 MKARRAY(0,strlen(szHello)+1,VT_UI1);
1451 memcpy(sa->pvData, szHello, strlen(szHello)+1);
1453 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_BSTR);
1454 ok(hres == S_OK, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR failed with %lx\n", hres);
1457 ok(V_VT(&v2) == VT_BSTR, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR did not return VT_BSTR, but %d.\n",V_VT(&v2));
1458 ok(strcmp((char*)V_BSTR(&v2),szHello) == 0,"Expected string '%s', got '%s'\n", szHello,
1459 (char*)V_BSTR(&v2));
1463 /* VT_VECTOR|VT_UI1 -> VT_BSTR */
1464 SafeArrayDestroy(sa);
1465 if (pSafeArrayCreateVector)
1467 sa = pSafeArrayCreateVector(VT_UI1, 0, strlen(szHello)+1);
1468 ok(sa != NULL, "CreateVector() failed.\n");
1472 memcpy(sa->pvData, szHello, strlen(szHello)+1);
1473 V_VT(&v) = VT_VECTOR|VT_UI1;
1477 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_BSTR);
1478 ok(hres == DISP_E_BADVARTYPE, "CTE VT_VECTOR|VT_UI1 returned %lx\n", hres);
1480 /* (vector)VT_ARRAY|VT_UI1 -> VT_BSTR (In place) */
1481 V_VT(&v) = VT_ARRAY|VT_UI1;
1482 hres = VariantChangeTypeEx(&v, &v, 0, 0, VT_BSTR);
1483 ok(hres == S_OK, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR failed with %lx\n", hres);
1486 ok(V_VT(&v) == VT_BSTR, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR did not return VT_BSTR, but %d.\n",V_VT(&v));
1487 ok(strcmp((char*)V_BSTR(&v),szHello) == 0,"Expected string '%s', got '%s'\n", szHello,
1493 /* To/from BSTR only works with arrays of VT_UI1 */
1494 for (vt = 0; vt <= VT_CLSID; vt++)
1499 MKARRAYCONT(0,1,vt);
1500 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_BSTR);
1501 ok(hres != S_OK, "CTE VT_ARRAY|VT %d->BSTR succeeded\n", vt);
1505 /* Can't change an array of one type into array of another type , even
1506 * if the other type is the same size
1508 if (pSafeArrayCreateVector)
1510 sa = pSafeArrayCreateVector(VT_UI1, 0, 1);
1511 ok(sa != NULL, "CreateVector() failed.\n");
1515 V_VT(&v) = VT_ARRAY|VT_UI1;
1517 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_ARRAY|VT_I1);
1518 ok(hres == DISP_E_TYPEMISMATCH, "CTE VT_ARRAY|VT_UI1->VT_ARRAY|VT_I1 returned %lx\n", hres);
1520 /* But can change to the same array type */
1521 SafeArrayDestroy(sa);
1522 sa = pSafeArrayCreateVector(VT_UI1, 0, 1);
1523 ok(sa != NULL, "CreateVector() failed.\n");
1526 V_VT(&v) = VT_ARRAY|VT_UI1;
1528 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_ARRAY|VT_UI1);
1529 ok(hres == S_OK, "CTE VT_ARRAY|VT_UI1->VT_ARRAY|VT_UI1 returned %lx\n", hres);
1533 MKARRAY(0,1,VT_UI1);
1534 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_NULL);
1535 ok(hres == DISP_E_TYPEMISMATCH, "CTE VT_ARRAY|VT_UI1 returned %lx\n", hres);
1536 MKARRAY(0,1,VT_UI1);
1537 hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_EMPTY);
1538 ok(hres == DISP_E_TYPEMISMATCH, "CTE VT_ARRAY|VT_UI1 returned %lx\n", hres);
1542 static void test_SafeArrayDestroyData (void)
1549 void HUGEP *temp_pvData;
1553 sa = SafeArrayCreate(VT_INT, 1, &sab);
1554 ok(sa != NULL, "Create() failed.\n");
1558 SafeArrayPutElement (sa, index, &value);
1560 /* SafeArrayDestroyData shouldn't do anything if FADF_STATIC is set. */
1561 sa->fFeatures |= FADF_STATIC;
1562 temp_pvData = sa->pvData;
1563 hres = SafeArrayDestroyData(sa);
1564 ok(hres == S_OK, "SADData FADF_STATIC failed, error code %lx.\n",hres);
1565 ok(sa->pvData == temp_pvData, "SADData FADF_STATIC: pvData=%p, expected %p (fFeatures = %d).\n",
1566 sa->pvData, temp_pvData, sa->fFeatures);
1568 /* Clear FADF_STATIC, now really destroy the data. */
1569 sa->fFeatures ^= FADF_STATIC;
1570 hres = SafeArrayDestroyData(sa);
1571 ok(hres == S_OK, "SADData !FADF_STATIC failed, error code %lx.\n",hres);
1572 ok(sa->pvData == NULL, "SADData !FADF_STATIC: pvData=%p, expected NULL.\n", sa->pvData);
1574 hres = SafeArrayDestroy(sa);
1575 ok(hres == S_OK, "SAD failed, error code %lx.\n", hres);
1578 START_TEST(safearray)
1580 hOleaut32 = LoadLibraryA("oleaut32.dll");
1582 GETPTR(SafeArrayAllocDescriptorEx);
1583 GETPTR(SafeArrayCopyData);
1584 GETPTR(SafeArrayGetIID);
1585 GETPTR(SafeArraySetIID);
1586 GETPTR(SafeArrayGetVartype);
1587 GETPTR(SafeArrayCreateEx);
1588 GETPTR(SafeArrayCreateVector);
1591 test_SafeArrayAllocDestroyDescriptor();
1592 test_SafeArrayCreateLockDestroy();
1593 test_VectorCreateLockDestroy();
1595 test_SafeArrayChangeTypeEx();
1596 test_SafeArrayCopy();
1597 test_SafeArrayClear();
1598 test_SafeArrayCreateEx();
1599 test_SafeArrayCopyData();
1600 test_SafeArrayDestroyData();
1601 test_SafeArrayGetPutElement();
1602 test_SafeArrayGetPutElement_BSTR();
1603 test_SafeArrayGetPutElement_IUnknown();
1604 test_SafeArrayGetPutElement_VARIANT();