oleaut32: Implement the ability to marshall VT_CARRAY's of user defined types.
[wine] / dlls / oleaut32 / tests / safearray.c
1 /*
2  * SafeArray test program
3  *
4  * Copyright 2002 Marcus Meissner
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <math.h>
25 #include <float.h>
26
27 #define COBJMACROS
28 #include "wine/test.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "wingdi.h"
33 #include "winnls.h"
34 #include "winsock.h"
35 #include "winerror.h"
36 #include "winnt.h"
37
38 #include "wtypes.h"
39 #include "oleauto.h"
40
41 static HMODULE hOleaut32;
42
43 static HRESULT (WINAPI *pSafeArrayAllocDescriptorEx)(VARTYPE,UINT,SAFEARRAY**);
44 static HRESULT (WINAPI *pSafeArrayCopyData)(SAFEARRAY*,SAFEARRAY*);
45 static HRESULT (WINAPI *pSafeArrayGetIID)(SAFEARRAY*,GUID*);
46 static HRESULT (WINAPI *pSafeArraySetIID)(SAFEARRAY*,REFGUID);
47 static HRESULT (WINAPI *pSafeArrayGetVartype)(SAFEARRAY*,VARTYPE*);
48 static HRESULT (WINAPI *pSafeArrayGetRecordInfo)(SAFEARRAY*,IRecordInfo**);
49 static SAFEARRAY* (WINAPI *pSafeArrayCreateEx)(VARTYPE,UINT,SAFEARRAYBOUND*,LPVOID);
50 static SAFEARRAY* (WINAPI *pSafeArrayCreateVector)(VARTYPE,LONG,ULONG);
51
52 #define GETPTR(func) p##func = (void*)GetProcAddress(hOleaut32, #func)
53
54 /* Is a given function exported from oleaut32? */
55 #define HAVE_FUNC(func) ((void*)GetProcAddress(hOleaut32, #func) != NULL)
56
57 /* Have IRecordInfo data type? */
58 #define HAVE_OLEAUT32_RECORD  HAVE_FUNC(SafeArraySetRecordInfo)
59 /* Have R8 data type? */
60 #define HAVE_OLEAUT32_R8      HAVE_FUNC(VarR8FromI1)
61 /* Have I8/UI8 data type? */
62 #define HAVE_OLEAUT32_I8      HAVE_FUNC(VarI8FromI1)
63 /* Have the decimal type? */
64 #define HAVE_OLEAUT32_DECIMAL HAVE_FUNC(VarDecAdd)
65 /* Have INT_PTR/UINT_PTR type? */
66 static BOOL HAVE_OLEAUT32_INT_PTR;
67
68 /* very old version? */
69 #define IS_ANCIENT (!HAVE_FUNC(VarI1FromI2))
70
71 #define START_REF_COUNT 1
72 #define RECORD_SIZE 64
73 #define RECORD_SIZE_FAIL 17
74 /************************************************************************
75  * Dummy IRecordInfo Implementation
76  */
77 typedef struct IRecordInfoImpl
78 {
79   const IRecordInfoVtbl *lpvtbl;
80   LONG ref;
81   DWORD sizeCalled;
82   DWORD clearCalled;
83 } IRecordInfoImpl;
84
85 static const IRecordInfoVtbl IRecordInfoImpl_VTable;
86
87 static IRecordInfoImpl *IRecordInfoImpl_Construct(void)
88 {
89   IRecordInfoImpl *rec;
90
91   rec = HeapAlloc(GetProcessHeap(), 0, sizeof(IRecordInfoImpl));
92   rec->lpvtbl = &IRecordInfoImpl_VTable;
93   rec->ref = START_REF_COUNT;
94   rec->clearCalled = 0;
95   rec->sizeCalled = 0;
96   return rec;
97 }
98
99 static ULONG CALLBACK IRecordInfoImpl_AddRef(IRecordInfo *iface)
100 {
101   IRecordInfoImpl* This=(IRecordInfoImpl*)iface;
102   return InterlockedIncrement(&This->ref);
103 }
104
105 static ULONG CALLBACK IRecordInfoImpl_Release(IRecordInfo *iface)
106 {
107   IRecordInfoImpl* This=(IRecordInfoImpl*)iface;
108   return InterlockedDecrement(&This->ref);
109 }
110
111 static BOOL fail_GetSize; /* Whether to fail the GetSize call */
112
113 static HRESULT CALLBACK IRecordInfoImpl_RecordClear(IRecordInfo *iface, PVOID pvExisting)
114 {
115   IRecordInfoImpl* This=(IRecordInfoImpl*)iface;
116   This->clearCalled++;
117   return S_OK;
118 }
119
120 static HRESULT CALLBACK IRecordInfoImpl_GetSize(IRecordInfo *iface, ULONG* size)
121 {
122   IRecordInfoImpl* This=(IRecordInfoImpl*)iface;
123   This->sizeCalled++;
124   *size = 17;
125   if (fail_GetSize)
126     return E_UNEXPECTED;
127   *size = RECORD_SIZE;
128   return S_OK;
129 }
130
131 static HRESULT CALLBACK IRecordInfoImpl_Dummy(IRecordInfo *iface)
132 {
133   trace("Called an unexpected IRecordInfo method - please report!\n");
134   /* Quit because we'll just crash anyway */
135   fflush(NULL);
136   exit(255);
137 }
138
139 static const IRecordInfoVtbl IRecordInfoImpl_VTable =
140 {
141   (PVOID)IRecordInfoImpl_Dummy,
142   IRecordInfoImpl_AddRef,
143   IRecordInfoImpl_Release,
144   (PVOID)IRecordInfoImpl_Dummy,
145   IRecordInfoImpl_RecordClear,
146   (PVOID)IRecordInfoImpl_Dummy,
147   (PVOID)IRecordInfoImpl_Dummy,
148   (PVOID)IRecordInfoImpl_Dummy,
149   IRecordInfoImpl_GetSize,
150   (PVOID)IRecordInfoImpl_Dummy,
151   (PVOID)IRecordInfoImpl_Dummy,
152   (PVOID)IRecordInfoImpl_Dummy,
153   (PVOID)IRecordInfoImpl_Dummy,
154   (PVOID)IRecordInfoImpl_Dummy,
155   (PVOID)IRecordInfoImpl_Dummy,
156   (PVOID)IRecordInfoImpl_Dummy,
157   (PVOID)IRecordInfoImpl_Dummy,
158   (PVOID)IRecordInfoImpl_Dummy,
159   (PVOID)IRecordInfoImpl_Dummy
160 };
161
162 static DWORD SAFEARRAY_GetVTSize(VARTYPE vt)
163 {
164   switch (vt)
165   {
166     case VT_I1:
167     case VT_UI1:      return sizeof(BYTE);
168     case VT_BOOL:
169     case VT_I2:
170     case VT_UI2:      return sizeof(SHORT);
171     case VT_I4:
172     case VT_UI4:
173     case VT_R4:
174     case VT_ERROR:    return sizeof(LONG);
175     case VT_R8:
176       if (HAVE_OLEAUT32_R8)
177         return sizeof(LONG64);
178     case VT_I8:
179     case VT_UI8:
180       if (HAVE_OLEAUT32_I8)
181         return sizeof(LONG64);
182       break;
183     case VT_INT:
184     case VT_UINT:     return sizeof(INT);
185     case VT_INT_PTR:
186     case VT_UINT_PTR: 
187       if (HAVE_OLEAUT32_INT_PTR)
188         return sizeof(UINT_PTR);
189       break;
190     case VT_CY:       return sizeof(CY);
191     case VT_DATE:     return sizeof(DATE);
192     case VT_BSTR:     return sizeof(BSTR);
193     case VT_DISPATCH: return sizeof(LPDISPATCH);
194     case VT_VARIANT:  return sizeof(VARIANT);
195     case VT_UNKNOWN:  return sizeof(LPUNKNOWN);
196     case VT_DECIMAL:
197       if (HAVE_OLEAUT32_DECIMAL)
198         return sizeof(DECIMAL);
199       break;
200   }
201   return 0;
202 }
203
204 static void check_for_VT_INT_PTR(void)
205 {
206     /* Set a global flag if VT_INT_PTR is supported */
207
208     SAFEARRAY* a;
209     SAFEARRAYBOUND bound;
210     bound.cElements     = 0;
211     bound.lLbound       = 0;
212     a = SafeArrayCreate(VT_INT_PTR, 1, &bound);
213     if (a) {
214         trace("VT_INT_PTR is supported\n");
215         HAVE_OLEAUT32_INT_PTR = TRUE;
216         SafeArrayDestroy(a);
217     }
218     else {
219         trace("VT_INT_PTR is not supported\n");
220         HAVE_OLEAUT32_INT_PTR = FALSE;
221     }        
222 }
223
224 #define VARTYPE_NOT_SUPPORTED 0
225 static struct {
226         VARTYPE vt;    /* VT */
227         UINT elemsize; /* elementsize by VT */
228         UINT expflags; /* fFeatures from SafeArrayAllocDescriptorEx */
229         UINT addflags; /* additional fFeatures from SafeArrayCreate */
230 } vttypes[] = {
231 {VT_EMPTY,    VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
232 {VT_NULL,     VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
233 {VT_I2,       2,                    FADF_HAVEVARTYPE,0},
234 {VT_I4,       4,                    FADF_HAVEVARTYPE,0},
235 {VT_R4,       4,                    FADF_HAVEVARTYPE,0},
236 {VT_R8,       8,                    FADF_HAVEVARTYPE,0},
237 {VT_CY,       8,                    FADF_HAVEVARTYPE,0},
238 {VT_DATE,     8,                    FADF_HAVEVARTYPE,0},
239 {VT_BSTR,     sizeof(BSTR),         FADF_HAVEVARTYPE,FADF_BSTR},
240 {VT_DISPATCH, sizeof(LPDISPATCH),   FADF_HAVEIID,    FADF_DISPATCH},
241 {VT_ERROR,    4,                    FADF_HAVEVARTYPE,0},
242 {VT_BOOL,     2,                    FADF_HAVEVARTYPE,0},
243 {VT_VARIANT,  sizeof(VARIANT),      FADF_HAVEVARTYPE,FADF_VARIANT},
244 {VT_UNKNOWN,  sizeof(LPUNKNOWN),    FADF_HAVEIID,    FADF_UNKNOWN},
245 {VT_DECIMAL,  sizeof(DECIMAL),      FADF_HAVEVARTYPE,0},
246 {15,          VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0}, /* no VT_xxx */
247 {VT_I1,       1,                    FADF_HAVEVARTYPE,0},
248 {VT_UI1,      1,                    FADF_HAVEVARTYPE,0},
249 {VT_UI2,      2,                    FADF_HAVEVARTYPE,0},
250 {VT_UI4,      4,                    FADF_HAVEVARTYPE,0},
251 {VT_I8,       VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
252 {VT_UI8,      VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
253 {VT_INT,      sizeof(INT),          FADF_HAVEVARTYPE,0},
254 {VT_UINT,     sizeof(UINT),         FADF_HAVEVARTYPE,0},
255 {VT_VOID,     VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
256 {VT_HRESULT,  VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
257 {VT_PTR,      VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
258 {VT_SAFEARRAY,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
259 {VT_CARRAY,   VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
260 {VT_USERDEFINED,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
261 {VT_LPSTR,    VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
262 {VT_LPWSTR,   VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
263 {VT_FILETIME, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
264 {VT_RECORD,   VARTYPE_NOT_SUPPORTED,FADF_RECORD,0},
265 {VT_BLOB,     VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
266 {VT_STREAM,   VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
267 {VT_STORAGE,  VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
268 {VT_STREAMED_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
269 {VT_STORED_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
270 {VT_BLOB_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
271 {VT_CF,       VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
272 {VT_CLSID,    VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
273 };
274
275 static void test_safearray(void)
276 {
277         SAFEARRAY       *a, b, *c;
278         unsigned int    i, diff;
279         LONG            indices[2];
280         HRESULT         hres;
281         SAFEARRAYBOUND  bound, bounds[2];
282         VARIANT         v;
283         LPVOID          data;
284         IID             iid;
285         VARTYPE         vt;
286         LONG            l;
287         unsigned char   *ptr1, *ptr2;
288
289         hres = SafeArrayDestroy( NULL);
290         ok( hres == S_OK, "SafeArrayDestroy( NULL) returned 0x%x\n", hres);
291
292         bound.cElements = 1;
293         bound.lLbound   = 0;
294         a = SafeArrayCreate(-1, 1, &bound);
295         ok(NULL == a,"SAC(-1,1,[1,0]) not failed?\n");
296
297         bound.cElements = 0;
298         bound.lLbound   = 42;
299         a = SafeArrayCreate(VT_I4, 1, &bound);
300         ok(NULL != a,"SAC(VT_I4,1,[0,0]) failed.\n");
301
302         hres = SafeArrayGetLBound(a, 1, &l);
303         ok(hres == S_OK, "SAGLB of 0 size dimensioned array failed with %x\n",hres);
304         ok(l == 42, "SAGLB of 0 size dimensioned array failed to return 42, but returned %d\n",l);
305         hres = SafeArrayGetUBound(a, 1, &l);
306         ok(hres == S_OK, "SAGUB of 0 size dimensioned array failed with %x\n",hres);
307         ok(l == 41, "SAGUB of 0 size dimensioned array failed to return 41, but returned %d\n",l);
308         
309         hres = SafeArrayAccessData(a, &data);
310         ok(hres == S_OK, "SafeArrayAccessData of 0 size dimensioned array failed with %x\n", hres);
311         SafeArrayUnaccessData(a);
312
313         bound.cElements = 2;
314         hres = SafeArrayRedim(a, &bound);
315         ok(hres == S_OK,"SAR of a 0 elements dimension failed with hres %x\n", hres);
316         bound.cElements = 0;
317         hres = SafeArrayRedim(a, &bound);
318         ok(hres == S_OK || hres == E_OUTOFMEMORY,
319           "SAR to a 0 elements dimension failed with hres %x\n", hres);
320         hres = SafeArrayDestroy(a);
321         ok(hres == S_OK,"SAD of 0 dim array faild with hres %x\n", hres);
322
323         SafeArrayAllocDescriptor(2, &a);
324         a->rgsabound[0].cElements = 2;
325         a->rgsabound[0].lLbound = 1;
326         a->rgsabound[1].cElements = 4;
327         a->rgsabound[1].lLbound = 1;
328         a->cbElements = 2;
329         SafeArrayAllocData(a);
330
331         indices[0] = 4;
332         indices[1] = 2;
333         hres = SafeArrayPtrOfIndex(a, indices, (void **)&ptr1);
334         ok(hres == S_OK, "SAPOI failed with hres %x\n", hres);
335         SafeArrayAccessData(a, (void **)&ptr2);
336         ok(ptr1 - ptr2 == 14, "SAPOI got wrong ptr\n");
337         *(WORD *)ptr1 = 0x55aa;
338         SafeArrayUnaccessData(a);
339
340         bound.cElements = 10;
341         bound.lLbound = 1;
342         SafeArrayRedim(a, &bound);
343         ptr1 = NULL;
344         SafeArrayPtrOfIndex(a, indices, (void **)&ptr1);
345         ok(*(WORD *)ptr1 == 0x55aa, "Data not preserved when resizing array\n");
346
347         bound.cElements = 10;
348         bound.lLbound = 0;
349         SafeArrayRedim(a, &bound);
350         SafeArrayPtrOfIndex(a, indices, (void **)&ptr1);
351         ok(*(WORD *)ptr1 == 0, "Expanded area not zero-initialized\n");
352
353         indices[1] = 1;
354         SafeArrayPtrOfIndex(a, indices, (void **)&ptr1);
355         ok(*(WORD *)ptr1 == 0x55aa, "Data not preserved when resizing array\n");
356
357         hres = SafeArrayDestroy(a);
358         ok(hres == S_OK,"SAD faild with hres %x\n", hres);
359
360         bounds[0].cElements = 0;        bounds[0].lLbound =  1;
361         bounds[1].cElements =  2;       bounds[1].lLbound = 23;
362         a = SafeArrayCreate(VT_I4,2,bounds);
363         ok(a != NULL,"SAC(VT_INT32,2,...) with 0 element dim failed.\n");
364
365         hres = SafeArrayDestroy(a);
366         ok(hres == S_OK,"SAD faild with hres %x\n", hres);
367         bounds[0].cElements = 1;        bounds[0].lLbound =  1;
368         bounds[1].cElements = 0;        bounds[1].lLbound = 23;
369         a = SafeArrayCreate(VT_I4,2,bounds);
370         ok(a != NULL,"SAC(VT_INT32,2,...) with 0 element dim failed.\n");
371
372         hres = SafeArrayDestroy(a);
373         ok(hres == S_OK,"SAD faild with hres %x\n", hres);
374
375         bounds[0].cElements = 42;       bounds[0].lLbound =  1;
376         bounds[1].cElements =  2;       bounds[1].lLbound = 23;
377     a = SafeArrayCreate(VT_I4,2,bounds);
378     ok(a != NULL,"SAC(VT_INT32,2,...) failed.\n");
379
380         hres = SafeArrayGetLBound (a, 0, &l);
381         ok (hres == DISP_E_BADINDEX, "SAGLB 0 failed with %x\n", hres);
382         hres = SafeArrayGetLBound (a, 1, &l);
383         ok (hres == S_OK, "SAGLB 1 failed with %x\n", hres);
384         ok (l == 1, "SAGLB 1 returned %d instead of 1\n", l);
385         hres = SafeArrayGetLBound (a, 2, &l);
386         ok (hres == S_OK, "SAGLB 2 failed with %x\n", hres);
387         ok (l == 23, "SAGLB 2 returned %d instead of 23\n", l);
388         hres = SafeArrayGetLBound (a, 3, &l);
389         ok (hres == DISP_E_BADINDEX, "SAGLB 3 failed with %x\n", hres);
390
391         hres = SafeArrayGetUBound (a, 0, &l);
392         ok (hres == DISP_E_BADINDEX, "SAGUB 0 failed with %x\n", hres);
393         hres = SafeArrayGetUBound (a, 1, &l);
394         ok (hres == S_OK, "SAGUB 1 failed with %x\n", hres);
395         ok (l == 42, "SAGUB 1 returned %d instead of 42\n", l);
396         hres = SafeArrayGetUBound (a, 2, &l);
397         ok (hres == S_OK, "SAGUB 2 failed with %x\n", hres);
398         ok (l == 24, "SAGUB 2 returned %d instead of 24\n", l);
399         hres = SafeArrayGetUBound (a, 3, &l);
400         ok (hres == DISP_E_BADINDEX, "SAGUB 3 failed with %x\n", hres);
401
402         i = SafeArrayGetDim(a);
403         ok(i == 2, "getdims of 2 din array returned %d\n",i);
404
405         indices[0] = 0;
406         indices[1] = 23;
407         hres = SafeArrayGetElement(a, indices, &i);
408         ok(DISP_E_BADINDEX == hres,"SAGE failed [0,23], hres 0x%x\n",hres);
409
410         indices[0] = 1;
411         indices[1] = 22;
412         hres = SafeArrayGetElement(a, indices, &i);
413         ok(DISP_E_BADINDEX == hres,"SAGE failed [1,22], hres 0x%x\n",hres);
414
415         indices[0] = 1;
416         indices[1] = 23;
417         hres = SafeArrayGetElement(a, indices, &i);
418         ok(S_OK == hres,"SAGE failed [1,23], hres 0x%x\n",hres);
419
420         indices[0] = 1;
421         indices[1] = 25;
422         hres = SafeArrayGetElement(a, indices, &i);
423         ok(DISP_E_BADINDEX == hres,"SAGE failed [1,24], hres 0x%x\n",hres);
424
425         indices[0] = 3;
426         indices[1] = 23;
427         hres = SafeArrayGetElement(a, indices, &i);
428         ok(S_OK == hres,"SAGE failed [42,23], hres 0x%x\n",hres);
429
430         hres = SafeArrayAccessData(a, (void**)&ptr1);
431         ok(S_OK == hres, "SAAD failed with 0x%x\n", hres);
432
433         indices[0] = 3;
434         indices[1] = 23;
435         hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
436         ok(S_OK == hres,"SAPOI failed [1,23], hres 0x%x\n",hres);
437         diff = ptr2 - ptr1;
438         ok(diff == 8,"ptr difference is not 8, but %d (%p vs %p)\n", diff, ptr2, ptr1);
439
440         indices[0] = 3;
441         indices[1] = 24;
442         hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
443         ok(S_OK == hres,"SAPOI failed [5,24], hres 0x%x\n",hres);
444         diff = ptr2 - ptr1;
445         ok(diff == 176,"ptr difference is not 176, but %d (%p vs %p)\n", diff, ptr2, ptr1);
446
447         indices[0] = 20;
448         indices[1] = 23;
449         hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
450         ok(S_OK == hres,"SAPOI failed [20,23], hres 0x%x\n",hres);
451         diff = ptr2 - ptr1;
452         ok(diff == 76,"ptr difference is not 76, but %d (%p vs %p)\n", diff, ptr2, ptr1);
453
454         hres = SafeArrayUnaccessData(a);
455         ok(S_OK == hres, "SAUAD failed with 0x%x\n", hres);
456
457         hres = SafeArrayDestroy(a);
458         ok(hres == S_OK,"SAD faild with hres %x\n", hres);
459
460         for (i=0;i<sizeof(vttypes)/sizeof(vttypes[0]);i++) {
461         if ((i == VT_I8 || i == VT_UI8) && HAVE_OLEAUT32_I8)
462         {
463           vttypes[i].elemsize = sizeof(LONG64);
464         }
465
466         a = SafeArrayCreate(vttypes[i].vt, 1, &bound);
467
468         ok((!a && !vttypes[i].elemsize) ||
469            (a && vttypes[i].elemsize == a->cbElements) ||
470            (IS_ANCIENT && (vttypes[i].vt == VT_DECIMAL || vttypes[i].vt == VT_I1 ||
471             vttypes[i].vt == VT_UI2 || vttypes[i].vt == VT_UI4 || vttypes[i].vt == VT_INT ||
472             vttypes[i].vt == VT_UINT)),
473            "SAC(%d,1,[1,0]), %p result %d, expected %d\n",
474            vttypes[i].vt,a,(a?a->cbElements:0),vttypes[i].elemsize);
475
476         if (a)
477         {
478           if (!HAVE_OLEAUT32_RECORD)
479             vttypes[i].expflags = 0;
480           ok(a->fFeatures == (vttypes[i].expflags | vttypes[i].addflags),
481              "SAC of %d returned feature flags %x, expected %x\n",
482           vttypes[i].vt, a->fFeatures,
483           vttypes[i].expflags|vttypes[i].addflags);
484           ok(SafeArrayGetElemsize(a) == vttypes[i].elemsize,
485              "SAGE for vt %d returned elemsize %d instead of expected %d\n",
486              vttypes[i].vt, SafeArrayGetElemsize(a),vttypes[i].elemsize);
487         }
488
489                 if (!a) continue;
490
491         if (pSafeArrayGetVartype)
492         {
493             hres = pSafeArrayGetVartype(a, &vt);
494             ok(hres == S_OK, "SAGVT of arra y with vt %d failed with %x\n", vttypes[i].vt, hres);
495             /* Windows prior to Vista returns VT_UNKNOWN instead of VT_DISPATCH */
496             ok(broken(vt == VT_UNKNOWN) || vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d\n", vttypes[i].vt, vt);
497         }
498
499                 hres = SafeArrayCopy(a, &c);
500                 ok(hres == S_OK, "failed to copy safearray of vt %d with hres %x\n", vttypes[i].vt, hres);
501
502                 ok(vttypes[i].elemsize == c->cbElements,"copy of SAC(%d,1,[1,0]), result %d, expected %d\n",vttypes[i].vt,(c?c->cbElements:0),vttypes[i].elemsize
503                 );
504                 ok(c->fFeatures == (vttypes[i].expflags | vttypes[i].addflags),"SAC of %d returned feature flags %x, expected %x\n", vttypes[i].vt, c->fFeatures, vttypes[i].expflags|vttypes[i].addflags);
505                 ok(SafeArrayGetElemsize(c) == vttypes[i].elemsize,"SAGE for vt %d returned elemsize %d instead of expected %d\n",vttypes[i].vt, SafeArrayGetElemsize(c),vttypes[i].elemsize);
506
507         if (pSafeArrayGetVartype) {
508             hres = pSafeArrayGetVartype(c, &vt);
509             ok(hres == S_OK, "SAGVT of array with vt %d failed with %x\n", vttypes[i].vt, hres);
510             /* Windows prior to Vista returns VT_UNKNOWN instead of VT_DISPATCH */
511             ok(broken(vt == VT_UNKNOWN) || vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d\n", vttypes[i].vt, vt);
512         }
513
514         if (pSafeArrayCopyData) {
515             hres = pSafeArrayCopyData(a, c);
516             ok(hres == S_OK, "failed to copy safearray data of vt %d with hres %x\n", vttypes[i].vt, hres);
517
518             hres = SafeArrayDestroyData(c);
519             ok(hres == S_OK,"SADD of copy of array with vt %d failed with hres %x\n", vttypes[i].vt, hres);
520         }
521
522                 hres = SafeArrayDestroy(c);
523                 ok(hres == S_OK,"SAD faild with hres %x\n", hres);
524
525                 hres = SafeArrayDestroy(a);
526                 ok(hres == S_OK,"SAD of array with vt %d failed with hres %x\n", vttypes[i].vt, hres);
527         }
528
529         /* Test conversion of type|VT_ARRAY <-> VT_BSTR */
530         bound.lLbound = 0;
531         bound.cElements = 10;
532         a = SafeArrayCreate(VT_UI1, 1, &bound);
533         ok(a != NULL, "SAC failed.\n");
534         ok(S_OK == SafeArrayAccessData(a, &data),"SACD failed\n");
535         memcpy(data,"Hello World\n",10);
536         ok(S_OK == SafeArrayUnaccessData(a),"SAUD failed\n");
537         V_VT(&v) = VT_ARRAY|VT_UI1;
538         V_ARRAY(&v) = a;
539         hres = VariantChangeTypeEx(&v, &v, 0, 0, VT_BSTR);
540         ok(hres==S_OK, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR failed with %x\n",hres);
541         ok(V_VT(&v) == VT_BSTR,"CTE VT_ARRAY|VT_UI1 -> VT_BSTR did not return VT_BSTR, but %d.v\n",V_VT(&v));
542         ok(V_BSTR(&v)[0] == 0x6548,"First letter are not 'He', but %x\n", V_BSTR(&v)[0]);
543         VariantClear(&v);
544
545         /* check locking functions */
546         a = SafeArrayCreate(VT_I4, 1, &bound);
547         ok(a!=NULL,"SAC should not fail\n");
548
549         hres = SafeArrayAccessData(a, &data);
550         ok(hres == S_OK,"SAAD failed with hres %x\n",hres);
551
552         hres = SafeArrayDestroy(a);
553         ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy not failed with DISP_E_ARRAYISLOCKED, but with hres %x\n", hres);
554
555         hres = SafeArrayDestroyData(a);
556         ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy data not failed with DISP_E_ARRAYISLOCKED, but with hres %x\n", hres);
557
558         hres = SafeArrayDestroyDescriptor(a);
559         ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy descriptor not failed with DISP_E_ARRAYISLOCKED, but with hres %x\n", hres);
560
561         hres = SafeArrayUnaccessData(a);
562         ok(hres == S_OK,"SAUD failed after lock/destroy test\n");
563
564         hres = SafeArrayDestroy(a);
565         ok(hres == S_OK,"SAD failed after lock/destroy test\n");
566
567         /* Test if we need to destroy data before descriptor */
568         a = SafeArrayCreate(VT_I4, 1, &bound);
569         ok(a!=NULL,"SAC should not fail\n");
570         hres = SafeArrayDestroyDescriptor(a);
571         ok(hres == S_OK,"SADD with data in array failed with hres %x\n",hres);
572
573         /* IID functions */
574         /* init a small stack safearray */
575     if (pSafeArraySetIID) {
576         memset(&b, 0, sizeof(b));
577         b.cDims = 1;
578         memset(&iid, 0x42, sizeof(IID));
579         hres = pSafeArraySetIID(&b,&iid);
580         ok(hres == E_INVALIDARG,"SafeArraySetIID of non IID capable safearray did not return E_INVALIDARG, but %x\n",hres);
581
582         hres = SafeArrayAllocDescriptor(1,&a);
583         ok((a->fFeatures & FADF_HAVEIID) == 0,"newly allocated descriptor with SAAD should not have FADF_HAVEIID\n");
584         hres = pSafeArraySetIID(a,&iid);
585         ok(hres == E_INVALIDARG,"SafeArraySetIID of newly allocated descriptor with SAAD should return E_INVALIDARG, but %x\n",hres);
586
587         hres = SafeArrayDestroyDescriptor(a);
588         ok(hres == S_OK,"SADD failed with hres %x\n",hres);
589     }
590
591     if (!pSafeArrayAllocDescriptorEx)
592         return;
593
594         for (i=0;i<sizeof(vttypes)/sizeof(vttypes[0]);i++) {
595         a = NULL;
596                 hres = pSafeArrayAllocDescriptorEx(vttypes[i].vt,1,&a);
597                 ok(a->fFeatures == vttypes[i].expflags,"SAADE(%d) resulted with flags %x, expected %x\n", vttypes[i].vt, a->fFeatures, vttypes[i].expflags);
598                 if (a->fFeatures & FADF_HAVEIID) {
599                         hres = pSafeArrayGetIID(a, &iid);
600                         ok(hres == S_OK,"SAGIID failed for vt %d with hres %x\n", vttypes[i].vt,hres);
601                         switch (vttypes[i].vt) {
602                         case VT_UNKNOWN:
603                                 ok(IsEqualGUID(((GUID*)a)-1,&IID_IUnknown),"guid for VT_UNKNOWN is not IID_IUnknown\n");
604                                 ok(IsEqualGUID(&iid, &IID_IUnknown),"SAGIID returned wrong GUID for IUnknown\n");
605                                 break;
606                         case VT_DISPATCH:
607                                 ok(IsEqualGUID(((GUID*)a)-1,&IID_IDispatch),"guid for VT_UNKNOWN is not IID_IDispatch\n");
608                                 ok(IsEqualGUID(&iid, &IID_IDispatch),"SAGIID returned wrong GUID for IDispatch\n");
609                                 break;
610                         default:
611                                 ok(FALSE,"unknown vt %d with FADF_HAVEIID\n",vttypes[i].vt);
612                                 break;
613                         }
614                 } else {
615                         hres = pSafeArrayGetIID(a, &iid);
616                         ok(hres == E_INVALIDARG,"SAGIID did not fail for vt %d with hres %x\n", vttypes[i].vt,hres);
617                 }
618                 if (a->fFeatures & FADF_RECORD) {
619                         ok(vttypes[i].vt == VT_RECORD,"FADF_RECORD for non record %d\n",vttypes[i].vt);
620                 }
621                 if (a->fFeatures & FADF_HAVEVARTYPE) {
622                         ok(vttypes[i].vt == ((DWORD*)a)[-1], "FADF_HAVEVARTYPE set, but vt %d mismatch stored %d\n",vttypes[i].vt,((DWORD*)a)[-1]);
623                 }
624
625                 hres = pSafeArrayGetVartype(a, &vt);
626                 ok(hres == S_OK, "SAGVT of array with vt %d failed with %x\n", vttypes[i].vt, hres);
627
628                 if (vttypes[i].vt == VT_DISPATCH) {
629                         /* Special case. Checked against Windows. */
630                         ok(vt == VT_UNKNOWN, "SAGVT of array with VT_DISPATCH returned not VT_UNKNOWN, but %d\n", vt);
631                 } else {
632                         ok(vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d\n", vttypes[i].vt, vt);
633                 }
634
635                 if (a->fFeatures & FADF_HAVEIID) {
636                         hres = pSafeArraySetIID(a, &IID_IStorage); /* random IID */
637                         ok(hres == S_OK,"SASIID failed with FADF_HAVEIID set for vt %d with %x\n", vttypes[i].vt, hres);
638                         hres = pSafeArrayGetIID(a, &iid);
639                         ok(hres == S_OK,"SAGIID failed with FADF_HAVEIID set for vt %d with %x\n", vttypes[i].vt, hres);
640                         ok(IsEqualGUID(&iid, &IID_IStorage),"returned iid is not IID_IStorage\n");
641                 } else {
642                         hres = pSafeArraySetIID(a, &IID_IStorage); /* random IID */
643                         ok(hres == E_INVALIDARG,"SASIID did not failed with !FADF_HAVEIID set for vt %d with %x\n", vttypes[i].vt, hres);
644                 }
645                 hres = SafeArrayDestroyDescriptor(a);
646                 ok(hres == S_OK,"SADD failed with hres %x\n",hres);
647         }
648 }
649
650 static void test_SafeArrayAllocDestroyDescriptor(void)
651 {
652   SAFEARRAY *sa;
653   HRESULT hres;
654   int i;
655
656   /* Failure cases */
657   hres = SafeArrayAllocDescriptor(0, &sa);
658   ok(hres == E_INVALIDARG, "0 dimensions gave hres 0x%x\n", hres);
659
660   hres = SafeArrayAllocDescriptor(65536, &sa);
661   ok(IS_ANCIENT || hres == E_INVALIDARG,
662      "65536 dimensions gave hres 0x%x\n", hres);
663
664   if (0)
665   {
666   /* Crashes on 95: XP & Wine return E_POINTER */
667   hres=SafeArrayAllocDescriptor(1, NULL);
668   ok(hres == E_POINTER,"NULL parm gave hres 0x%x\n", hres);
669   }
670
671   /* Test up to the dimension boundary case */
672   for (i = 5; i <= 65535; i += 30)
673   {
674     hres = SafeArrayAllocDescriptor(i, &sa);
675     ok(hres == S_OK, "%d dimensions failed; hres 0x%x\n", i, hres);
676
677     if (hres == S_OK)
678     {
679       ok(SafeArrayGetDim(sa) == (UINT)i, "Dimension is %d; should be %d\n",
680          SafeArrayGetDim(sa), i);
681
682       hres = SafeArrayDestroyDescriptor(sa);
683       ok(hres == S_OK, "destroy failed; hres 0x%x\n", hres);
684     }
685   }
686
687   if (!pSafeArrayAllocDescriptorEx)
688     return;
689
690   hres = pSafeArrayAllocDescriptorEx(VT_UI1, 0, &sa);
691   ok(hres == E_INVALIDARG, "0 dimensions gave hres 0x%x\n", hres);
692
693   hres = pSafeArrayAllocDescriptorEx(VT_UI1, 65536, &sa);
694   ok(hres == E_INVALIDARG, "65536 dimensions gave hres 0x%x\n", hres);
695
696   hres = pSafeArrayAllocDescriptorEx(VT_UI1, 1, NULL);
697   ok(hres == E_POINTER,"NULL parm gave hres 0x%x\n", hres);
698
699   hres = pSafeArrayAllocDescriptorEx(-1, 1, &sa);
700   ok(hres == S_OK, "VT = -1 gave hres 0x%x\n", hres);
701
702   sa->rgsabound[0].cElements = 0;
703   sa->rgsabound[0].lLbound = 1;
704
705   hres = SafeArrayAllocData(sa);
706   ok(hres == S_OK, "SafeArrayAllocData gave hres 0x%x\n", hres);
707
708   hres = SafeArrayDestroy(sa);
709   ok(hres == S_OK,"SafeArrayDestroy failed with hres %x\n",hres);
710 }
711
712 static void test_SafeArrayCreateLockDestroy(void)
713 {
714   SAFEARRAYBOUND sab[4];
715   SAFEARRAY *sa;
716   HRESULT hres;
717   VARTYPE vt;
718   int dimension;
719
720 #define NUM_DIMENSIONS (int)(sizeof(sab) / sizeof(sab[0]))
721
722   for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
723   {
724     sab[dimension].lLbound = 0;
725     sab[dimension].cElements = 8;
726   }
727
728   /* Failure cases */
729 /* This test crashes very early versions with no error checking...
730   sa = SafeArrayCreate(VT_UI1, 1, NULL);
731   ok(sa == NULL, "NULL bounds didn't fail\n");
732 */
733   sa = SafeArrayCreate(VT_UI1, 65536, sab);
734   ok(IS_ANCIENT || !sa, "Max bounds didn't fail\n");
735
736   memset(sab, 0, sizeof(sab));
737
738   /* Don't test 0 sized dimensions, as Windows has a bug which allows this */
739
740   for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
741     sab[dimension].cElements = 8;
742
743   /* Test all VARTYPES in 1-4 dimensions */
744   for (dimension = 1; dimension < 4; dimension++)
745   {
746     for (vt = VT_EMPTY; vt < VT_CLSID; vt++)
747     {
748       DWORD dwLen = SAFEARRAY_GetVTSize(vt);
749
750       sa = SafeArrayCreate(vt, dimension, sab);
751
752       if (dwLen)
753         ok(sa || (IS_ANCIENT && (vt == VT_DECIMAL || vt == VT_I1 || vt == VT_UI2 ||
754            vt == VT_UI4 || vt == VT_INT || vt == VT_UINT)),
755            "VARTYPE %d (@%d dimensions) failed\n", vt, dimension);
756       else
757         ok(sa == NULL || vt == VT_R8,
758            "VARTYPE %d (@%d dimensions) succeeded!\n", vt, dimension);
759
760       if (sa)
761       {
762         ok(SafeArrayGetDim(sa) == (UINT)dimension,
763            "VARTYPE %d (@%d dimensions) cDims is %d, expected %d\n",
764            vt, dimension, SafeArrayGetDim(sa), dimension);
765         ok(SafeArrayGetElemsize(sa) == dwLen || vt == VT_R8,
766            "VARTYPE %d (@%d dimensions) cbElements is %d, expected %d\n",
767            vt, dimension, SafeArrayGetElemsize(sa), dwLen);
768
769         if (vt != VT_UNKNOWN && vt != VT_DISPATCH)
770         {
771           ok((sa->fFeatures & FADF_HAVEIID) == 0,
772              "Non interface type should not have FADF_HAVEIID\n");
773           if (pSafeArraySetIID)
774           {
775             hres = pSafeArraySetIID(sa, &IID_IUnknown);
776             ok(hres == E_INVALIDARG,
777                "Non interface type allowed SetIID(), hres %x\n", hres);
778           }
779           if (vt != VT_RECORD)
780           {
781             VARTYPE aVt;
782
783             ok(IS_ANCIENT || sa->fFeatures & FADF_HAVEVARTYPE,
784                "Non interface type should have FADF_HAVEVARTYPE\n");
785             if (pSafeArrayGetVartype)
786             {
787               hres = pSafeArrayGetVartype(sa, &aVt);
788               ok(hres == S_OK && aVt == vt,
789                  "Non interface type %d: bad type %d, hres %x\n", vt, aVt, hres);
790             }
791           }
792         }
793         else
794         {
795           ok(IS_ANCIENT || sa->fFeatures & FADF_HAVEIID,
796              "Interface type should have FADF_HAVEIID\n");
797           if (pSafeArraySetIID)
798           {
799             hres = pSafeArraySetIID(sa, &IID_IUnknown);
800             ok(hres == S_OK,
801                "Non interface type disallowed SetIID(), hres %x\n", hres);
802           }
803           ok((sa->fFeatures & FADF_HAVEVARTYPE) == 0,
804              "Interface type %d should not have FADF_HAVEVARTYPE\n", vt);
805         }
806
807         hres = SafeArrayLock(sa);
808         ok(hres == S_OK, "Lock VARTYPE %d (@%d dimensions) failed; hres 0x%x\n",
809            vt, dimension, hres);
810
811         if (hres == S_OK)
812         {
813           hres = SafeArrayDestroy(sa);
814           ok(hres == DISP_E_ARRAYISLOCKED,"Destroy() got hres %x\n", hres);
815
816           hres = SafeArrayDestroyData(sa);
817           ok(hres == DISP_E_ARRAYISLOCKED,"DestroyData() got hres %x\n", hres);
818
819           hres = SafeArrayDestroyDescriptor(sa);
820           ok(hres == DISP_E_ARRAYISLOCKED,"DestroyDescriptor() got hres %x\n", hres);
821
822           hres = SafeArrayUnlock(sa);
823           ok(hres == S_OK, "Unlock VARTYPE %d (@%d dims) hres 0x%x\n",
824              vt, dimension, hres);
825
826           hres = SafeArrayDestroy(sa);
827           ok(hres == S_OK, "destroy VARTYPE %d (@%d dims) hres 0x%x\n",
828              vt, dimension, hres);
829         }
830       }
831     }
832   }
833 }
834
835 static void test_VectorCreateLockDestroy(void)
836 {
837   SAFEARRAY *sa;
838   HRESULT hres;
839   VARTYPE vt;
840   int element;
841
842   if (!pSafeArrayCreateVector)
843     return;
844   sa = pSafeArrayCreateVector(VT_UI1, 0, 0);
845   ok(sa != NULL, "SACV with 0 elements failed.\n");
846
847   hres = SafeArrayDestroy(sa);
848   ok(hres == S_OK, "SafeArrayDestroy failed with hres %x\n",hres);
849
850   /* Test all VARTYPES in different lengths */
851   for (element = 1; element <= 101; element += 10)
852   {
853     for (vt = VT_EMPTY; vt < VT_CLSID; vt++)
854     {
855       DWORD dwLen = SAFEARRAY_GetVTSize(vt);
856
857       sa = pSafeArrayCreateVector(vt, 0, element);
858
859       if (dwLen)
860         ok(sa != NULL, "VARTYPE %d (@%d elements) failed\n", vt, element);
861       else
862         ok(sa == NULL, "VARTYPE %d (@%d elements) succeeded!\n", vt, element);
863
864       if (sa)
865       {
866         ok(SafeArrayGetDim(sa) == 1, "VARTYPE %d (@%d elements) cDims %d, not 1\n",
867            vt, element, SafeArrayGetDim(sa));
868         ok(SafeArrayGetElemsize(sa) == dwLen,
869            "VARTYPE %d (@%d elements) cbElements is %d, expected %d\n",
870            vt, element, SafeArrayGetElemsize(sa), dwLen);
871
872         hres = SafeArrayLock(sa);
873         ok(hres == S_OK, "Lock VARTYPE %d (@%d elements) failed; hres 0x%x\n",
874            vt, element, hres);
875
876         if (hres == S_OK)
877         {
878           hres = SafeArrayUnlock(sa);
879           ok(hres == S_OK, "Unlock VARTYPE %d (@%d elements) failed; hres 0x%x\n",
880              vt, element, hres);
881
882           hres = SafeArrayDestroy(sa);
883           ok(hres == S_OK, "destroy VARTYPE %d (@%d elements) failed; hres 0x%x\n",
884              vt, element, hres);
885         }
886       }
887     }
888   }
889 }
890
891 static void test_LockUnlock(void)
892 {
893   SAFEARRAYBOUND sab[4];
894   SAFEARRAY *sa;
895   HRESULT hres;
896   BOOL bVector = FALSE;
897   int dimension;
898
899   /* Failure cases */
900   hres = SafeArrayLock(NULL);
901   ok(hres == E_INVALIDARG, "Lock NULL array hres 0x%x\n", hres);
902   hres = SafeArrayUnlock(NULL);
903   ok(hres == E_INVALIDARG, "Lock NULL array hres 0x%x\n", hres);
904
905   for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
906   {
907     sab[dimension].lLbound = 0;
908     sab[dimension].cElements = 8;
909   }
910
911   sa = SafeArrayCreate(VT_UI1, NUM_DIMENSIONS, sab);
912
913   /* Test maximum locks */
914 test_LockUnlock_Vector:
915   if (sa)
916   {
917     int count = 0;
918
919     hres = SafeArrayUnlock(sa);
920     ok (hres == E_UNEXPECTED, "Bad %sUnlock gave hres 0x%x\n",
921         bVector ? "vector " : "\n", hres);
922
923     while ((hres = SafeArrayLock(sa)) == S_OK)
924       count++;
925     ok (count == 65535 && hres == E_UNEXPECTED, "Lock %sfailed at %d; hres 0x%x\n",
926         bVector ? "vector " : "\n", count, hres);
927
928     if (count == 65535 && hres == E_UNEXPECTED)
929     {
930       while ((hres = SafeArrayUnlock(sa)) == S_OK)
931         count--;
932       ok (count == 0 && hres == E_UNEXPECTED, "Unlock %sfailed at %d; hres 0x%x\n",
933           bVector ? "vector " : "\n", count, hres);
934     }
935
936     SafeArrayDestroy(sa);
937   }
938
939   if (bVector == FALSE && pSafeArrayCreateVector)
940   {
941     /* Test again with a vector */
942     sa = pSafeArrayCreateVector(VT_UI1, 0, 100);
943     bVector = TRUE;
944     goto test_LockUnlock_Vector;
945   }
946 }
947
948 static void test_SafeArrayGetPutElement(void)
949 {
950   SAFEARRAYBOUND sab[4];
951   LONG indices[NUM_DIMENSIONS];
952   SAFEARRAY *sa;
953   HRESULT hres;
954   int value = 0, gotvalue, dimension;
955   unsigned int x,y,z,a;
956
957   for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
958   {
959     sab[dimension].lLbound = dimension * 2 + 1;
960     sab[dimension].cElements = dimension * 3 + 1;
961   }
962
963   sa = SafeArrayCreate(VT_INT, NUM_DIMENSIONS, sab);
964   if (!sa)
965     return; /* Some early versions can't handle > 3 dims */
966
967   ok(sa->cbElements == sizeof(value), "int size mismatch\n");
968   if (sa->cbElements != sizeof(value))
969     return;
970
971   /* Failure cases */
972   for (x = 0; x < NUM_DIMENSIONS; x++)
973   {
974     indices[0] = sab[0].lLbound;
975     indices[1] = sab[1].lLbound;
976     indices[2] = sab[2].lLbound;
977     indices[3] = sab[3].lLbound;
978
979     indices[x] = indices[x] - 1;
980     hres = SafeArrayPutElement(sa, indices, &value);
981     ok(hres == DISP_E_BADINDEX, "Put allowed too small index in dimension %d\n", x);
982     hres = SafeArrayGetElement(sa, indices, &value);
983     ok(hres == DISP_E_BADINDEX, "Get allowed too small index in dimension %d\n", x);
984
985     indices[x] = sab[x].lLbound + sab[x].cElements;
986     hres = SafeArrayPutElement(sa, indices, &value);
987     ok(hres == DISP_E_BADINDEX, "Put allowed too big index in dimension %d\n", x);
988     hres = SafeArrayGetElement(sa, indices, &value);
989     ok(hres == DISP_E_BADINDEX, "Get allowed too big index in dimension %d\n", x);
990   }
991
992   indices[0] = sab[0].lLbound;
993   indices[1] = sab[1].lLbound;
994   indices[2] = sab[2].lLbound;
995   indices[3] = sab[3].lLbound;
996
997   hres = SafeArrayPutElement(NULL, indices, &value);
998   ok(hres == E_INVALIDARG, "Put NULL array hres 0x%x\n", hres);
999   hres = SafeArrayGetElement(NULL, indices, &value);
1000   ok(hres == E_INVALIDARG, "Get NULL array hres 0x%x\n", hres);
1001
1002   hres = SafeArrayPutElement(sa, NULL, &value);
1003   ok(hres == E_INVALIDARG, "Put NULL indices hres 0x%x\n", hres);
1004   hres = SafeArrayGetElement(sa, NULL, &value);
1005   ok(hres == E_INVALIDARG, "Get NULL indices hres 0x%x\n", hres);
1006
1007   if (0)
1008   {
1009   /* This is retarded. Windows checks every case of invalid parameters
1010    * except the following, which crashes. We ERR this in Wine.
1011    */
1012   hres = SafeArrayPutElement(sa, indices, NULL);
1013   ok(hres == E_INVALIDARG, "Put NULL value hres 0x%x\n", hres);
1014   }
1015
1016   hres = SafeArrayGetElement(sa, indices, NULL);
1017   ok(hres == E_INVALIDARG, "Get NULL value hres 0x%x\n", hres);
1018
1019   value = 0;
1020
1021   /* Make sure we can read and get back the correct values in 4 dimensions,
1022    * Each with a different size and lower bound.
1023    */
1024   for (x = 0; x < sab[0].cElements; x++)
1025   {
1026     indices[0] = sab[0].lLbound + x;
1027     for (y = 0; y < sab[1].cElements; y++)
1028     {
1029       indices[1] = sab[1].lLbound + y;
1030       for (z = 0; z < sab[2].cElements; z++)
1031       {
1032         indices[2] = sab[2].lLbound + z;
1033         for (a = 0; a < sab[3].cElements; a++)
1034         {
1035           indices[3] = sab[3].lLbound + a;
1036           hres = SafeArrayPutElement(sa, indices, &value);
1037           ok(hres == S_OK, "Failed to put element at (%d,%d,%d,%d) hres 0x%x\n",
1038              x, y, z, a, hres);
1039           value++;
1040         }
1041       }
1042     }
1043   }
1044
1045   value = 0;
1046
1047   for (x = 0; x < sab[0].cElements; x++)
1048   {
1049     indices[0] = sab[0].lLbound + x;
1050     for (y = 0; y < sab[1].cElements; y++)
1051     {
1052       indices[1] = sab[1].lLbound + y;
1053       for (z = 0; z < sab[2].cElements; z++)
1054       {
1055         indices[2] = sab[2].lLbound + z;
1056         for (a = 0; a < sab[3].cElements; a++)
1057         {
1058           indices[3] = sab[3].lLbound + a;
1059           gotvalue = value / 3;
1060           hres = SafeArrayGetElement(sa, indices, &gotvalue);
1061           ok(hres == S_OK, "Failed to get element at (%d,%d,%d,%d) hres 0x%x\n",
1062              x, y, z, a, hres);
1063           if (hres == S_OK)
1064             ok(value == gotvalue, "Got value %d instead of %d at (%d,%d,%d,%d)\n",
1065                gotvalue, value, x, y, z, a);
1066           value++;
1067         }
1068       }
1069     }
1070   }
1071   SafeArrayDestroy(sa);
1072 }
1073
1074 static void test_SafeArrayGetPutElement_BSTR(void)
1075 {
1076   SAFEARRAYBOUND sab;
1077   LONG indices[1];
1078   SAFEARRAY *sa;
1079   HRESULT hres;
1080   BSTR value = 0, gotvalue;
1081   const OLECHAR szTest[5] = { 'T','e','s','t','\0' };
1082
1083   sab.lLbound = 1;
1084   sab.cElements = 1;
1085
1086   sa = SafeArrayCreate(VT_BSTR, 1, &sab);
1087   ok(sa != NULL, "BSTR test couldn't create array\n");
1088   if (!sa)
1089     return;
1090
1091   ok(sa->cbElements == sizeof(BSTR), "BSTR size mismatch\n");
1092   if (sa->cbElements != sizeof(BSTR))
1093     return;
1094
1095   indices[0] = sab.lLbound;
1096   value = SysAllocString(szTest);
1097   ok (value != NULL, "Expected non-NULL\n");
1098   hres = SafeArrayPutElement(sa, indices, value);
1099   ok(hres == S_OK, "Failed to put bstr element hres 0x%x\n", hres);
1100   gotvalue = NULL;
1101   hres = SafeArrayGetElement(sa, indices, &gotvalue);
1102   ok(hres == S_OK, "Failed to get bstr element at hres 0x%x\n", hres);
1103   if (hres == S_OK)
1104     ok(SysStringLen(value) == SysStringLen(gotvalue), "Got len %d instead of %d\n", SysStringLen(gotvalue), SysStringLen(value));
1105   SafeArrayDestroy(sa);
1106   SysFreeString(value);
1107   SysFreeString(gotvalue);
1108 }
1109
1110 static int tunk_xref = 0;
1111 static HRESULT WINAPI tunk_QueryInterface(LPUNKNOWN punk,REFIID riid, LPVOID *x) {
1112         return E_FAIL;
1113 }
1114 static ULONG WINAPI tunk_AddRef(LPUNKNOWN punk) {
1115         return ++tunk_xref;
1116 }
1117
1118 static ULONG WINAPI tunk_Release(LPUNKNOWN punk) {
1119         return --tunk_xref;
1120 }
1121
1122 static const IUnknownVtbl xtunk_vtbl = {
1123         tunk_QueryInterface,
1124         tunk_AddRef,
1125         tunk_Release
1126 };
1127
1128 static struct xtunk_iface {
1129         const IUnknownVtbl *lpvtbl;
1130 } xtunk_iface;
1131
1132
1133 static void test_SafeArrayGetPutElement_IUnknown(void)
1134 {
1135   SAFEARRAYBOUND sab;
1136   LONG indices[1];
1137   SAFEARRAY *sa;
1138   HRESULT hres;
1139   LPUNKNOWN value = 0, gotvalue;
1140
1141   sab.lLbound = 1;
1142   sab.cElements = 1;
1143   sa = SafeArrayCreate(VT_UNKNOWN, 1, &sab);
1144   ok(sa != NULL, "UNKNOWN test couldn't create array\n");
1145   if (!sa)
1146     return;
1147
1148   ok(sa->cbElements == sizeof(LPUNKNOWN), "LPUNKNOWN size mismatch\n");
1149   if (sa->cbElements != sizeof(LPUNKNOWN))
1150     return;
1151
1152   indices[0] = sab.lLbound;
1153   xtunk_iface.lpvtbl = &xtunk_vtbl;
1154   value = (LPUNKNOWN)&xtunk_iface;
1155   tunk_xref = 1;
1156   ok (value != NULL, "Expected non-NULL\n");
1157   hres = SafeArrayPutElement(sa, indices, value);
1158   ok(hres == S_OK, "Failed to put bstr element hres 0x%x\n", hres);
1159   ok(tunk_xref == 2,"Failed to increment refcount of iface.\n");
1160   gotvalue = NULL;
1161   hres = SafeArrayGetElement(sa, indices, &gotvalue);
1162   ok(tunk_xref == 3,"Failed to increment refcount of iface.\n");
1163   ok(hres == S_OK, "Failed to get bstr element at hres 0x%x\n", hres);
1164   if (hres == S_OK)
1165     ok(value == gotvalue, "Got %p instead of %p\n", gotvalue, value);
1166   SafeArrayDestroy(sa);
1167   ok(tunk_xref == 2,"Failed to decrement refcount of iface.\n");
1168 }
1169
1170 static void test_SafeArrayRedim_IUnknown(void)
1171 {
1172   SAFEARRAYBOUND sab;
1173   LONG indices[1];
1174   SAFEARRAY *sa;
1175   HRESULT hres;
1176   LPUNKNOWN value;
1177
1178   sab.lLbound = 1;
1179   sab.cElements = 2;
1180   sa = SafeArrayCreate(VT_UNKNOWN, 1, &sab);
1181   ok(sa != NULL, "UNKNOWN test couldn't create array\n");
1182   if (!sa)
1183     return;
1184
1185   ok(sa->cbElements == sizeof(LPUNKNOWN), "LPUNKNOWN size mismatch\n");
1186   if (sa->cbElements != sizeof(LPUNKNOWN))
1187     return;
1188
1189   indices[0] = 2;
1190   xtunk_iface.lpvtbl = &xtunk_vtbl;
1191   value = (LPUNKNOWN)&xtunk_iface;
1192   tunk_xref = 1;
1193   hres = SafeArrayPutElement(sa, indices, value);
1194   ok(hres == S_OK, "Failed to put IUnknown element hres 0x%x\n", hres);
1195   ok(tunk_xref == 2,"Failed to increment refcount of iface.\n");
1196   sab.cElements = 1;
1197   hres = SafeArrayRedim(sa, &sab);
1198   ok(hres == S_OK, "Failed to shrink array hres 0x%x\n", hres);
1199   ok(tunk_xref == 1, "Failed to decrement refcount\n");
1200   SafeArrayDestroy(sa);
1201 }
1202
1203 static void test_SafeArrayGetPutElement_VARIANT(void)
1204 {
1205   SAFEARRAYBOUND sab;
1206   LONG indices[1];
1207   SAFEARRAY *sa;
1208   HRESULT hres;
1209   VARIANT value, gotvalue;
1210
1211   sab.lLbound = 1;
1212   sab.cElements = 1;
1213   sa = SafeArrayCreate(VT_VARIANT, 1, &sab);
1214   ok(sa != NULL, "VARIANT test couldn't create array\n");
1215   if (!sa)
1216     return;
1217
1218   ok(sa->cbElements == sizeof(VARIANT), "VARIANT size mismatch\n");
1219   if (sa->cbElements != sizeof(VARIANT))
1220     return;
1221
1222   indices[0] = sab.lLbound;
1223   V_VT(&value) = VT_I4;
1224   V_I4(&value) = 0x42424242;
1225   hres = SafeArrayPutElement(sa, indices, &value);
1226   ok(hres == S_OK, "Failed to put Variant I4 element hres 0x%x\n", hres);
1227
1228   V_VT(&gotvalue) = 0xdead;
1229   hres = SafeArrayGetElement(sa, indices, &gotvalue);
1230   ok(hres == S_OK, "Failed to get variant element at hres 0x%x\n", hres);
1231
1232   V_VT(&gotvalue) = VT_EMPTY;
1233   hres = SafeArrayGetElement(sa, indices, &gotvalue);
1234   ok(hres == S_OK, "Failed to get variant element at hres 0x%x\n", hres);
1235   if (hres == S_OK) {
1236     ok(V_VT(&value) == V_VT(&gotvalue), "Got type 0x%x instead of 0x%x\n", V_VT(&value), V_VT(&gotvalue));
1237     if (V_VT(&value) == V_VT(&gotvalue))
1238         ok(V_I4(&value) == V_I4(&gotvalue), "Got %d instead of %d\n", V_I4(&value), V_VT(&gotvalue));
1239   }
1240   SafeArrayDestroy(sa);
1241 }
1242
1243
1244 static void test_SafeArrayCopyData(void)
1245 {
1246   SAFEARRAYBOUND sab[4];
1247   SAFEARRAY *sa;
1248   SAFEARRAY *sacopy;
1249   HRESULT hres;
1250   int dimension,size=1;
1251
1252   if (!pSafeArrayCopyData)
1253     return;
1254
1255   for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
1256   {
1257     sab[dimension].lLbound = dimension * 2 + 2;
1258     sab[dimension].cElements = dimension * 3 + 1;
1259     size *= sab[dimension].cElements;
1260   }
1261
1262   sa = SafeArrayCreate(VT_INT, NUM_DIMENSIONS, sab);
1263   ok(sa != NULL, "Copy test couldn't create array\n");
1264   sacopy = SafeArrayCreate(VT_INT, NUM_DIMENSIONS, sab);
1265   ok(sacopy != NULL, "Copy test couldn't create copy array\n");
1266
1267   if (!sa || !sacopy)
1268     return;
1269
1270   ok(sa->cbElements == sizeof(int), "int size mismatch\n");
1271   if (sa->cbElements != sizeof(int))
1272     return;
1273
1274   /* Fill the source array with some data; it doesn't matter what */
1275   for (dimension = 0; dimension < size; dimension++)
1276   {
1277     int* data = sa->pvData;
1278     data[dimension] = dimension;
1279   }
1280
1281   hres = pSafeArrayCopyData(sa, sacopy);
1282   ok(hres == S_OK, "copy data failed hres 0x%x\n", hres);
1283   if (hres == S_OK)
1284   {
1285     ok(!memcmp(sa->pvData, sacopy->pvData, size * sizeof(int)), "compared different\n");
1286   }
1287
1288   /* Failure cases */
1289   hres = pSafeArrayCopyData(NULL, sacopy);
1290   ok(hres == E_INVALIDARG, "Null copy source hres 0x%x\n", hres);
1291   hres = pSafeArrayCopyData(sa, NULL);
1292   ok(hres == E_INVALIDARG, "Null copy hres 0x%x\n", hres);
1293
1294   sacopy->rgsabound[0].cElements += 1;
1295   hres = pSafeArrayCopyData(sa, sacopy);
1296   ok(hres == E_INVALIDARG, "Bigger copy first dimension hres 0x%x\n", hres);
1297
1298   sacopy->rgsabound[0].cElements -= 2;
1299   hres = pSafeArrayCopyData(sa, sacopy);
1300   ok(hres == E_INVALIDARG, "Smaller copy first dimension hres 0x%x\n", hres);
1301   sacopy->rgsabound[0].cElements += 1;
1302
1303   sacopy->rgsabound[3].cElements += 1;
1304   hres = pSafeArrayCopyData(sa, sacopy);
1305   ok(hres == E_INVALIDARG, "Bigger copy last dimension hres 0x%x\n", hres);
1306
1307   sacopy->rgsabound[3].cElements -= 2;
1308   hres = pSafeArrayCopyData(sa, sacopy);
1309   ok(hres == E_INVALIDARG, "Smaller copy last dimension hres 0x%x\n", hres);
1310   sacopy->rgsabound[3].cElements += 1;
1311
1312   SafeArrayDestroy(sacopy);
1313   sacopy = NULL;
1314   hres = pSafeArrayCopyData(sa, sacopy);
1315   ok(hres == E_INVALIDARG, "->Null copy hres 0x%x\n", hres);
1316
1317   hres = SafeArrayCopy(sa, &sacopy);
1318   ok(hres == S_OK, "copy failed hres 0x%x\n", hres);
1319   if (hres == S_OK)
1320   {
1321     ok(SafeArrayGetElemsize(sa) == SafeArrayGetElemsize(sacopy),"elemsize wrong\n");
1322     ok(SafeArrayGetDim(sa) == SafeArrayGetDim(sacopy),"dimensions wrong\n");
1323     ok(!memcmp(sa->pvData, sacopy->pvData, size * sizeof(int)), "compared different\n");
1324   }
1325
1326   SafeArrayDestroy(sa);
1327 }
1328
1329 static void test_SafeArrayCreateEx(void)
1330 {
1331   IRecordInfoImpl* iRec;
1332   SAFEARRAYBOUND sab[4];
1333   SAFEARRAY *sa;
1334   HRESULT hres;
1335   int dimension;
1336
1337   if (!pSafeArrayCreateEx)
1338     return;
1339
1340   for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
1341   {
1342     sab[dimension].lLbound = 0;
1343     sab[dimension].cElements = 8;
1344   }
1345
1346   /* Failure cases */
1347   sa = pSafeArrayCreateEx(VT_UI1, 1, NULL, NULL);
1348   ok(sa == NULL, "CreateEx NULL bounds didn't fail\n");
1349
1350   /* test IID storage & defaulting */
1351   sa = pSafeArrayCreateEx(VT_DISPATCH, 1, sab, (PVOID)&IID_ITypeInfo);
1352   ok(sa != NULL, "CreateEx (ITypeInfo) failed\n");
1353
1354   if (sa)
1355   {
1356     GUID guid;
1357     if (pSafeArrayGetIID)
1358     {
1359       hres = pSafeArrayGetIID(sa, &guid);
1360       ok(hres == S_OK, "CreateEx (ITypeInfo) no IID hres 0x%x\n", hres);
1361       if (hres == S_OK)
1362       {
1363         ok(IsEqualGUID(&guid, &IID_ITypeInfo), "CreateEx (ITypeInfo) bad IID\n");
1364       }
1365     }
1366     if (pSafeArraySetIID)
1367     {
1368       hres = pSafeArraySetIID(sa, &IID_IUnknown);
1369       ok(hres == S_OK, "Failed to set IID, hres = %8x\n", hres);
1370       if (hres == S_OK && pSafeArrayGetIID)
1371       {
1372         hres = pSafeArrayGetIID(sa, &guid);
1373         ok(hres == S_OK && IsEqualGUID(&guid, &IID_IUnknown), "Set bad IID\n");
1374       }
1375     }
1376     SafeArrayDestroy(sa);
1377   }
1378
1379   sa = pSafeArrayCreateEx(VT_DISPATCH, 1, sab, NULL);
1380   ok(sa != NULL, "CreateEx (NULL) failed\n");
1381
1382   if (sa)
1383   {
1384     GUID guid;
1385     if (pSafeArrayGetIID)
1386     {
1387       hres = pSafeArrayGetIID(sa, &guid);
1388       ok(hres == S_OK, "CreateEx (NULL) no IID hres 0x%x\n", hres);
1389       if (hres == S_OK)
1390       {
1391         ok(IsEqualGUID(&guid, &IID_IDispatch), "CreateEx (NULL) bad IID\n");
1392       }
1393     }
1394     SafeArrayDestroy(sa);
1395   }
1396
1397   sa = pSafeArrayCreateEx(VT_UNKNOWN, 1, sab, NULL);
1398   ok(sa != NULL, "CreateEx (NULL-Unk) failed\n");
1399
1400   if (sa)
1401   {
1402     GUID guid;
1403     if (pSafeArrayGetIID)
1404     {
1405       hres = pSafeArrayGetIID(sa, &guid);
1406       ok(hres == S_OK, "CreateEx (NULL-Unk) no IID hres 0x%x\n", hres);
1407       if (hres == S_OK)
1408       {
1409         ok(IsEqualGUID(&guid, &IID_IUnknown), "CreateEx (NULL-Unk) bad IID\n");
1410       }
1411     }
1412     SafeArrayDestroy(sa);
1413   }
1414
1415   /* VT_RECORD failure case */
1416   sa = pSafeArrayCreateEx(VT_RECORD, 1, sab, NULL);
1417   ok(sa == NULL, "CreateEx (NULL-Rec) succeded\n");
1418
1419   iRec = IRecordInfoImpl_Construct();
1420
1421   /* Win32 doesn't care if GetSize fails */
1422   fail_GetSize = TRUE;
1423   sa = pSafeArrayCreateEx(VT_RECORD, 1, sab, iRec);
1424   ok(sa != NULL, "CreateEx (Fail Size) failed\n");
1425   ok(iRec->ref == START_REF_COUNT + 1, "Wrong iRec refcount %d\n", iRec->ref);
1426   ok(iRec->sizeCalled == 1, "GetSize called %d times\n", iRec->sizeCalled);
1427   ok(iRec->clearCalled == 0, "Clear called %d times\n", iRec->clearCalled);
1428   if (sa)
1429   {
1430     ok(sa->cbElements == RECORD_SIZE_FAIL, "Altered size to %d\n", sa->cbElements);
1431     SafeArrayDestroy(sa);
1432     ok(iRec->clearCalled == sab[0].cElements, "Destroy->Clear called %d times\n", iRec->clearCalled);
1433   }
1434
1435   /* Test VT_RECORD array */
1436   fail_GetSize = FALSE;
1437   iRec->ref = START_REF_COUNT;
1438   iRec->sizeCalled = 0;
1439   iRec->clearCalled = 0;
1440   sa = pSafeArrayCreateEx(VT_RECORD, 1, sab, iRec);
1441   ok(sa != NULL, "CreateEx (Rec) failed\n");
1442   ok(iRec->ref == START_REF_COUNT + 1, "Wrong iRec refcount %d\n", iRec->ref);
1443   ok(iRec->sizeCalled == 1, "GetSize called %d times\n", iRec->sizeCalled);
1444   ok(iRec->clearCalled == 0, "Clear called %d times\n", iRec->clearCalled);
1445   if (sa && pSafeArrayGetRecordInfo)
1446   {
1447     IRecordInfo* saRec = NULL;
1448     hres = pSafeArrayGetRecordInfo(sa, &saRec);
1449
1450     ok(hres == S_OK,"GRI failed\n");
1451     ok(saRec == (IRecordInfo*)iRec,"Different saRec\n");
1452     ok(iRec->ref == START_REF_COUNT + 2, "Didn't AddRef %d\n", iRec->ref);
1453     if (iRec->ref == START_REF_COUNT + 2)
1454       IRecordInfo_Release(saRec);
1455
1456     ok(sa->cbElements == RECORD_SIZE,"Elemsize is %d\n", sa->cbElements);
1457
1458     SafeArrayDestroy(sa);
1459     ok(iRec->sizeCalled == 1, "Destroy->GetSize called %d times\n", iRec->sizeCalled);
1460     ok(iRec->clearCalled == sab[0].cElements, "Destroy->Clear called %d times\n", iRec->clearCalled);
1461     ok(iRec->ref == START_REF_COUNT, "Wrong iRec refcount %d\n", iRec->ref);
1462   }
1463 }
1464
1465 static void test_SafeArrayClear(void)
1466 {
1467   SAFEARRAYBOUND sab;
1468   SAFEARRAY *sa;
1469   VARIANTARG v;
1470   HRESULT hres;
1471
1472   sab.lLbound = 0;
1473   sab.cElements = 10;
1474   sa = SafeArrayCreate(VT_UI1, 1, &sab);
1475   ok(sa != NULL, "Create() failed.\n");
1476   if (!sa)
1477     return;
1478
1479   /* Test clearing non-NULL variants containing arrays */
1480   V_VT(&v) = VT_ARRAY|VT_UI1;
1481   V_ARRAY(&v) = sa;
1482   hres = VariantClear(&v);
1483   ok(hres == S_OK && V_VT(&v) == VT_EMPTY, "VariantClear: hres 0x%x, Type %d\n", hres, V_VT(&v));
1484   ok(V_ARRAY(&v) == sa, "VariantClear: Overwrote value\n");
1485
1486   sa = SafeArrayCreate(VT_UI1, 1, &sab);
1487   ok(sa != NULL, "Create() failed.\n");
1488   if (!sa)
1489     return;
1490
1491   V_VT(&v) = VT_SAFEARRAY;
1492   V_ARRAY(&v) = sa;
1493   hres = VariantClear(&v);
1494   ok(hres == DISP_E_BADVARTYPE, "VariantClear: hres 0x%x\n", hres);
1495
1496   V_VT(&v) = VT_SAFEARRAY|VT_BYREF;
1497   V_ARRAYREF(&v) = &sa;
1498   hres = VariantClear(&v);
1499   ok(hres == DISP_E_BADVARTYPE, "VariantClear: hres 0x%x\n", hres);
1500
1501   SafeArrayDestroy(sa);
1502 }
1503
1504 static void test_SafeArrayCopy(void)
1505 {
1506   SAFEARRAYBOUND sab;
1507   SAFEARRAY *sa, *sa2;
1508   VARIANTARG vSrc, vDst;
1509   HRESULT hres;
1510
1511   sab.lLbound = 0;
1512   sab.cElements = 10;
1513   sa = SafeArrayCreate(VT_UI1, 1, &sab);
1514   ok(sa != NULL, "Create() failed.\n");
1515   if (!sa)
1516     return;
1517
1518   /* Test copying non-NULL variants containing arrays */
1519   V_VT(&vSrc) = (VT_ARRAY|VT_BYREF|VT_UI1);
1520   V_ARRAYREF(&vSrc) = &sa;
1521   V_VT(&vDst) = VT_EMPTY;
1522
1523   hres = VariantCopy(&vDst, &vSrc);
1524   ok(hres == S_OK && V_VT(&vDst) == (VT_ARRAY|VT_BYREF|VT_UI1),
1525      "VariantCopy: hres 0x%x, Type %d\n", hres, V_VT(&vDst));
1526   ok(V_ARRAYREF(&vDst) == &sa, "VariantClear: Performed deep copy\n");
1527
1528   V_VT(&vSrc) = (VT_ARRAY|VT_UI1);
1529   V_ARRAY(&vSrc) = sa;
1530   V_VT(&vDst) = VT_EMPTY;
1531
1532   hres = VariantCopy(&vDst, &vSrc);
1533   ok(hres == S_OK && V_VT(&vDst) == (VT_ARRAY|VT_UI1),
1534      "VariantCopy: hres 0x%x, Type %d\n", hres, V_VT(&vDst));
1535   ok(V_ARRAY(&vDst) != sa, "VariantClear: Performed shallow copy\n");
1536
1537   SafeArrayDestroy(V_ARRAY(&vSrc));
1538   SafeArrayDestroy(V_ARRAY(&vDst));
1539
1540   hres = SafeArrayAllocDescriptor(1, &sa);
1541   ok(hres == S_OK, "SafeArrayAllocDescriptor failed with error 0x%08x\n", hres);
1542
1543   hres = SafeArrayCopy(sa, &sa2);
1544   ok(hres == E_INVALIDARG,
1545     "SafeArrayCopy with empty array should have failed with error E_INVALIDARG instead of 0x%08x\n",
1546     hres);
1547   sa->cbElements = 16;
1548   hres = SafeArrayCopy(sa, &sa2);
1549   ok(hres == S_OK, "SafeArrayCopy failed with error 0x%08x\n", hres);
1550
1551   SafeArrayDestroy(sa);
1552 }
1553
1554 #define MKARRAY(low,num,typ) sab.lLbound = low; sab.cElements = num; \
1555   sa = SafeArrayCreate(typ, 1, &sab); ok(sa != NULL, "Create() failed.\n"); \
1556   if (!sa) return; \
1557   V_VT(&v) = VT_ARRAY|typ; V_ARRAY(&v) = sa; VariantInit(&v2)
1558
1559 #define MKARRAYCONT(low,num,typ) sab.lLbound = low; sab.cElements = num; \
1560   sa = SafeArrayCreate(typ, 1, &sab); if (!sa) continue; \
1561   V_VT(&v) = VT_ARRAY|typ; V_ARRAY(&v) = sa; VariantInit(&v2)
1562
1563 static void test_SafeArrayChangeTypeEx(void)
1564 {
1565   static const char *szHello = "Hello World";
1566   SAFEARRAYBOUND sab;
1567   SAFEARRAY *sa;
1568   VARIANTARG v,v2;
1569   VARTYPE vt;
1570   HRESULT hres;
1571
1572   /* VT_ARRAY|VT_UI1 -> VT_BSTR */
1573   MKARRAY(0,strlen(szHello)+1,VT_UI1);
1574   memcpy(sa->pvData, szHello, strlen(szHello)+1);
1575
1576   hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_BSTR);
1577   ok(hres == S_OK, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR failed with %x\n", hres);
1578   if (hres == S_OK)
1579   {
1580     ok(V_VT(&v2) == VT_BSTR, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR did not return VT_BSTR, but %d.\n",V_VT(&v2));
1581     ok(strcmp((char*)V_BSTR(&v2),szHello) == 0,"Expected string '%s', got '%s'\n", szHello,
1582        (char*)V_BSTR(&v2));
1583     VariantClear(&v2);
1584   }
1585
1586   /* VT_VECTOR|VT_UI1 -> VT_BSTR */
1587   SafeArrayDestroy(sa);
1588   if (pSafeArrayCreateVector)
1589   {
1590     sa = pSafeArrayCreateVector(VT_UI1, 0, strlen(szHello)+1);
1591     ok(sa != NULL, "CreateVector() failed.\n");
1592     if (!sa)
1593       return;
1594
1595     memcpy(sa->pvData, szHello, strlen(szHello)+1);
1596     V_VT(&v) = VT_VECTOR|VT_UI1;
1597     V_ARRAY(&v) = sa;
1598     VariantInit(&v2);
1599
1600     hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_BSTR);
1601     ok(hres == DISP_E_BADVARTYPE, "CTE VT_VECTOR|VT_UI1 returned %x\n", hres);
1602
1603     /* (vector)VT_ARRAY|VT_UI1 -> VT_BSTR (In place) */
1604     V_VT(&v) = VT_ARRAY|VT_UI1;
1605     hres = VariantChangeTypeEx(&v, &v, 0, 0, VT_BSTR);
1606     ok(hres == S_OK, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR failed with %x\n", hres);
1607     if (hres == S_OK)
1608     {
1609       ok(V_VT(&v) == VT_BSTR, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR did not return VT_BSTR, but %d.\n",V_VT(&v));
1610       ok(strcmp((char*)V_BSTR(&v),szHello) == 0,"Expected string '%s', got '%s'\n", szHello,
1611               (char*)V_BSTR(&v));
1612       VariantClear(&v);
1613     }
1614   }
1615
1616   /* To/from BSTR only works with arrays of VT_UI1 */
1617   for (vt = 0; vt <= VT_CLSID; vt++)
1618   {
1619     if (vt == VT_UI1)
1620       continue;
1621
1622     MKARRAYCONT(0,1,vt);
1623     hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_BSTR);
1624     ok(hres != S_OK, "CTE VT_ARRAY|VT %d->BSTR succeeded\n", vt);
1625     VariantClear(&v2);
1626     VariantClear(&v);
1627   }
1628
1629   /* Can't change an array of one type into array of another type , even
1630    * if the other type is the same size
1631    */
1632   if (pSafeArrayCreateVector)
1633   {
1634     sa = pSafeArrayCreateVector(VT_UI1, 0, 1);
1635     ok(sa != NULL, "CreateVector() failed.\n");
1636     if (!sa)
1637       return;
1638
1639     V_VT(&v) = VT_ARRAY|VT_UI1;
1640     V_ARRAY(&v) = sa;
1641     hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_ARRAY|VT_I1);
1642     ok(hres == DISP_E_TYPEMISMATCH, "CTE VT_ARRAY|VT_UI1->VT_ARRAY|VT_I1 returned %x\n", hres);
1643
1644     /* But can change to the same array type */
1645     SafeArrayDestroy(sa);
1646     sa = pSafeArrayCreateVector(VT_UI1, 0, 1);
1647     ok(sa != NULL, "CreateVector() failed.\n");
1648     if (!sa)
1649       return;
1650     V_VT(&v) = VT_ARRAY|VT_UI1;
1651     V_ARRAY(&v) = sa;
1652     hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_ARRAY|VT_UI1);
1653     ok(hres == S_OK, "CTE VT_ARRAY|VT_UI1->VT_ARRAY|VT_UI1 returned %x\n", hres);
1654     SafeArrayDestroy(sa);
1655   }
1656
1657   /* NULL/EMPTY */
1658   MKARRAY(0,1,VT_UI1);
1659   hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_NULL);
1660   ok(hres == DISP_E_TYPEMISMATCH, "CTE VT_ARRAY|VT_UI1 returned %x\n", hres);
1661   MKARRAY(0,1,VT_UI1);
1662   hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_EMPTY);
1663   ok(hres == DISP_E_TYPEMISMATCH, "CTE VT_ARRAY|VT_UI1 returned %x\n", hres);
1664
1665 }
1666
1667 static void test_SafeArrayDestroyData (void)
1668 {
1669   SAFEARRAYBOUND sab;
1670   SAFEARRAY *sa;
1671   HRESULT hres;
1672   int value = 0xdeadbeef;
1673   LONG index[1];
1674   void HUGEP *temp_pvData;
1675
1676   sab.lLbound = 0;
1677   sab.cElements = 10;
1678   sa = SafeArrayCreate(VT_INT, 1, &sab);
1679   ok(sa != NULL, "Create() failed.\n");
1680   if (!sa)
1681     return;
1682   index[0] = 1;
1683   SafeArrayPutElement (sa, index, &value);
1684
1685 /* SafeArrayDestroyData shouldn't free pvData if FADF_STATIC is set. */
1686   sa->fFeatures |= FADF_STATIC;
1687   temp_pvData = sa->pvData;
1688   hres = SafeArrayDestroyData(sa);
1689   ok(hres == S_OK, "SADData FADF_STATIC failed, error code %x.\n",hres);
1690   ok(sa->pvData == temp_pvData, "SADData FADF_STATIC: pvData=%p, expected %p (fFeatures = %d).\n",
1691     sa->pvData, temp_pvData, sa->fFeatures);
1692   SafeArrayGetElement (sa, index, &value);
1693   ok(value == 0, "Data not cleared after SADData\n");
1694
1695 /* Clear FADF_STATIC, now really destroy the data. */
1696   sa->fFeatures ^= FADF_STATIC;
1697   hres = SafeArrayDestroyData(sa);
1698   ok(hres == S_OK, "SADData !FADF_STATIC failed, error code %x.\n",hres);
1699   ok(sa->pvData == NULL, "SADData !FADF_STATIC: pvData=%p, expected NULL.\n", sa->pvData);
1700
1701   hres = SafeArrayDestroy(sa);
1702   ok(hres == S_OK, "SAD failed, error code %x.\n", hres);
1703 }
1704
1705 START_TEST(safearray)
1706 {
1707     hOleaut32 = GetModuleHandleA("oleaut32.dll");
1708
1709     GETPTR(SafeArrayAllocDescriptorEx);
1710     GETPTR(SafeArrayCopyData);
1711     GETPTR(SafeArrayGetIID);
1712     GETPTR(SafeArraySetIID);
1713     GETPTR(SafeArrayGetVartype);
1714     GETPTR(SafeArrayCreateEx);
1715     GETPTR(SafeArrayCreateVector);
1716
1717     check_for_VT_INT_PTR();
1718     test_safearray();
1719     test_SafeArrayAllocDestroyDescriptor();
1720     test_SafeArrayCreateLockDestroy();
1721     test_VectorCreateLockDestroy();
1722     test_LockUnlock();
1723     test_SafeArrayChangeTypeEx();
1724     test_SafeArrayCopy();
1725     test_SafeArrayClear();
1726     test_SafeArrayCreateEx();
1727     test_SafeArrayCopyData();
1728     test_SafeArrayDestroyData();
1729     test_SafeArrayGetPutElement();
1730     test_SafeArrayGetPutElement_BSTR();
1731     test_SafeArrayGetPutElement_IUnknown();
1732     test_SafeArrayRedim_IUnknown();
1733     test_SafeArrayGetPutElement_VARIANT();
1734 }