9 V_VT(v) = VT_ARRAY | v_type;
10 SAFEARRAYBOUND* sab =
new SAFEARRAYBOUND[ndims];
16 for(i=0; i<ndims; i++)
19 sab[i].cElements = dims_array[ndims-i-1];
21 V_UNION(v,parray) = SafeArrayCreate(v_type, ndims, sab);
23 if (V_UNION(v,parray) == NULL)
36 hr = SafeArrayGetVartype(V_UNION(v,parray), &vtt);
41 hr = SafeArrayAccessData(V_UNION(v,parray), values);
42 if ( FAILED(hr) || (*values == NULL) )
77 HRESULT hr = SafeArrayUnaccessData(V_UNION(v,parray));
88 if (!(V_VT(v) & VT_ARRAY))
92 SAFEARRAY* psa = V_UNION(v,parray);
97 int ndims = SafeArrayGetDim(psa);
102 long lbounds, ubounds;
105 for(i=0; i<ndims; i++)
107 lbounds = ubounds = 0;
108 SafeArrayGetLBound(psa, i+1, &lbounds);
109 SafeArrayGetUBound(psa, i+1, &ubounds);
110 len *= (ubounds - lbounds + 1);
119 if (!(V_VT(v) & VT_ARRAY))
123 SAFEARRAY* psa = V_UNION(v,parray);
128 ndims = SafeArrayGetDim(psa);
129 long lbounds, ubounds;
130 for(
int i=0; i<ndims; i++)
132 lbounds = ubounds = 0;
133 SafeArrayGetLBound(psa, i+1, &lbounds);
134 SafeArrayGetUBound(psa, i+1, &ubounds);
135 dims_array[i] = ubounds - lbounds + 1;
140 template <
typename T>
149 int n = the_array.size();
151 BSTR* v_array = NULL;
153 for(
int i=0; i<n; ++i)
155 CComBSTR bstr_wrapper(the_array[i].c_str());
156 v_array[i] = bstr_wrapper.Detach();
162 template <
typename T>
165 VARTYPE vt_type = VT_R4;
169 for(
int i=0; i<n; ++i)
171 v_array[i] = the_array[i];
187 static void GenieVariableToVariant(GenieVariable& gv, VARIANT* v,
const char* opt)
190 static OOP gxcominterface = typeNameToOOP(
"GXCominterface");
197 case GenieIntegerType:
199 V_UNION(v,lVal) = (GenieInteger)gv;
204 V_UNION(v,dblVal) = (GenieReal)gv;
207 case GenieStringType:
209 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
210 (GenieString)gv, -1, olestr_buffer, MAX_OLE_SIZE);
211 V_UNION(v,bstrVal) = SysAllocString(olestr_buffer);
214 case GenieWorkspaceType:
216 GenieWorkspace& gw = (GenieWorkspace&)gv;
217 if (gw.getClass() == gxcominterface)
219 GenieIntegerArray gia = gw(
"value");
220 l = (GenieInteger)gw(
"type");
225 memcpy(&(V_UNION(v,punkVal)), (g_integer*)gia,
sizeof(IUnknown*));
229 memcpy(&(V_UNION(v,pdispVal)), (g_integer*)gia,
sizeof(IDispatch*));
233 gerr <<
"DCOM: GenieVariableToVariant(workspace, unknown type) " << l << std::endl;
239 gerr <<
"DCOM: Unsupported workspace type" << std::endl;
244 case GenieWorkspaceArrayType:
246 GenieWorkspaceArray& gwa = (GenieWorkspaceArray&)gv;
248 tmp = (VARIANT*)safeArraySetup(gwa, v, VT_VARIANT);
251 gerr <<
"DCOM: error" << std::endl;
254 for(i=0; i<gwa.length(); i++)
256 GenieWorkspace gw = gwa[i];
257 GenieVariable gv = gw(
"value");
258 GenieVariableToVariant(gv, &tmp[i],
"");
260 hr = SafeArrayUnaccessData(V_UNION(v,parray));
263 gerr <<
"DCOM: failed" << std::endl;
269 case GenieIntegerArrayType:
271 GenieIntegerArray& gia = (GenieIntegerArray&)gv;
273 vt_type = (*opt ==
'A' ? VT_I4 : VT_VARIANT);
274 tmp = (VARIANT*)safeArraySetup(gia, v, vt_type);
277 gerr <<
"DCOM: error" << std::endl;
280 if (vt_type == VT_VARIANT)
282 for(i=0; i<gia.length(); i++)
284 V_VT(&tmp[i]) = VT_I4;
285 V_UNION(&tmp[i],lVal) = gia[i];
290 memcpy(tmp, (g_integer*)gia, gia.length() * 4);
292 hr = SafeArrayUnaccessData(V_UNION(v,parray));
295 gerr <<
"DCOM: failed" << std::endl;
300 case GenieRealArrayType:
302 GenieRealArray& gra = (GenieRealArray&)gv;
304 vt_type = (*opt ==
'A' ? VT_R8 : VT_VARIANT);
305 tmp = (VARIANT*)safeArraySetup(gra, v, vt_type);
308 gerr <<
"DCOM: error" << std::endl;
311 if (vt_type == VT_VARIANT)
313 for(i=0; i<gra.length(); i++)
315 V_VT(&tmp[i]) = VT_R8;
316 V_UNION(&tmp[i],dblVal) = gra[i];
321 memcpy(tmp, (g_real*)gra, gra.length() * 8);
323 hr = SafeArrayUnaccessData(V_UNION(v,parray));
326 gerr <<
"DCOM: failed" << std::endl;
331 case GenieStringArrayType:
333 GenieStringArray& gsa = (GenieStringArray&)gv;
335 vt_type = (*opt ==
'A' ? VT_BSTR : VT_VARIANT);
336 tmp = (VARIANT*)safeArraySetup(gsa, v, vt_type);
339 gerr <<
"DCOM: error" << std::endl;
342 for(i=0; i<gsa.length(); i++)
344 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
345 (GenieString)gsa[i], -1, olestr_buffer,
347 if (vt_type == VT_VARIANT)
349 V_VT(&tmp[i]) = VT_BSTR;
350 V_UNION(&tmp[i],bstrVal) = SysAllocString(olestr_buffer);
354 ((BSTR*)tmp)[i] = SysAllocString(olestr_buffer);
357 hr = SafeArrayUnaccessData(V_UNION(v,parray));
360 gerr <<
"DCOM: failed" << std::endl;
366 gerr <<
"DCOM: GenieVariableToVariant(unknown type) " << std::endl;
371 static void VariantToGenieVariable(VARIANT* v, GenieVariable& gv);
373 static int getVariantArrayLength(VARIANT* v)
375 if (!(V_VT(v) & VT_ARRAY))
379 SAFEARRAY* psa = V_UNION(v,parray);
384 int ndims = SafeArrayGetDim(psa);
385 long lbounds, ubounds;
388 for(i=0; i<ndims; i++)
390 lbounds = ubounds = 0;
391 SafeArrayGetLBound(psa, i+1, &lbounds);
392 SafeArrayGetUBound(psa, i+1, &ubounds);
393 len *= (ubounds - lbounds + 1);
398 static void VariantToGenieVariableArray(VARIANT* v, GenieVariable& gv)
400 SAFEARRAY* psa = V_UNION(v,parray);
403 gerr <<
"DCOM: VariantToGenieVariableArray error" << std::endl;
406 int ndims = SafeArrayGetDim(psa);
407 long *lbounds =
new long[ndims];
408 long *ubounds =
new long[ndims];
409 int* dims_array =
new int[ndims];
413 for(i=0; i<ndims; i++)
415 SafeArrayGetLBound(psa, i+1, lbounds+i);
416 SafeArrayGetUBound(psa, i+1, ubounds+i);
417 dims_array[ndims - i - 1] = ubounds[i] - lbounds[i] + 1;
418 len *= dims_array[ndims - i - 1];
422 vvt = (V_VT(v) & ~VT_ARRAY);
425 hr = SafeArrayGetVartype(psa, &vt);
426 if ( FAILED(hr) || (vt != vvt) )
428 gerr <<
"DCOM: VariantToGenieVariableArray - possible data type mismatch?" << std::endl;
434 hr = SafeArrayAccessData(psa, &pdata);
435 if ( FAILED(hr) || (pdata == NULL) )
437 gerr <<
"DCOM: error" << std::endl;
440 GenieIntegerArray* gia;
442 GenieStringArray *gsa;
443 GenieWorkspaceArray *gwa;
445 if (vt == VT_VARIANT)
448 gwa =
new GenieWorkspaceArray(len);
451 pv = (VARIANT*)pdata + i;
453 VariantToGenieVariable(pv, gvv);
455 gw(
"value", gvv.value());
458 gv.newType(GenieWorkspaceArrayType, gwa->value());
474 gia =
new GenieIntegerArray((
long*)pdata, dims_array, ndims);
475 gv.newType(GenieIntegerArrayType, gia->value());
481 gia =
new GenieIntegerArray((
int*)pdata, dims_array, ndims);
482 gv.newType(GenieIntegerArrayType, gia->value());
486 gra =
new GenieRealArray((
float*)pdata, dims_array, ndims);
487 gv.newType(GenieRealArrayType, gra->value());
491 gra =
new GenieRealArray((
double*)pdata, dims_array, ndims);
492 gv.newType(GenieRealArrayType, gra->value());
496 gsa =
new GenieStringArray(dims_array, ndims);
500 pBstr = (BSTR*)pdata + i;
501 l = SysStringLen(*pBstr);
502 j = WideCharToMultiByte(CP_ACP, 0, *pBstr, l,
503 str_buffer, MAX_OLE_SIZE, NULL, NULL);
504 str_buffer[j] =
'\0';
505 (*gsa)(i, str_buffer);
507 gv.newType(GenieStringArrayType, gsa->value());
511 gerr <<
"VariantToGenieVariableArray(unknown type) " << vt << std::endl;
515 SafeArrayUnaccessData(psa);
521 static void VariantToGenieVariable(VARIANT* v, GenieVariable& gv)
527 VariantInit(&temp_v);
528 GenieIntegerArray gia(2);
529 if (V_VT(v) & VT_ARRAY)
532 VariantToGenieVariableArray(v, gv);
550 VariantChangeType(&temp_v, v, 0, VT_I4);
551 gi = V_UNION(&temp_v,lVal);
552 gv.newType(GenieIntegerType, gi);
559 gi = (V_UNION(v,boolVal) != 0 ? 1 : 0);
560 gv.newType(GenieIntegerType, gi);
568 VariantChangeType(&temp_v, v, 0, VT_R8);
569 gr = V_UNION(&temp_v,dblVal);
570 gv.newType(GenieRealType, gr);
576 l = SysStringLen(V_UNION(v,bstrVal));
577 i = WideCharToMultiByte(CP_ACP, 0, V_UNION(v,bstrVal), l,
578 str_buffer, MAX_OLE_SIZE, NULL, NULL);
579 str_buffer[i] =
'\0';
581 gv.newType(GenieStringType, gs);
587 GenieWorkspace gw(
"GXCominterface");
588 gia((g_integer*)&(V_UNION(v,punkVal)),
sizeof(IUnknown*) /
sizeof(g_integer));
589 gw(
"name",
"IUnknown");
590 gw(
"type", VT_UNKNOWN);
592 gv.newType(GenieWorkspaceType, gw);
593 iunk_list.push(iunk_item(V_UNION(v,punkVal)));
599 GenieWorkspace gw(
"GXCominterface");
600 gia((g_integer*)&(V_UNION(v,pdispVal)),
sizeof(IDispatch*) /
sizeof(g_integer));
601 gw(
"name",
"IDispatch");
602 gw(
"type", VT_DISPATCH);
604 gv.newType(GenieWorkspaceType, gw);
605 idisp_list.push(idisp_item(V_UNION(v,pdispVal), 0));
611 gerr <<
"DCOM: VariantToGenieVariable(unknown type) " << V_VT(v) << std::endl;
618 static void DCOMCleanupVariant(VARIANT* v)
625 vt = (vvt & ~VT_ARRAY);
626 if ( (vvt & VT_ARRAY) && (vt == VT_VARIANT) )
628 SAFEARRAY* psa = V_UNION(v,parray);
629 len = getVariantArrayLength(v);
630 hr = SafeArrayAccessData(psa, (
void**)&pv);
631 if ( SUCCEEDED(hr) && (pv != NULL) )
635 DCOMCleanupVariant(pv + i);
637 SafeArrayUnaccessData(psa);
666 gerr <<
"DCOM: CleanupVariant(unknown type) " << V_VT(v)<<
"/" << vt << std::endl;
int allocateArrayVariant(VARIANT *v, VARTYPE v_type, int *dims_array, int ndims)
static int accessArrayVariant(VARIANT *v, void **values, VARTYPE vt)
int arrayVariantLength(VARIANT *v)
int arrayVariantDimensions(VARIANT *v, int dims_array[], int &ndims)
int unaccessArrayVariant(VARIANT *v)
int makeVariantFromArray(VARIANT *v, const std::vector< T > &the_array)