2 * SafeArray test program
4 * Copyright 2002 Marcus Meissner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "wine/test.h"
42 static HRESULT (WINAPI *pSafeArrayAllocDescriptorEx)(VARTYPE,UINT,struct tagSAFEARRAY**)=NULL;
43 static HRESULT (WINAPI *pSafeArrayCopyData)(struct tagSAFEARRAY*,struct tagSAFEARRAY*)=NULL;
44 static HRESULT (WINAPI *pSafeArrayGetIID)(struct tagSAFEARRAY*,GUID*)=NULL;
45 static HRESULT (WINAPI *pSafeArraySetIID)(struct tagSAFEARRAY*,REFGUID)=NULL;
46 static HRESULT (WINAPI *pSafeArrayGetVartype)(struct tagSAFEARRAY*,VARTYPE*)=NULL;
48 #define VARTYPE_NOT_SUPPORTED 0
51 UINT elemsize; /* elementsize by VT */
52 UINT expflags; /* fFeatures from SafeArrayAllocDescriptorEx */
53 UINT addflags; /* additional fFeatures from SafeArrayCreate */
55 {VT_EMPTY, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
56 {VT_NULL, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
57 {VT_I2, 2, FADF_HAVEVARTYPE,0},
58 {VT_I4, 4, FADF_HAVEVARTYPE,0},
59 {VT_R4, 4, FADF_HAVEVARTYPE,0},
60 {VT_R8, 8, FADF_HAVEVARTYPE,0},
61 {VT_CY, 8, FADF_HAVEVARTYPE,0},
62 {VT_DATE, 8, FADF_HAVEVARTYPE,0},
63 {VT_BSTR, sizeof(BSTR), FADF_HAVEVARTYPE,FADF_BSTR},
64 {VT_DISPATCH, sizeof(LPDISPATCH), FADF_HAVEIID, FADF_DISPATCH},
65 {VT_ERROR, 4, FADF_HAVEVARTYPE,0},
66 {VT_BOOL, 2, FADF_HAVEVARTYPE,0},
67 {VT_VARIANT, sizeof(VARIANT), FADF_HAVEVARTYPE,FADF_VARIANT},
68 {VT_UNKNOWN, sizeof(LPUNKNOWN), FADF_HAVEIID, FADF_UNKNOWN},
69 {VT_DECIMAL, sizeof(DECIMAL), FADF_HAVEVARTYPE,0},
70 {15, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0}, /* no VT_xxx */
71 {VT_I1, 1, FADF_HAVEVARTYPE,0},
72 {VT_UI1, 1, FADF_HAVEVARTYPE,0},
73 {VT_UI2, 2, FADF_HAVEVARTYPE,0},
74 {VT_UI4, 4, FADF_HAVEVARTYPE,0},
75 {VT_I8, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
76 {VT_UI8, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
77 {VT_INT, sizeof(INT), FADF_HAVEVARTYPE,0},
78 {VT_UINT, sizeof(UINT), FADF_HAVEVARTYPE,0},
79 {VT_VOID, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
80 {VT_HRESULT, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
81 {VT_PTR, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
82 {VT_SAFEARRAY,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
83 {VT_CARRAY, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
84 {VT_USERDEFINED,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
85 {VT_LPSTR, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
86 {VT_LPWSTR, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
87 {VT_FILETIME, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
88 {VT_RECORD, VARTYPE_NOT_SUPPORTED,FADF_RECORD,0},
89 {VT_BLOB, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
90 {VT_STREAM, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
91 {VT_STORAGE, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
92 {VT_STREAMED_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
93 {VT_STORED_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
94 {VT_BLOB_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
95 {VT_CF, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
96 {VT_CLSID, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
106 SAFEARRAYBOUND bound, bounds[2];
112 unsigned char *ptr1, *ptr2;
114 hdll=LoadLibraryA("oleaut32.dll");
115 pSafeArrayAllocDescriptorEx=(void*)GetProcAddress(hdll,"SafeArrayAllocDescriptorEx");
116 pSafeArrayCopyData=(void*)GetProcAddress(hdll,"SafeArrayCopyData");
117 pSafeArrayGetIID=(void*)GetProcAddress(hdll,"SafeArrayGetIID");
118 pSafeArraySetIID=(void*)GetProcAddress(hdll,"SafeArraySetIID");
119 pSafeArrayGetVartype=(void*)GetProcAddress(hdll,"SafeArrayGetVartype");
121 hres = SafeArrayAllocDescriptor(0,&a);
122 ok(E_INVALIDARG == hres,"SAAD(0) failed with hres %lx",hres);
124 hres=SafeArrayAllocDescriptor(1,&a);
125 ok(S_OK == hres,"SAAD(1) failed with %lx",hres);
127 for (i=1;i<100;i++) {
128 hres=SafeArrayAllocDescriptor(i,&a);
129 ok(S_OK == hres,"SAAD(%d) failed with %lx",i,hres);
131 ok(a->cDims == i,"a->cDims not initialised?");
133 hres=SafeArrayDestroyDescriptor(a);
134 ok(S_OK == hres,"SADD failed with %lx",hres);
137 hres=SafeArrayAllocDescriptor(65535,&a);
138 ok(S_OK == hres,"SAAD(65535) failed with %lx",hres);
140 hres=SafeArrayDestroyDescriptor(a);
141 ok(S_OK == hres,"SADD failed with %lx",hres);
143 hres=SafeArrayAllocDescriptor(65536,&a);
144 ok(E_INVALIDARG == hres,"SAAD(65536) failed with %lx",hres);
146 /* Crashes on Win95: SafeArrayAllocDescriptor(xxx,NULL) */
150 a = SafeArrayCreate(-1, 1, &bound);
151 ok(NULL == a,"SAC(-1,1,[1,0]) not failed?");
154 bounds[0].cElements = 42; bounds[0].lLbound = 1;
155 bounds[1].cElements = 2; bounds[1].lLbound = 23;
156 a = SafeArrayCreate(VT_I4,2,bounds);
157 ok(a != NULL,"SAC(VT_INT32,2,...) failed.");
159 hres = SafeArrayGetLBound (a, 0, &l);
160 ok (hres == DISP_E_BADINDEX, "SAGLB 0 failed with %lx", hres);
161 hres = SafeArrayGetLBound (a, 1, &l);
162 ok (hres == S_OK, "SAGLB 1 failed with %lx", hres);
163 ok (l == 1, "SAGLB 1 returned %ld instead of 1", l);
164 hres = SafeArrayGetLBound (a, 2, &l);
165 ok (hres == S_OK, "SAGLB 2 failed with %lx", hres);
166 ok (l == 23, "SAGLB 2 returned %ld instead of 1", l);
167 hres = SafeArrayGetLBound (a, 3, &l);
168 ok (hres == DISP_E_BADINDEX, "SAGLB 3 failed with %lx", hres);
170 hres = SafeArrayGetUBound (a, 0, &l);
171 ok (hres == DISP_E_BADINDEX, "SAGUB 0 failed with %lx", hres);
172 hres = SafeArrayGetUBound (a, 1, &l);
173 ok (hres == S_OK, "SAGUB 1 failed with %lx", hres);
174 ok (l == 42, "SAGUB 1 returned %ld instead of 1", l);
175 hres = SafeArrayGetUBound (a, 2, &l);
176 ok (hres == S_OK, "SAGUB 2 failed with %lx", hres);
177 ok (l == 24, "SAGUB 2 returned %ld instead of 24", l);
178 hres = SafeArrayGetUBound (a, 3, &l);
179 ok (hres == DISP_E_BADINDEX, "SAGUB 3 failed with %lx", hres);
181 i = SafeArrayGetDim(a);
182 ok(i == 2, "getdims of 2 din array returned %d",i);
186 hres = SafeArrayGetElement(a, indices, &i);
187 ok(DISP_E_BADINDEX == hres,"SAGE failed [0,23], hres 0x%lx",hres);
191 hres = SafeArrayGetElement(a, indices, &i);
192 ok(DISP_E_BADINDEX == hres,"SAGE failed [1,22], hres 0x%lx",hres);
196 hres = SafeArrayGetElement(a, indices, &i);
197 ok(S_OK == hres,"SAGE failed [1,23], hres 0x%lx",hres);
201 hres = SafeArrayGetElement(a, indices, &i);
202 ok(DISP_E_BADINDEX == hres,"SAGE failed [1,24], hres 0x%lx",hres);
206 hres = SafeArrayGetElement(a, indices, &i);
207 ok(S_OK == hres,"SAGE failed [42,23], hres 0x%lx",hres);
209 hres = SafeArrayAccessData(a, (void**)&ptr1);
210 ok(S_OK == hres, "SAAD failed with 0x%lx", hres);
214 hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
215 ok(S_OK == hres,"SAPOI failed [1,23], hres 0x%lx",hres);
216 ok(ptr2 - ptr1 == 8,"ptr difference is not 8, but %d (%p vs %p)", ptr2-ptr1, ptr2, ptr1);
220 hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
221 ok(S_OK == hres,"SAPOI failed [5,24], hres 0x%lx",hres);
222 ok(ptr2 - ptr1 == 176,"ptr difference is not 176, but %d (%p vs %p)", ptr2-ptr1, ptr2, ptr1);
226 hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
227 ok(S_OK == hres,"SAPOI failed [20,23], hres 0x%lx",hres);
228 ok(ptr2 - ptr1 == 76,"ptr difference is not 176, but %d (%p vs %p)", ptr2-ptr1, ptr2, ptr1);
230 hres = SafeArrayUnaccessData(a);
231 ok(S_OK == hres, "SAUAD failed with 0x%lx", hres);
233 for (i=0;i<sizeof(vttypes)/sizeof(vttypes[0]);i++) {
234 a = SafeArrayCreate(vttypes[i].vt, 1, &bound);
235 ok( ((a == NULL) && (vttypes[i].elemsize == 0)) ||
236 ((a != NULL) && (vttypes[i].elemsize == a->cbElements)),
237 "SAC(%d,1,[1,0]), result %ld, expected %d",vttypes[i].vt,(a?a->cbElements:0),vttypes[i].elemsize
240 ok(a->fFeatures == (vttypes[i].expflags | vttypes[i].addflags),
241 "SAC of %d returned feature flags %x, expected %x",
242 vttypes[i].vt, a->fFeatures,
243 vttypes[i].expflags|vttypes[i].addflags);
244 ok(SafeArrayGetElemsize(a) == vttypes[i].elemsize,
245 "SAGE for vt %d returned elemsize %d instead of expected %d",
246 vttypes[i].vt, SafeArrayGetElemsize(a),vttypes[i].elemsize);
251 if (pSafeArrayGetVartype) {
252 hres = pSafeArrayGetVartype(a, &vt);
253 ok(hres == S_OK, "SAGVT of arra y with vt %d failed with %lx", vttypes[i].vt, hres);
254 if (vttypes[i].vt == VT_DISPATCH) {
255 /* Special case. Checked against Windows. */
256 ok(vt == VT_UNKNOWN, "SAGVT of a rray with VT_DISPATCH returned not VT_UNKNOWN, but %d", vt);
258 ok(vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d", vttypes[i].vt, vt);
262 hres = SafeArrayCopy(a, &c);
263 ok(hres == S_OK, "failed to copy safearray of vt %d with hres %lx", vttypes[i].vt, hres);
265 ok(vttypes[i].elemsize == c->cbElements,"copy of SAC(%d,1,[1,0]), result %ld, expected %d",vttypes[i].vt,(c?c->cbElements:0),vttypes[i].elemsize
267 ok(c->fFeatures == (vttypes[i].expflags | vttypes[i].addflags),"SAC of %d returned feature flags %x, expected %x", vttypes[i].vt, c->fFeatures, vttypes[i].expflags|vttypes[i].addflags);
268 ok(SafeArrayGetElemsize(c) == vttypes[i].elemsize,"SAGE for vt %d returned elemsize %d instead of expected %d",vttypes[i].vt, SafeArrayGetElemsize(c),vttypes[i].elemsize);
270 if (pSafeArrayGetVartype) {
271 hres = pSafeArrayGetVartype(c, &vt);
272 ok(hres == S_OK, "SAGVT of array with vt %d failed with %lx", vttypes[i].vt, hres);
273 if (vttypes[i].vt == VT_DISPATCH) {
274 /* Special case. Checked against Windows. */
275 ok(vt == VT_UNKNOWN, "SAGVT of array with VT_DISPATCH returned not VT_UNKNOWN, but %d", vt);
277 ok(vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d", vttypes[i].vt, vt);
281 if (pSafeArrayCopyData) {
282 hres = pSafeArrayCopyData(a, c);
283 ok(hres == S_OK, "failed to copy safearray data of vt %d with hres %lx", vttypes[i].vt, hres);
285 hres = SafeArrayDestroyData(c);
286 ok(hres == S_OK,"SADD of copy of array with vt %d failed with hres %lx", vttypes[i].vt, hres);
289 hres = SafeArrayDestroy(a);
290 ok(hres == S_OK,"SAD of array with vt %d failed with hres %lx", vttypes[i].vt, hres);
293 /* Test conversion of type|VT_ARRAY <-> VT_BSTR */
295 bound.cElements = 10;
296 a = SafeArrayCreate(VT_UI1, 1, &bound);
297 ok(a != NULL, "SAC failed.");
298 ok(S_OK == SafeArrayAccessData(a, &data),"SACD failed");
299 memcpy(data,"Hello World",10);
300 ok(S_OK == SafeArrayUnaccessData(a),"SAUD failed");
301 V_VT(&v) = VT_ARRAY|VT_UI1;
303 hres = VariantChangeTypeEx(&v, &v, 0, 0, VT_BSTR);
304 ok(hres==S_OK, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR failed with %lx",hres);
305 ok(V_VT(&v) == VT_BSTR,"CTE VT_ARRAY|VT_UI1 -> VT_BSTR did not return VT_BSTR, but %d.",V_VT(&v));
306 ok(V_BSTR(&v)[0] == 0x6548,"First letter are not 'He', but %x", V_BSTR(&v)[0]);
308 /* check locking functions */
309 a = SafeArrayCreate(VT_I4, 1, &bound);
310 ok(a!=NULL,"SAC should not fail");
312 hres = SafeArrayAccessData(a, &data);
313 ok(hres == S_OK,"SAAD failed with hres %lx",hres);
315 hres = SafeArrayDestroy(a);
316 ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy not failed with DISP_E_ARRAYISLOCKED, but with hres %lx", hres);
318 hres = SafeArrayDestroyData(a);
319 ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy data not failed with DISP_E_ARRAYISLOCKED, but with hres %lx", hres);
321 hres = SafeArrayDestroyDescriptor(a);
322 ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy descriptor not failed with DISP_E_ARRAYISLOCKED, but with hres %lx", hres);
324 hres = SafeArrayUnaccessData(a);
325 ok(hres == S_OK,"SAUD failed after lock/destroy test");
327 hres = SafeArrayDestroy(a);
328 ok(hres == S_OK,"SAD failed after lock/destroy test");
330 /* Test if we need to destroy data before descriptor */
331 a = SafeArrayCreate(VT_I4, 1, &bound);
332 ok(a!=NULL,"SAC should not fail");
333 hres = SafeArrayDestroyDescriptor(a);
334 ok(hres == S_OK,"SADD with data in array failed with hres %lx",hres);
338 /* init a small stack safearray */
339 if (pSafeArraySetIID) {
340 memset(&b, 0, sizeof(b));
342 memset(&iid, 0x42, sizeof(IID));
343 hres = pSafeArraySetIID(&b,&iid);
344 ok(hres == E_INVALIDARG,"SafeArraySetIID of non IID capable safearray did not return E_INVALIDARG, but %lx",hres);
346 hres = SafeArrayAllocDescriptor(1,&a);
347 ok((a->fFeatures & FADF_HAVEIID) == 0,"newly allocated descriptor with SAAD should not have FADF_HAVEIID");
348 hres = pSafeArraySetIID(a,&iid);
349 ok(hres == E_INVALIDARG,"SafeArraySetIID of newly allocated descriptor with SAAD should return E_INVALIDARG, but %lx",hres);
352 if (!pSafeArrayAllocDescriptorEx)
355 for (i=0;i<sizeof(vttypes)/sizeof(vttypes[0]);i++) {
356 hres = pSafeArrayAllocDescriptorEx(vttypes[i].vt,1,&a);
357 ok(a->fFeatures == vttypes[i].expflags,"SAADE(%d) resulted with flags %x, expected %x\n", vttypes[i].vt, a->fFeatures, vttypes[i].expflags);
358 if (a->fFeatures & FADF_HAVEIID) {
359 hres = pSafeArrayGetIID(a, &iid);
360 ok(hres == S_OK,"SAGIID failed for vt %d with hres %lx", vttypes[i].vt,hres);
361 switch (vttypes[i].vt) {
363 ok(IsEqualGUID(((GUID*)a)-1,&IID_IUnknown),"guid for VT_UNKNOWN is not IID_IUnknown");
364 ok(IsEqualGUID(&iid, &IID_IUnknown),"SAGIID returned wrong GUID for IUnknown");
367 ok(IsEqualGUID(((GUID*)a)-1,&IID_IDispatch),"guid for VT_UNKNOWN is not IID_IDispatch");
368 ok(IsEqualGUID(&iid, &IID_IDispatch),"SAGIID returned wrong GUID for IDispatch");
371 ok(FALSE,"unknown vt %d with FADF_HAVEIID",vttypes[i].vt);
375 hres = pSafeArrayGetIID(a, &iid);
376 ok(hres == E_INVALIDARG,"SAGIID did not fail for vt %d with hres %lx", vttypes[i].vt,hres);
378 if (a->fFeatures & FADF_RECORD) {
379 ok(vttypes[i].vt == VT_RECORD,"FADF_RECORD for non record %d",vttypes[i].vt);
381 if (a->fFeatures & FADF_HAVEVARTYPE) {
382 ok(vttypes[i].vt == ((DWORD*)a)[-1], "FADF_HAVEVARTYPE set, but vt %d mismatch stored %ld",vttypes[i].vt,((DWORD*)a)[-1]);
385 hres = pSafeArrayGetVartype(a, &vt);
386 ok(hres == S_OK, "SAGVT of array with vt %d failed with %lx", vttypes[i].vt, hres);
388 if (vttypes[i].vt == VT_DISPATCH) {
389 /* Special case. Checked against Windows. */
390 ok(vt == VT_UNKNOWN, "SAGVT of array with VT_DISPATCH returned not VT_UNKNOWN, but %d", vt);
392 ok(vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d", vttypes[i].vt, vt);
395 if (a->fFeatures & FADF_HAVEIID) {
396 hres = pSafeArraySetIID(a, &IID_IStorage); /* random IID */
397 ok(hres == S_OK,"SASIID failed with FADF_HAVEIID set for vt %d with %lx", vttypes[i].vt, hres);
398 hres = pSafeArrayGetIID(a, &iid);
399 ok(hres == S_OK,"SAGIID failed with FADF_HAVEIID set for vt %d with %lx", vttypes[i].vt, hres);
400 ok(IsEqualGUID(&iid, &IID_IStorage),"returned iid is not IID_IStorage");
402 hres = pSafeArraySetIID(a, &IID_IStorage); /* random IID */
403 ok(hres == E_INVALIDARG,"SASIID did not failed with !FADF_HAVEIID set for vt %d with %lx", vttypes[i].vt, hres);
405 hres = SafeArrayDestroyDescriptor(a);
406 ok(hres == S_OK,"SADD failed with hres %lx",hres);