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