Esempio n. 1
0
static zval *com_read_dimension(zval *object, zval *offset, int type, zval *rv)
{
	php_com_dotnet_object *obj;
	VARIANT v;

	ZVAL_NULL(rv);

	obj = CDNO_FETCH(object);

	if (V_VT(&obj->v) == VT_DISPATCH) {
		VariantInit(&v);

		if (SUCCESS == php_com_do_invoke_by_id(obj, DISPID_VALUE,
				DISPATCH_METHOD|DISPATCH_PROPERTYGET, &v, 1, offset, 0, 0)) {
			php_com_zval_from_variant(rv, &v, obj->code_page);
			VariantClear(&v);
		}
	} else if (V_ISARRAY(&obj->v)) {
		convert_to_long(offset);

		if (SafeArrayGetDim(V_ARRAY(&obj->v)) == 1) {
			if (php_com_safearray_get_elem(&obj->v, &v, (LONG)Z_LVAL_P(offset))) {
				php_com_wrap_variant(rv, &v, obj->code_page);
				VariantClear(&v);
			}
		} else {
			php_com_saproxy_create(object, rv, offset);
		}

	} else {
		php_com_throw_exception(E_INVALIDARG, "this variant is not an array type");
	}

	return rv;
}
Esempio n. 2
0
static unsigned long wire_extra_user_size(unsigned long *pFlags, unsigned long Start, VARIANT *pvar)
{
  if (V_ISARRAY(pvar))
  {
    if (V_ISBYREF(pvar))
      return LPSAFEARRAY_UserSize(pFlags, Start, V_ARRAYREF(pvar));
    else 
      return LPSAFEARRAY_UserSize(pFlags, Start, &V_ARRAY(pvar));
  }

  switch (V_VT(pvar)) {
  case VT_BSTR:
    return BSTR_UserSize(pFlags, Start, &V_BSTR(pvar));
  case VT_BSTR | VT_BYREF:
    return BSTR_UserSize(pFlags, Start, V_BSTRREF(pvar));
  case VT_VARIANT | VT_BYREF:
    return VARIANT_UserSize(pFlags, Start, V_VARIANTREF(pvar));
  case VT_UNKNOWN:
    return Start + interface_variant_size(pFlags, &IID_IUnknown, pvar);
  case VT_DISPATCH:
    return Start + interface_variant_size(pFlags, &IID_IDispatch, pvar);
  case VT_RECORD:
    FIXME("wire-size record\n");
    return Start;
  case VT_SAFEARRAY:
  case VT_SAFEARRAY | VT_BYREF:
    FIXME("wire-size safearray: shouldn't be marshaling this\n");
    return Start;
  default:
    return Start;
  }
}
Esempio n. 3
0
/*
 *  call-seq:
 *     WIN32OLE_VARIANT[i,j,...] #=> element of OLE array.
 *
 *  Returns the element of WIN32OLE_VARIANT object(OLE array).
 *  This method is available only when the variant type of
 *  WIN32OLE_VARIANT object is VT_ARRAY.
 *
 *  REMARK:
 *     The all indices should be 0 or natural number and
 *     lower than or equal to max indices.
 *     (This point is different with Ruby Array indices.)
 *
 *     obj = WIN32OLE_VARIANT.new([[1,2,3],[4,5,6]])
 *     p obj[0,0] # => 1
 *     p obj[1,0] # => 4
 *     p obj[2,0] # => WIN32OLERuntimeError
 *     p obj[0, -1] # => WIN32OLERuntimeError
 *
 */
static VALUE
folevariant_ary_aref(int argc, VALUE *argv, VALUE self)
{
    struct olevariantdata *pvar;
    SAFEARRAY *psa;
    VALUE val = Qnil;
    VARIANT variant;
    LONG *pid;
    HRESULT hr;

    TypedData_Get_Struct(self, struct olevariantdata, &olevariant_datatype, pvar);
    if (!V_ISARRAY(&(pvar->var))) {
        rb_raise(eWIN32OLERuntimeError,
                 "`[]' is not available for this variant type object");
    }
    psa = get_locked_safe_array(self);
    if (psa == NULL) {
        return val;
    }

    pid = ary2safe_array_index(argc, argv, psa);

    VariantInit(&variant);
    V_VT(&variant) = (V_VT(&(pvar->var)) & ~VT_ARRAY) | VT_BYREF;
    hr = SafeArrayPtrOfIndex(psa, pid, &V_BYREF(&variant));
    if (FAILED(hr)) {
        ole_raise(hr, eWIN32OLERuntimeError, "failed to SafeArrayPtrOfIndex");
    }
    val = ole_variant2val(&variant);

    unlock_safe_array(psa);
    if (pid) free(pid);
    return val;
}
Esempio n. 4
0
static void com_write_dimension(zval *object, zval *offset, zval *value)
{
	php_com_dotnet_object *obj;
	zval args[2];
	VARIANT v;
	HRESULT res;

	obj = CDNO_FETCH(object);

	if (V_VT(&obj->v) == VT_DISPATCH) {
		ZVAL_COPY_VALUE(&args[0], offset);
		ZVAL_COPY_VALUE(&args[1], value);

		VariantInit(&v);

		if (SUCCESS == php_com_do_invoke_by_id(obj, DISPID_VALUE,
				DISPATCH_METHOD|DISPATCH_PROPERTYPUT, &v, 2, args, 0, 0)) {
			VariantClear(&v);
		}
	} else if (V_ISARRAY(&obj->v)) {
		LONG indices = 0;
		VARTYPE vt;

		if (SafeArrayGetDim(V_ARRAY(&obj->v)) == 1) {
			if (FAILED(SafeArrayGetVartype(V_ARRAY(&obj->v), &vt)) || vt == VT_EMPTY) {
				vt = V_VT(&obj->v) & ~VT_ARRAY;
			}

			convert_to_long(offset);
			indices = (LONG)Z_LVAL_P(offset);

			VariantInit(&v);
			php_com_variant_from_zval(&v, value, obj->code_page);

			if (V_VT(&v) != vt) {
				VariantChangeType(&v, &v, 0, vt);
			}

			if (vt == VT_VARIANT) {
				res = SafeArrayPutElement(V_ARRAY(&obj->v), &indices, &v);
			} else {
				res = SafeArrayPutElement(V_ARRAY(&obj->v), &indices, &v.lVal);
			}

			VariantClear(&v);

			if (FAILED(res)) {
				php_com_throw_exception(res, NULL);
			}

		} else {
			php_com_throw_exception(DISP_E_BADINDEX, "this variant has multiple dimensions; you can't set a new value without specifying *all* dimensions");
		}

	} else {
		php_com_throw_exception(E_INVALIDARG, "this variant is not an array type");
	}
}
Esempio n. 5
0
STDMETHODIMP SoundDA::SetChannelProperty(long User, NESTABLEPROP *pChan, VARIANT *newValue)
{
    int chan=pChan->Index;   

    if (chan>2) return E_EXCEEDS_MAX_CHANNELS;
   
    long numChans;
    
    HRESULT hRes = _EngineChannelList->GetNumberOfChannels(&numChans);
    if (FAILED(hRes)) return hRes;
        

        if (User==HwChan) 
        {
            variant_t val = (variant_t*)newValue;
            if (V_VT(newValue)==VT_R8 && (double)val!=chan)
                return E_INVALID_CHANNEL;
        }
        // Winsound supports only an input range of [-1 1]
        else if (User==OUTPUTRANGE)
        {
            if (V_ISARRAY (newValue) || V_ISVECTOR (newValue))
            {                            
                SAFEARRAY *ps = newValue->parray;
                if (ps==NULL)
                    return E_OUTOFMEMORY;
                                
                double *pr;
                
                HRESULT hr = SafeArrayAccessData (ps, (void **) &pr);
                if (FAILED (hr)) 
                {
                    SafeArrayDestroy (ps);
                    return hr;
                }
                
                if (pr[0]!=-1 || pr[1]!=1)
                {
                    SafeArrayUnaccessData (ps);
                    return E_INV_OUTPUT_RANGE;
                }
                
                SafeArrayUnaccessData (ps);
            }

        }


    if (numChans!=_nChannels)
        _nChannels = numChans;       
    
    return S_OK;
}
Esempio n. 6
0
/*
 *  call-seq:
 *     WIN32OLE_VARIANT.value = val #=> set WIN32OLE_VARIANT value to val.
 *
 *  Sets variant value to val. If the val type does not match variant value
 *  type(vartype), then val is changed to match variant value type(vartype)
 *  before setting val.
 *  This method is not available when vartype is VT_ARRAY(except VT_UI1|VT_ARRAY).
 *  If the vartype is VT_UI1|VT_ARRAY, the val should be String object.
 *
 *     obj = WIN32OLE_VARIANT.new(1) # obj.vartype is WIN32OLE::VARIANT::VT_I4
 *     obj.value = 3.2 # 3.2 is changed to 3 when setting value.
 *     p obj.value # => 3
 */
static VALUE
folevariant_set_value(VALUE self, VALUE val)
{
    struct olevariantdata *pvar;
    VARTYPE vt;
    TypedData_Get_Struct(self, struct olevariantdata, &olevariant_datatype, pvar);
    vt = V_VT(&(pvar->var));
    if (V_ISARRAY(&(pvar->var)) && ((vt & ~VT_BYREF) != (VT_UI1|VT_ARRAY) || !RB_TYPE_P(val, T_STRING))) {
        rb_raise(eWIN32OLERuntimeError,
                 "`value=' is not available for this variant type object");
    }
    ole_val2olevariantdata(val, vt, pvar);
    return Qnil;
}
Esempio n. 7
0
/* this is a convenience function for fetching a particular
 * element from a (possibly multi-dimensional) safe array */
PHP_COM_DOTNET_API int php_com_safearray_get_elem(VARIANT *array, VARIANT *dest, LONG dim1)
{
    UINT dims;
    LONG lbound, ubound;
    LONG indices[1];
    VARTYPE vt;

    if (!V_ISARRAY(array)) {
        return 0;
    }

    dims = SafeArrayGetDim(V_ARRAY(array));

    if (dims != 1) {
        php_error_docref(NULL, E_WARNING,
                         "Can only handle single dimension variant arrays (this array has %d)", dims);
        return 0;
    }

    if (FAILED(SafeArrayGetVartype(V_ARRAY(array), &vt)) || vt == VT_EMPTY) {
        vt = V_VT(array) & ~VT_ARRAY;
    }

    /* determine the bounds */
    SafeArrayGetLBound(V_ARRAY(array), 1, &lbound);
    SafeArrayGetUBound(V_ARRAY(array), 1, &ubound);

    /* check bounds */
    if (dim1 < lbound || dim1 > ubound) {
        php_com_throw_exception(DISP_E_BADINDEX, "index out of bounds");
        return 0;
    }

    /* now fetch that element */
    VariantInit(dest);

    indices[0] = dim1;

    if (vt == VT_VARIANT) {
        SafeArrayGetElement(V_ARRAY(array), indices, dest);
    } else {
        V_VT(dest) = vt;
        /* store the value into "lVal" member of the variant.
         * This works because it is a union; since we know the variant
         * type, we end up with a working variant */
        SafeArrayGetElement(V_ARRAY(array), indices, &dest->lVal);
    }

    return 1;
}
Esempio n. 8
0
static int saproxy_count_elements(zend_object *object, zend_long *count)
{
	php_com_saproxy *proxy = (php_com_saproxy*) object;
	LONG ubound, lbound;

	if (!V_ISARRAY(&proxy->obj->v)) {
		return FAILURE;
	}

	SafeArrayGetLBound(V_ARRAY(&proxy->obj->v), proxy->dimensions, &lbound);
	SafeArrayGetUBound(V_ARRAY(&proxy->obj->v), proxy->dimensions, &ubound);

	*count = ubound - lbound + 1;

	return SUCCESS;
}
Esempio n. 9
0
static int com_object_count(zval *object, zend_long *count)
{
	php_com_dotnet_object *obj;
	LONG ubound = 0, lbound = 0;

	obj = CDNO_FETCH(object);

	if (!V_ISARRAY(&obj->v)) {
		return FAILURE;
	}

	SafeArrayGetLBound(V_ARRAY(&obj->v), 1, &lbound);
	SafeArrayGetUBound(V_ARRAY(&obj->v), 1, &ubound);

	*count = ubound - lbound + 1;

	return SUCCESS;
}
Esempio n. 10
0
// display variant as a string
void DisplayVariant (VARIANT *pvar)
{
	VARIANT vtemp, *ptemp;
	int vtype;
	char *str;

	if (V_ISARRAY(pvar)) 
	{
		vtype = V_VT(pvar) & ~VT_ARRAY;
		DisplayArray(V_ARRAY(pvar), vtype);
	}
	else 
	{
		// Display result as a string
		if (V_VT(pvar) != VT_BSTR)
		{
			::VariantInit(&vtemp);
			::VariantChangeType(&vtemp, pvar, 0, VT_BSTR);
			ptemp = &vtemp;
		}
		else
		{
			ptemp = pvar;
		}

		if (V_VT(ptemp) == VT_BSTR) 
		{
			str = BstrToCstr(V_BSTR(ptemp));
			printf("DADiSP returned: %s\n", str);
			free(str);

			// Free the string.
			if (ptemp == &vtemp)
			{
				::SysFreeString(V_BSTR(ptemp));
			}
		}
		else 
		{
			printf("Could Not Display Type %d", V_VT(pvar));
		}
	}
}
Esempio n. 11
0
/*
 *  call-seq:
 *     WIN32OLE_VARIANT[i,j,...] = val #=> set the element of OLE array
 *
 *  Set the element of WIN32OLE_VARIANT object(OLE array) to val.
 *  This method is available only when the variant type of
 *  WIN32OLE_VARIANT object is VT_ARRAY.
 *
 *  REMARK:
 *     The all indices should be 0 or natural number and
 *     lower than or equal to max indices.
 *     (This point is different with Ruby Array indices.)
 *
 *     obj = WIN32OLE_VARIANT.new([[1,2,3],[4,5,6]])
 *     obj[0,0] = 7
 *     obj[1,0] = 8
 *     p obj.value # => [[7,2,3], [8,5,6]]
 *     obj[2,0] = 9 # => WIN32OLERuntimeError
 *     obj[0, -1] = 9 # => WIN32OLERuntimeError
 *
 */
static VALUE
folevariant_ary_aset(int argc, VALUE *argv, VALUE self)
{
    struct olevariantdata *pvar;
    SAFEARRAY *psa;
    VARIANT var;
    VARTYPE vt;
    LONG *pid;
    HRESULT hr;
    VOID *p = NULL;

    TypedData_Get_Struct(self, struct olevariantdata, &olevariant_datatype, pvar);
    if (!V_ISARRAY(&(pvar->var))) {
        rb_raise(eWIN32OLERuntimeError,
                 "`[]' is not available for this variant type object");
    }
    psa = get_locked_safe_array(self);
    if (psa == NULL) {
        rb_raise(rb_eRuntimeError, "failed to get SafeArray pointer");
    }

    pid = ary2safe_array_index(argc-1, argv, psa);

    VariantInit(&var);
    vt = (V_VT(&(pvar->var)) & ~VT_ARRAY);
    p = val2variant_ptr(argv[argc-1], &var, vt);
    if ((V_VT(&var) == VT_DISPATCH && V_DISPATCH(&var) == NULL) ||
        (V_VT(&var) == VT_UNKNOWN && V_UNKNOWN(&var) == NULL)) {
        rb_raise(eWIN32OLERuntimeError, "argument does not have IDispatch or IUnknown Interface");
    }
    hr = SafeArrayPutElement(psa, pid, p);
    if (FAILED(hr)) {
        ole_raise(hr, eWIN32OLERuntimeError, "failed to SafeArrayPutElement");
    }

    unlock_safe_array(psa);
    if (pid) free(pid);
    return argv[argc-1];
}
Esempio n. 12
0
zend_object_iterator *php_com_iter_get(zend_class_entry *ce, zval *object, int by_ref)
{
	php_com_dotnet_object *obj;
	struct php_com_iterator *I;
	IEnumVARIANT *iev = NULL;
	DISPPARAMS dp;
	VARIANT v;
	unsigned long n_fetched;
	zval ptr;

	if (by_ref) {
		zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
		return NULL;
	}

	obj = CDNO_FETCH(object);

	if (V_VT(&obj->v) != VT_DISPATCH && !V_ISARRAY(&obj->v)) {
		php_error_docref(NULL, E_WARNING, "variant is not an object or array VT=%d", V_VT(&obj->v));
		return NULL;
	}

	memset(&dp, 0, sizeof(dp));
	VariantInit(&v);

	I = (struct php_com_iterator*)ecalloc(1, sizeof(*I));
	zend_iterator_init(&I->iter);
	I->iter.funcs = &com_iter_funcs;
	Z_PTR(I->iter.data) = I;
	I->code_page = obj->code_page;
	ZVAL_UNDEF(&I->zdata);
	VariantInit(&I->safe_array);
	VariantInit(&I->v);

	if (V_ISARRAY(&obj->v)) {
		LONG bound;
		UINT dims;

		dims = SafeArrayGetDim(V_ARRAY(&obj->v));

		if (dims != 1) {
			php_error_docref(NULL, E_WARNING,
				   "Can only handle single dimension variant arrays (this array has %d)", dims);
			goto fail;
		}

		/* same semantics as foreach on a PHP array;
		 * make a copy and enumerate that copy */
		VariantCopy(&I->safe_array, &obj->v);

		/* determine the key value for the array */
		SafeArrayGetLBound(V_ARRAY(&I->safe_array), 1, &bound);
		SafeArrayGetUBound(V_ARRAY(&I->safe_array), 1, &I->sa_max);

		/* pre-fetch the element */
		if (php_com_safearray_get_elem(&I->safe_array, &I->v, bound)) {
			I->key = bound;
			ZVAL_NULL(&ptr);
			php_com_zval_from_variant(&ptr, &I->v, I->code_page);
			ZVAL_COPY_VALUE(&I->zdata, &ptr);
		} else {
			I->key = (ulong)-1;
		}

	} else {
		/* can we enumerate it? */
		if (FAILED(IDispatch_Invoke(V_DISPATCH(&obj->v), DISPID_NEWENUM,
						&IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD|DISPATCH_PROPERTYGET,
						&dp, &v, NULL, NULL))) {
			goto fail;
		}

		/* get something useful out of it */
		if (V_VT(&v) == VT_UNKNOWN) {
			IUnknown_QueryInterface(V_UNKNOWN(&v), &IID_IEnumVARIANT, (void**)&iev);
		} else if (V_VT(&v) == VT_DISPATCH) {
			IDispatch_QueryInterface(V_DISPATCH(&v), &IID_IEnumVARIANT, (void**)&iev);
		}

		VariantClear(&v);

		if (iev == NULL) {
			goto fail;
		}

		I->ev = iev;

		/* Get the first element now */
		if (SUCCEEDED(IEnumVARIANT_Next(I->ev, 1, &I->v, &n_fetched)) && n_fetched > 0) {
			/* indicate that we have element 0 */
			I->key = 0;
			ZVAL_NULL(&ptr);
			php_com_zval_from_variant(&ptr, &I->v, I->code_page);
			ZVAL_COPY_VALUE(&I->zdata, &ptr);
		} else {
			/* indicate that there are no more items */
			I->key = (ulong)-1;
		}
	}

	return &I->iter;

fail:
	if (I) {
		VariantClear(&I->safe_array);
		VariantClear(&I->v);
		efree(I);
	}
	return NULL;
}
Esempio n. 13
0
SEXP
R_convertDCOMObjectToR(VARIANT *var)
{
  SEXP ans = R_NilValue;

  VARTYPE type = V_VT(var);

#if defined(RDCOM_VERBOSE) && RDCOM_VERBOSE
  errorLog("Converting VARIANT to R %d\n", V_VT(var));
#endif


  if(V_ISARRAY(var)) {
#if defined(RDCOM_VERBOSE) && RDCOM_VERBOSE
  errorLog("Finishing convertDCOMObjectToR - convert array\n");
#endif
    return(convertArrayToR(var));
  } else if(V_VT(var) == VT_DISPATCH || (V_ISBYREF(var) && ((V_VT(var) & (~ VT_BYREF)) == VT_DISPATCH)) ) {
    IDispatch *ptr;
    if(V_ISBYREF(var)) {

#if defined(RDCOM_VERBOSE) && RDCOM_VERBOSE
      errorLog("BYREF and DISPATCH in convertDCOMObjectToR\n");
#endif

      IDispatch **tmp = V_DISPATCHREF(var);
      if(!tmp)
	return(ans);
      ptr = *tmp;
    } else
      ptr = V_DISPATCH(var);
       //xxx
    if(ptr) 
      ptr->AddRef();
    ans = R_createRCOMUnknownObject((void*) ptr, "COMIDispatch");
#if defined(RDCOM_VERBOSE) && RDCOM_VERBOSE
    errorLog("Finished convertDCOMObjectToR  COMIDispatch\n");
#endif
    return(ans);
  }



  if(V_ISBYREF(var)) {
    VARTYPE rtype = type & (~ VT_BYREF);

#if defined(RDCOM_VERBOSE) && RDCOM_VERBOSE
    errorLog("ISBYREF() in convertDCOMObjectToR: ref type %d\n", rtype);
#endif

    if(rtype == VT_BSTR) {
        BSTR *tmp;
        const char *ptr = "";
#if defined(RDCOM_VERBOSE) && RDCOM_VERBOSE
	errorLog("BYREF and BSTR convertDCOMObjectToR  (scalar string)\n");
#endif
        tmp = V_BSTRREF(var);
        if(tmp)
  	  ptr = FromBstr(*tmp);
        ans = R_scalarString(ptr);
	return(ans);
    } else if(rtype == VT_BOOL || rtype == VT_I4 || rtype == VT_R8){
      return(createVariantRef(var, rtype));
    } else {
        fprintf(stderr, "Unhandled by-reference conversion type %d\n", V_VT(var));fflush(stderr);
	return(R_NilValue);        
    }
  }

  switch(type) {

    case VT_BOOL:
      ans = R_scalarLogical( (Rboolean) (V_BOOL(var) ? TRUE : FALSE));
      break;

    case VT_UI1:
    case VT_UI2:
    case VT_UI4:
    case VT_UINT:
      VariantChangeType(var, var, 0, VT_I4);
      ans = R_scalarReal((double) V_I4(var));
      break;

    case VT_I1:
    case VT_I2:
    case VT_I4:
    case VT_INT:
      VariantChangeType(var, var, 0, VT_I4);
      ans = R_scalarInteger(V_I4(var));
      break;

    case VT_R4:
    case VT_R8:
    case VT_I8:
      VariantChangeType(var, var, 0, VT_R8);
      ans = R_scalarReal(V_R8(var));
      break;

    case VT_CY:
    case VT_DATE:
    case VT_HRESULT:
    case VT_DECIMAL:
      VariantChangeType(var, var, 0, VT_R8);
      ans = numberFromVariant(var, type);
      break;

    case VT_BSTR:
      {
	char *ptr = FromBstr(V_BSTR(var));
        ans = R_scalarString(ptr);
      }
      break;

    case VT_UNKNOWN:
      {
       IUnknown *ptr = V_UNKNOWN(var);
       //xxx
       if(ptr)
         ptr->AddRef();
       ans = R_createRCOMUnknownObject((void**) ptr, "COMUnknown");
      }
       break;
  case VT_EMPTY:
  case VT_NULL:
  case VT_VOID:
    return(R_NilValue);
    break;


/*XXX Need to fill these in */
  case VT_RECORD:
  case VT_FILETIME:
  case VT_BLOB:
  case VT_STREAM:
  case VT_STORAGE:
  case VT_STREAMED_OBJECT:
    /*  case LPSTR: */
  case VT_LPWSTR:
  case VT_PTR:
  case VT_ERROR:
  case VT_VARIANT:
  case VT_CARRAY:
  case VT_USERDEFINED:
  default:
    fprintf(stderr, "Unhandled conversion type %d\n", V_VT(var));fflush(stderr);
    //XXX this consumes the variant. So the variant clearance in Invoke() does it again!
    ans = createRVariantObject(var, V_VT(var));
  }

#if defined(RDCOM_VERBOSE) && RDCOM_VERBOSE
  errorLog("Finished convertDCOMObjectToR\n");
#endif

  return(ans);
}
Esempio n. 14
0
TclObject::TclObject (VARIANT *pSrc, const Type &type, Tcl_Interp *interp, int bytes)
{
    if (V_ISARRAY(pSrc)) {
        SAFEARRAY *psa = V_ISBYREF(pSrc) ? *V_ARRAYREF(pSrc) : V_ARRAY(pSrc);
        VARTYPE elementType = V_VT(pSrc) & VT_TYPEMASK;
        unsigned numDimensions = SafeArrayGetDim(psa);
        std::vector<long> indices(numDimensions);

        m_pObj = convertFromSafeArray( psa, elementType, 1, &indices[0], type, interp, bytes);

    } else if (vtMissing == pSrc) {
        m_pObj = Extension::newNaObj();

    } else {
        switch (V_VT(pSrc)) {
        case VT_BOOL:
            m_pObj = Tcl_NewBooleanObj(V_BOOL(pSrc));
            break;

        case VT_ERROR:
            m_pObj = Tcl_NewLongObj(V_ERROR(pSrc));
            break;

        case VT_I1:
        case VT_UI1:
            m_pObj = Tcl_NewLongObj(V_I1(pSrc));
            break;

        case VT_I2:
        case VT_UI2:
            m_pObj = Tcl_NewLongObj(V_I2(pSrc));
            break;

        case VT_I4:
        case VT_UI4:
        case VT_INT:
        case VT_UINT:
            m_pObj = Tcl_NewLongObj(V_I4(pSrc));
            break;

#ifdef V_I8
        case VT_I8:
        case VT_UI8:
            m_pObj = Tcl_NewWideIntObj(V_I8(pSrc));
            break;
#endif

        case VT_R4:
            m_pObj = Tcl_NewDoubleObj(V_R4(pSrc));
            break;

        case VT_DATE:
        case VT_R8:
            m_pObj = Tcl_NewDoubleObj(V_R8(pSrc));
            break;

        case VT_DISPATCH:
            m_pObj = convertFromUnknown(V_DISPATCH(pSrc), type.iid(), interp);
            break;

        case VT_DISPATCH | VT_BYREF:
            m_pObj = convertFromUnknown(
                (V_DISPATCHREF(pSrc) != 0) ? *V_DISPATCHREF(pSrc) : 0,
                type.iid(),
                interp);
            break;

        case VT_UNKNOWN:
            m_pObj = convertFromUnknown(V_UNKNOWN(pSrc), type.iid(), interp);
            break;

        case VT_UNKNOWN | VT_BYREF:
            m_pObj = convertFromUnknown(
                (V_UNKNOWNREF(pSrc) != 0) ? *V_UNKNOWNREF(pSrc) : 0,
                type.iid(),
                interp);
            break;

        case VT_NULL:
            m_pObj = Extension::newNullObj();
            break;

        case VT_LPSTR:
            m_pObj = Tcl_NewStringObj(V_I1REF(pSrc), -1);
            break;

        case VT_LPWSTR:
            {
#if TCL_MINOR_VERSION >= 2
                // Uses Unicode function introduced in Tcl 8.2.
                m_pObj = newUnicodeObj(V_UI2REF(pSrc), -1);
#else
		const wchar_t *pWide = V_UI2REF(pSrc);
                _bstr_t str(pWide);
                m_pObj = Tcl_NewStringObj(str, -1);
#endif
            }
            break;

        default:
            if (V_VT(pSrc) == VT_USERDEFINED && type.name() == "GUID") {
                Uuid uuid(*static_cast<UUID *>(V_BYREF(pSrc)));
                m_pObj = Tcl_NewStringObj(
                    const_cast<char *>(uuid.toString().c_str()), -1);
            } else {
                if (V_VT(pSrc) == (VT_VARIANT | VT_BYREF)) {
                    pSrc = V_VARIANTREF(pSrc);
                }

                _bstr_t str(pSrc);
#if TCL_MINOR_VERSION >= 2
                // Uses Unicode function introduced in Tcl 8.2.
		wchar_t *pWide = str;
                m_pObj = newUnicodeObj(
                    reinterpret_cast<Tcl_UniChar *>(pWide), str.length());
#else
                m_pObj = Tcl_NewStringObj(str, -1);
#endif
            }
        }
    }

    Tcl_IncrRefCount(m_pObj);
}
Esempio n. 15
0
void ReleaseVariant(VARIANTARG *pvarg)
{
    VARTYPE vt;
    VARIANTARG HUGEP *pvargArray;
    long lLBound, lUBound, l;

    vt = pvarg->vt & 0xfff;        // mask off flags

    // check if an array.  If so, free its contents, 
    // then the array itself.
    if (V_ISARRAY(pvarg)) 
    {
        // variant arrays are all this routine currently knows about.
        // Since a variant can contain anything (even other arrays), 
        // call ourselves recursively.
        if (vt == VT_VARIANT) 
        {
            SafeArrayGetLBound(pvarg->parray, 1, &lLBound);
            SafeArrayGetUBound(pvarg->parray, 1, &lUBound);

            if (lUBound > lLBound) 
            {
                lUBound -= lLBound;

                SafeArrayAccessData(pvarg->parray,(void **) &pvargArray);




                for (l = 0; l < lUBound; l++) 
                {
                    ReleaseVariant(pvargArray);
                    pvargArray++;
                }

                SafeArrayUnaccessData(pvarg->parray);
            }
        }
        else 
        {
            MessageBox(g_hwndApp, "ReleaseVariant: Array contains non-variant type", g_szAppTitle, MB_OK | MB_ICONSTOP);
        }

        // Free the array itself.
        SafeArrayDestroy(pvarg->parray);
    }
    else 
    {
        switch (vt) 
        {
            case VT_DISPATCH:
				//14/12/2004
				//!!!all comments had to be added
                (/***/(pvarg->pdispVal/*->lpVtbl*/->Release))(/*pvarg->pdispVal*/);
                break;

            case VT_BSTR:
                SysFreeString(pvarg->bstrVal);
                break;

            case VT_I2:
            case VT_BOOL:
            case VT_R8:
            case VT_ERROR:        // to avoid erroring on an error return 
                                // from Excel
                // no work for these types
                break;

            default:
                MessageBox(g_hwndApp, "ReleaseVariant: Unknown type",g_szAppTitle, MB_OK | MB_ICONSTOP);
                break;
        }
    }

    ClearVariant(pvarg);
}
Esempio n. 16
0
static void saproxy_write_dimension(zend_object *object, zval *offset, zval *value)
{
	php_com_saproxy *proxy = (php_com_saproxy*) object;
	UINT dims, i;
	HRESULT res;
	VARIANT v;

	if (V_VT(&proxy->obj->v) == VT_DISPATCH) {
		/* We do a prop-set using the first dimension as the property name,
		 * all subsequent dimensions and offset as parameters, with value as
		 * the final value */
		zval *args = safe_emalloc(proxy->dimensions + 2, sizeof(zval), 0);

		for (i = 1; i < (UINT) proxy->dimensions; i++) {
			ZVAL_COPY_VALUE(&args[i-1], &proxy->indices[i]);
		}
		ZVAL_COPY_VALUE(&args[i-1], offset);
		ZVAL_COPY_VALUE(&args[i], value);

		convert_to_string(&proxy->indices[0]);
		VariantInit(&v);
		if (SUCCESS == php_com_do_invoke(proxy->obj, Z_STRVAL(proxy->indices[0]),
					Z_STRLEN(proxy->indices[0]), DISPATCH_PROPERTYPUT, &v, proxy->dimensions + 1,
					args, 0)) {
			VariantClear(&v);
		}

		efree(args);

	} else if (V_ISARRAY(&proxy->obj->v)) {
		LONG *indices;
		VARTYPE vt;

		dims = SafeArrayGetDim(V_ARRAY(&proxy->obj->v));
		indices = safe_emalloc(dims, sizeof(LONG), 0);
		/* copy indices from proxy */
		for (i = 0; i < dims; i++) {
			convert_to_long(&proxy->indices[i]);
			indices[i] = (LONG)Z_LVAL(proxy->indices[i]);
		}

		/* add user-supplied index */
		convert_to_long(offset);
		indices[dims-1] = (LONG)Z_LVAL_P(offset);

		if (FAILED(SafeArrayGetVartype(V_ARRAY(&proxy->obj->v), &vt)) || vt == VT_EMPTY) {
			vt = V_VT(&proxy->obj->v) & ~VT_ARRAY;
		}

		VariantInit(&v);
		php_com_variant_from_zval(&v, value, proxy->obj->code_page);

		if (V_VT(&v) != vt) {
			VariantChangeType(&v, &v, 0, vt);
		}

		if (vt == VT_VARIANT) {
			res = SafeArrayPutElement(V_ARRAY(&proxy->obj->v), indices, &v);
		} else {
			res = SafeArrayPutElement(V_ARRAY(&proxy->obj->v), indices, &v.lVal);
		}

		efree(indices);
		VariantClear(&v);

		if (FAILED(res)) {
			php_com_throw_exception(res, NULL);
		}
	} else {
		php_com_throw_exception(E_NOTIMPL, "invalid write to com proxy object");
	}
}
HRESULT CTCPropBagOnRegKey::_WriteVariant(CRegKey& key,
  const _bstr_t& strValueName, VARIANT* pVar)
{
  // Check for an array
  if (V_ISARRAY(pVar))
    return WriteSafeArray(key, strValueName, pVar);

  // Write the value to the registry based on the VARIANT type
  switch (V_VT(pVar))
  {
    // Empty variant is written as nothing
    case VT_EMPTY:
      key.DeleteValue(strValueName);
      key.RecurseDeleteKey(strValueName);
      break;

    // Integer types are written as a REG_DWORD value
    case VT_I1:
    case VT_I2:
    case VT_I4:
    case VT_UI1:
    case VT_UI2:
    case VT_ERROR:
    {
      // Coerce the value to a VT_UI4
      VariantChangeTypeEx(pVar, pVar, GetThreadLocale(), m_wChangeTypeFlags,
        VT_UI4);
      // Fall thru to next case
    }
    case VT_UI4:
    {
      // Write the REG_DWORD value to the registry
      key.RecurseDeleteKey(strValueName);
      key.WriteDWord(strValueName, V_UI4(pVar));
      break;
    }

    // BOOL's, float types and strings are written as a REG_SZ value
    case VT_R4:
    case VT_R8:
    case VT_CY:
    case VT_DATE:
    case VT_DECIMAL:
    case VT_BOOL:
    {
      // Coerce the value to a VT_BSTR
      VariantChangeTypeEx(pVar, pVar, GetThreadLocale(), m_wChangeTypeFlags,
        VT_BSTR);
      // Fall thru to next case
    }
    case VT_BSTR:
    {
      // Write the REG_SZ value to the registry
      key.RecurseDeleteKey(strValueName);
      key.WriteString(strValueName, V_BSTR(pVar));
      break;
    }

    // Objects written as REG_BINARY, if they don't support IPersistPropertyBag
    case VT_UNKNOWN:
    case VT_DISPATCH:
    {
      // Attempt first to save the object property using IPersistPropertyBag
      key.DeleteValue(strValueName);
      key.RecurseDeleteKey(strValueName);

      CComObjectStack<CTCPropBagOnRegKey> bag;
      bag.Init(key, _bstr_t(), this);
      HRESULT hr = bag.SaveObject(strValueName, V_UNKNOWN(pVar), FALSE, TRUE);
      if (FAILED(hr))
      {
        TRACE1("CTCPropBagOnRegKey::_WriteVariant: Saving object property \"%s\" as a binary value\n",
          strValueName);

        // Create a CArchive on a CMemFile
        CMemFile file;
        CArchive ar(&file, CArchive::store, 0);

        // Archive the variant to the CArchive and close it
        CComVariant v(pVar);
        ar << v;
        ar.Close();

        // Write the REG_BINARY value to the registry
        int cbData = file.GetLength();
        BYTE* pData = file.Detach();
        key.RecurseDeleteKey(strValueName);
        key.WriteBinary(strValueName, pData, cbData);
        file.Attach(pData, cbData);
      }
      break;
    }

    default:
      TRACE1("CTCPropBagOnRegKey::_WriteVariant(\"%ls\"): ", strValueName);
      TRACE2("Unsupported variant type 0x%02X (%d)\n", UINT(V_VT(pVar)),
        UINT(V_VT(pVar)));
      return E_FAIL;
  }

  // Indicate success
  return S_OK;
}
Esempio n. 18
0
PyObject *PyRecord::getattro(PyObject *self, PyObject *obname)
{
	PyObject *res;
	PyRecord *pyrec = (PyRecord *)self;
	char *name=PYWIN_ATTR_CONVERT(obname);
	if (name==NULL)
		return NULL;
	if (strcmp(name, "__members__")==0) {
		ULONG cnames = 0;
		HRESULT hr = pyrec->pri->GetFieldNames(&cnames, NULL);
		if (FAILED(hr))
			return PyCom_BuildPyException(hr, pyrec->pri, IID_IRecordInfo);
		BSTR *strs = (BSTR *)malloc(sizeof(BSTR) * cnames);
		if (strs==NULL)
			return PyErr_NoMemory();
		hr = pyrec->pri->GetFieldNames(&cnames, strs);
		if (FAILED(hr)) {
			free(strs);
			return PyCom_BuildPyException(hr, pyrec->pri, IID_IRecordInfo);
		}
		res = PyList_New(cnames);
		for (ULONG i=0;i<cnames && res != NULL;i++) {
			PyObject *item = PyWinCoreString_FromString(strs[i]);
			SysFreeString(strs[i]);
			if (item==NULL) {
				Py_DECREF(res);
				res = NULL;
			} else
				PyList_SET_ITEM(res, i, item); // ref count swallowed.
		}
		free(strs);
		return res;
	}

	res = PyObject_GenericGetAttr(self, obname);
	if (res != NULL)
		return res;

	PyErr_Clear();
	WCHAR *wname;
	if (!PyWinObject_AsWCHAR(obname, &wname))
		return NULL;

	VARIANT vret;
	VariantInit(&vret);
	void *sub_data = NULL;

	PY_INTERFACE_PRECALL;
	HRESULT hr = pyrec->pri->GetFieldNoCopy(pyrec->pdata, wname, &vret, &sub_data);
	PyWinObject_FreeWCHAR(wname);
	PY_INTERFACE_POSTCALL;

	if (FAILED(hr)) {
		if (hr == TYPE_E_FIELDNOTFOUND){
			// This is slightly suspect - throwing a unicode
			// object for an AttributeError in py2k - but this
			// is the value we asked COM for, so it makes sense...
			// (and PyErr_Format doesn't handle unicode in py2x)
			PyErr_SetObject(PyExc_AttributeError, obname);
			return NULL;
			}
		return PyCom_BuildPyException(hr, pyrec->pri, IID_IRecordInfo);
	}

	// Short-circuit sub-structs and arrays here, so we dont allocate a new chunk
	// of memory and copy it - we need sub-structs to persist.
	if (V_VT(&vret)==(VT_BYREF | VT_RECORD))
		return new PyRecord(V_RECORDINFO(&vret), V_RECORD(&vret), pyrec->owner);
	else if (V_VT(&vret)==(VT_BYREF | VT_ARRAY | VT_RECORD)) {
		SAFEARRAY *psa = *V_ARRAYREF(&vret);
		int d = SafeArrayGetDim(psa);
		if (sub_data==NULL)
			return PyErr_Format(PyExc_RuntimeError, "Did not get a buffer for the array!");
		if (SafeArrayGetDim(psa) != 1)
			return PyErr_Format(PyExc_TypeError, "Only support single dimensional arrays of records");
		IRecordInfo *sub = NULL;
		long ubound, lbound, nelems;
		int i;
		BYTE *this_data;
		PyObject *ret_tuple = NULL;
		ULONG element_size = 0;
		hr = SafeArrayGetUBound(psa, 1, &ubound);
		if (FAILED(hr)) goto array_end;
		hr = SafeArrayGetLBound(psa, 1, &lbound);
		if (FAILED(hr)) goto array_end;
		hr = SafeArrayGetRecordInfo(psa, &sub);
		if (FAILED(hr)) goto array_end;
		hr = sub->GetSize(&element_size);
		if (FAILED(hr)) goto array_end;
		nelems = ubound-lbound;
		ret_tuple = PyTuple_New(nelems);
		if (ret_tuple==NULL) goto array_end;
		this_data = (BYTE *)sub_data;
		for (i=0;i<nelems;i++) {
			PyTuple_SET_ITEM(ret_tuple, i, new PyRecord(sub, this_data, pyrec->owner));
			this_data += element_size;
		}
array_end:
		if (sub)
			sub->Release();
		if (FAILED(hr)) 
			return PyCom_BuildPyException(hr, pyrec->pri, IID_IRecordInfo);
		return ret_tuple;
	}

	// This default conversion we use is a little slow (but it will do!)
	// For arrays, the pparray->pvData member is *not* set, since the actual data
	// pointer from the record is returned in sub_data, so set it here.
	if (V_ISARRAY(&vret) && V_ISBYREF(&vret))
		(*V_ARRAYREF(&vret))->pvData = sub_data;
	PyObject *ret = PyCom_PyObjectFromVariant(&vret);

//	VariantClear(&vret);
	return ret;
}
LPCTSTR COcsWmi::strVariantArray( VARIANT var)

{

    LONG dwSLBound = 0;

    LONG dwSUBound = 0;

    VARIANT v;
	VariantInit(&v);

    LONG i;

	static CString strRet;

	HRESULT hr;



    if(!V_ISARRAY(&var))

        return strVariant( var);



    //

    // Check that there is only one dimension in this array

    //



    if ((V_ARRAY(&var))->cDims != 1)

		return NULL;



	//

    // Check that there is atleast one element in this array

    //



    if ((V_ARRAY(&var))->rgsabound[0].cElements == 0)

		return NULL;



    //

    // We know that this is a valid single dimension array

    //



    hr = SafeArrayGetLBound( V_ARRAY(&var), 1, (long FAR *)&dwSLBound);

	if (FAILED( hr))

		return NULL;



    hr = SafeArrayGetUBound( V_ARRAY(&var), 1, (long FAR *)&dwSUBound);

    if (FAILED( hr))

		return NULL;



	strRet = _T( "");

    // VariantInit(&v);

	DECIMAL_SETZERO(v.decVal);

    for (i = dwSLBound; i <= dwSUBound; i++)

	{

        hr = SafeArrayGetElement( V_ARRAY(&var), (long FAR *)&i, &v);

        if (FAILED(hr))

            continue;



        if (i < dwSUBound)

		{

			strRet += strVariant( v);

			strRet += _T( ";");

		}

        else

		{

			strRet += strVariant( v);

        }

        // VariantClear(&v);

		DECIMAL_SETZERO(v.decVal);

    }

    return(strRet);

}
LPCTSTR COcsWmi::strCimArrayValue(VARIANT &pVal, CIMTYPE &pType)

{

    LONG dwSLBound = 0;

    LONG dwSUBound = 0;

    VARIANT v;
	VariantInit(&v);

    LONG i;

	static CString strRet;

	HRESULT hr;

	CIMTYPE cimTypeWithoutArray;



    if(!V_ISARRAY(&pVal))

        return strCimValue( pVal, pType);



    //

    // Check that there is only one dimension in this array

    //



    if ((V_ARRAY(&pVal))->cDims != 1)

		return NULL;



	//

    // Check that there is atleast one element in this array

    //



    if ((V_ARRAY(&pVal))->rgsabound[0].cElements == 0)

		return NULL;



    //

    // We know that this is a valid single dimension array

    //



    hr = SafeArrayGetLBound( V_ARRAY(&pVal), 1, (long FAR *)&dwSLBound);

	if (FAILED( hr))

		return NULL;



    hr = SafeArrayGetUBound( V_ARRAY(&pVal), 1, (long FAR *)&dwSUBound);

    if (FAILED( hr))

		return NULL;



	// Calculate CIM type without the array flag

	cimTypeWithoutArray = pType ^ CIM_FLAG_ARRAY;



	// Parse the array

	strRet = _T( "");

    //VariantInit(&v);

/*    for (i = dwSLBound; i <= dwSUBound; i++)

	{

        hr = SafeArrayGetElement( V_ARRAY(&pVal), (long FAR *)&i, &v);

        if (FAILED(hr))

            continue;



        if (i < dwSUBound)

		{

			strRet += strCimValue( v, cimTypeWithoutArray);

			strRet += _T( ";");

		}

        else

		{

			strRet += strCimValue( v, cimTypeWithoutArray);

        }

        VariantClear(&v);

    }

*/

	v.vt=(unsigned short)cimTypeWithoutArray;

	DECIMAL_SETZERO(v.decVal);

	for (i = dwSLBound; i <= dwSUBound; i++)

	{

		  hr = SafeArrayGetElement( V_ARRAY(&pVal), (long FAR *)&i, &v.lVal);

		  if (FAILED(hr))

				continue;

		  strRet += strCimValue( v, cimTypeWithoutArray);

		  if (i < dwSUBound)

		  {

				strRet += _T( ";");

		  }

		  DECIMAL_SETZERO(v.decVal);

	}

    return(strRet);

}
Esempio n. 21
0
static zval *saproxy_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
{
	php_com_saproxy *proxy = (php_com_saproxy*) object;
	UINT dims, i;
	SAFEARRAY *sa;
	LONG ubound, lbound;
	HRESULT res;

	ZVAL_NULL(rv);

	if (V_VT(&proxy->obj->v) == VT_DISPATCH) {
		VARIANT v;
		zval *args;

		/* prop-get using first dimension as the property name,
		 * all subsequent dimensions and the offset as parameters */

		args = safe_emalloc(proxy->dimensions + 1, sizeof(zval), 0);

		for (i = 1; i < (UINT) proxy->dimensions; i++) {
			args[i-1] = proxy->indices[i];
		}
		ZVAL_COPY_VALUE(&args[i-1], offset);

		convert_to_string(&proxy->indices[0]);
		VariantInit(&v);

		res = php_com_do_invoke(proxy->obj, Z_STRVAL(proxy->indices[0]),
			   	Z_STRLEN(proxy->indices[0]), DISPATCH_METHOD|DISPATCH_PROPERTYGET, &v,
			   	proxy->dimensions, args, 0);

		if (res == SUCCESS) {
			php_com_zval_from_variant(rv, &v, proxy->obj->code_page);
			VariantClear(&v);
		} else if (res == DISP_E_BADPARAMCOUNT) {
			/* return another proxy */
			php_com_saproxy_create(object, rv, offset);
		}

		return rv;

	} else if (!V_ISARRAY(&proxy->obj->v)) {
		php_com_throw_exception(E_INVALIDARG, "invalid read from com proxy object");
		return rv;
	}

	/* the SafeArray case */

	/* offset/index must be an integer */
	convert_to_long(offset);

	sa = V_ARRAY(&proxy->obj->v);
	dims = SafeArrayGetDim(sa);

	if ((UINT) proxy->dimensions >= dims) {
		/* too many dimensions */
		php_com_throw_exception(E_INVALIDARG, "too many dimensions!");
		return rv;
	}

	/* bounds check */
	SafeArrayGetLBound(sa, proxy->dimensions, &lbound);
	SafeArrayGetUBound(sa, proxy->dimensions, &ubound);

	if (Z_LVAL_P(offset) < lbound || Z_LVAL_P(offset) > ubound) {
		php_com_throw_exception(DISP_E_BADINDEX, "index out of bounds");
		return rv;
	}

	if (dims - 1 == proxy->dimensions) {
		LONG *indices;
		VARTYPE vt;
		VARIANT v;

		VariantInit(&v);

		/* we can return a real value */
		indices = safe_emalloc(dims, sizeof(LONG), 0);

		/* copy indices from proxy */
		for (i = 0; i < dims; i++) {
			convert_to_long(&proxy->indices[i]);
			indices[i] = (LONG)Z_LVAL(proxy->indices[i]);
		}

		/* add user-supplied index */
		indices[dims-1] = (LONG)Z_LVAL_P(offset);

		/* now fetch the value */
		if (FAILED(SafeArrayGetVartype(sa, &vt)) || vt == VT_EMPTY) {
			vt = V_VT(&proxy->obj->v) & ~VT_ARRAY;
		}

		if (vt == VT_VARIANT) {
			res = SafeArrayGetElement(sa, indices, &v);
		} else {
			V_VT(&v) = vt;
			res = SafeArrayGetElement(sa, indices, &v.lVal);
		}

		efree(indices);

		if (SUCCEEDED(res)) {
			php_com_wrap_variant(rv, &v, proxy->obj->code_page);
		} else {
			php_com_throw_exception(res, NULL);
		}

		VariantClear(&v);

	} else {
		/* return another proxy */
		php_com_saproxy_create(object, rv, offset);
	}

	return rv;
}
Esempio n. 22
0
/*
 *  ReleaseVariant
 *
 *  Clears a particular variant structure and releases any external objects
 *  or memory contained in the variant.  Supports the data types listed above.
 */
int CXLAutomation::ReleaseVariant(VARIANTARG *pvarg)
{
	VARTYPE vt;
	VARIANTARG *pvargArray;
	long lLBound, lUBound, l;
	
	vt = pvarg->vt & 0xfff;		// mask off flags
	
	// check if an array.  If so, free its contents, then the array itself.
	if (V_ISARRAY(pvarg)) 
	{
		// variant arrays are all this routine currently knows about.  Since a
		// variant can contain anything (even other arrays), call ourselves
		// recursively.
		if (vt == VT_VARIANT) 
		{
			SafeArrayGetLBound(pvarg->parray, 1, &lLBound);
			SafeArrayGetUBound(pvarg->parray, 1, &lUBound);
			
			if (lUBound > lLBound) 
			{
				lUBound -= lLBound;
				
				SafeArrayAccessData(pvarg->parray, (void**)&pvargArray);
				
				for (l = 0; l < lUBound; l++) 
				{
					ReleaseVariant(pvargArray);
					pvargArray++;
				}
				
				SafeArrayUnaccessData(pvarg->parray);
			}
		}
		else 
		{
			return 1; //  non-variant type

			// MessageBox(NULL, _T("ReleaseVariant: Array contains non-variant type"), "Failed", MB_OK | MB_ICONSTOP);
		}
		
		// Free the array itself.
		SafeArrayDestroy(pvarg->parray);
	}
	else 
	{
		switch (vt) 
		{
			case VT_DISPATCH:
				//(*(pvarg->pdispVal->lpVtbl->Release))(pvarg->pdispVal);
				pvarg->pdispVal->Release();
				break;
				
			case VT_BSTR:
				SysFreeString(pvarg->bstrVal);
				break;
				
			case VT_I2:
			case VT_BOOL:
			case VT_R8:
			case VT_ERROR:		// to avoid erroring on an error return from Excel
				// no work for these types
				break;
				
			default:

				return 2; //unknonw type
				// MessageBox(NULL, _T("ReleaseVariant: Unknown type"), "Failed", MB_OK | MB_ICONSTOP);
				break;
		}
	}
	
	ClearVariant(pvarg);
	return 0;

}
HRESULT CTCPropBagOnRegKey::_WriteSafeArray(CRegKey& key,
  const _bstr_t& strPropName, VARIANT* pVar)
{
  ASSERT(V_ISARRAY(pVar));
  ASSERT(lstrlen(strPropName));

  // Get the SAFEARRAY pointer from the variant
  SAFEARRAY* psa = V_ARRAY(pVar);
  if (IsBadReadPtr(psa))
    return E_POINTER;

  // Only support 1-dimensional arrays (currently)
  if (1 != SafeArrayGetDim(psa))
    return E_INVALIDARG;

  // Get the element size of the safe array
  UINT cbElement = SafeArrayGetElemsize(psa);

  // Get the safe array type from the variant
  VARTYPE vt = V_VT(pVar) & ~VT_ARRAY;

  // Check for supported types and validate the element size
  switch (vt)
  {
    case VT_BOOL:
      if (sizeof(V_BOOL(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    case VT_I1:
      if (sizeof(V_I1(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    case VT_I2:
      if (sizeof(V_I2(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    case VT_I4:
      if (sizeof(V_I4(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    case VT_UI1:
      if (sizeof(V_UI1(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    case VT_UI2:
      if (sizeof(V_UI2(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    case VT_UI4:
      if (sizeof(V_UI4(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    case VT_ERROR:
      if (sizeof(V_ERROR(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    case VT_R4:
      if (sizeof(V_R4(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    case VT_R8:
      if (sizeof(V_R8(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    case VT_DECIMAL:
      if (sizeof(V_DECIMAL(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    case VT_CY:
      if (sizeof(V_CY(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    case VT_DATE:
      if (sizeof(V_DATE(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    case VT_BSTR:
      if (sizeof(V_BSTR(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    case VT_UNKNOWN:
      if (sizeof(V_UNKNOWN(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    case VT_DISPATCH:
      if (sizeof(V_DISPATCH(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    case VT_VARIANT:
      if (sizeof(V_VARIANTREF(pVar)) != cbElement)
        return E_UNEXPECTED;
      break;
    default:
      return E_UNEXPECTED;
  }

  // Get the upper and lower bounds of the safe array
  HRESULT hr;
  LONG lUBound = 0, lLBound = 0;
  if (FAILED(hr = SafeArrayGetUBound(psa, 1, &lUBound)))
    return hr;
  if (FAILED(hr = SafeArrayGetLBound(psa, 1, &lLBound)))
    return hr;
  UINT nElements = lUBound - lLBound + 1;

  // Create a subkey with the specified name
  key.DeleteValue(strPropName);
  key.RecurseDeleteKey(strPropName);
  CRegKey subkey;
  if (!subkey.Open(key, strPropName))
    return HRESULT_FROM_WIN32(GetLastError());

  // Get access to the safe array data
  BYTE* pElement = NULL;
  if (FAILED(hr = SafeArrayAccessData(psa, (void**)&pElement)))
    return hr;

  // Write the variant type value
  subkey.WriteDWord(m_szVariantType, DWORD(V_VT(pVar)));

  // Write the element count value
  subkey.WriteDWord(m_szElementCount, DWORD(nElements));

  // Write the lower bound value, if not 0
  if (lLBound)
    subkey.WriteDWord(m_szLowerBound, DWORD(lLBound));

  // Special handling for arrays of variants 
  _bstr_t strText;
  if (VT_VARIANT == vt)
  {
    // Write each variant array element to the registry
    for (UINT i = 0; i < nElements; i++, pElement += cbElement)
    {
      // Format the value name
      strText.Format(m_szElementFmt, i);

      // Write the variant array element to the registry subkey
      if (FAILED(hr = WriteVariant(subkey, strText, (VARIANT*)pElement)))
      {
        TRACE1("CTCPropBagOnRegKey::_WriteSafeArray(\"%s\", pVar): ",
          strPropName);
        TRACE2("WriteVariant(subkey, \"%s\", &var) returned 0x%08X\n",
          strText, hr);
      }
    }
  }
  else
  {
    // Write each array element to the registry
    VARIANT var;
    V_VT(&var) = vt;
    for (UINT i = 0; i < nElements; i++, pElement += cbElement)
    {
      // Copy the array element to the data portion of the VARIANT
      memcpy(&V_NONE(&var), pElement, cbElement);

      // Format the value name
      strText.Format(m_szElementFmt, i);

      // Write the variant to the registry subkey
      if (FAILED(hr = WriteVariant(subkey, strText, &var)))
      {
        TRACE1("CTCPropBagOnRegKey::_WriteSafeArray(\"%s\", pVar): ",
          strPropName);
        TRACE2("WriteVariant(subkey, \"%s\", &var) returned 0x%08X\n",
          strText, hr);
      }
    }
  }

  // Release access to the safe array data
  VERIFY(SUCCEEDED(SafeArrayUnaccessData(psa)));

  // Indicate success
  return S_OK;
}