contents   index   previous   next



VariantCopy

 

f90VB Modules

 

f90VBDefs, f90VBVariants

Summary

 

Frees the destination variant and makes a copy of the source variant.

Syntax

 

subroutine VariantCopy (VarSrc, VarDest, iRet)

type(VARIANT),intent(in)::  VarSrc
type(VARIANT),intent(inout)::  VarDest
integer(HRESULT_KIND),intent(out),optional:: iRet

Arguments

 

VarSrc [Input]

Variant to be copied (source variant).

VarDest [Input/Output]

Variant that will contain the copy of VarSrc (destination variant). VarDest must be an initialized variant.

iRet [Output/Optional]

Upon return, iRet contains S_OK or an error code. See comments for more information.

Comments

 

VariantCopy first calls VariantClear on the destination variant (VarDest) to clear any contents the destination variant may have. It then proceeds to write a copy of the source variant into the destination variant.

 

If VarSrc contains a BString or a Safe Array, VariantCopy makes a copy of the BString or the Safe Array, allocating new resources as necessary. If VarSrc is a reference to an object (i.e. it has vtType VT_DISPATCH or VT_UNKNOWN), the reference counter of the object is increased (calling the object’s AddRef).

 

Note that the destination variant must be an initialized variant (i.e. created with any of the variations of VariantCreate), and its original contents will be freed by this subroutine.

 

Also note that VariantCopy does not de-reference VarSrc, so if the source variant has the flag VT_BYREF, the destination variant will also have this flag set. To create a de-referenced copy of a variant, you can use subroutine VariantCopyInd.

Argument iRet

 

Indicates success or failure of the subroutine. The following codes can be returned in this argument:

 

 

Value returned in argument iRet Description
S_OK Success.
DISP_E_BADVARTYPE The source or destination variants have an invalid vt. This usually happens when the variants have not been initialized.
DISP_E_ARRAYISLOCKED The destination variant contains an array that is locked
E_INVALIDARG One of the arguments is invalid.
E_OUTOFMEMORY Memory could not be allocated for the destination variant.

Examples

 

program VariantCopyExample

 

!Demonstrates subroutines VariantCopy and

!VariantCopyInd

!Copyright 1999-2000, Canaima Software

!All rights reserved

 

use f90VBDefs

use f90VBBStrings

use f90VBVariants

implicit none

 

type(VARIANT)::vVar1, vVar2, vVar3, vVar4

integer(BSTRHNDL_KIND)::BStr1

integer(HRESULT_KIND)::iRet

character(len=30)::TmpStr

 

!create a Variant containing a BString

vVar1 = VariantCreate(VT_BSTR,'Example string')

!initialize all the other variants to VT_EMPTY

vVar2 = VariantCreate(iRet) 

vVar3 = vVar2

vVar4 = vVar2

 

!create a variant that contains a reference to vVar1

!Set the fields manually

vVar2%varVal%vt = VT_VARIANT+VT_BYREF

vvar2%varVal%byref = loc(vVar1)

 

!print information for vVar1 and vVar2

print *,'Variant vVar1'

print *,'Memory location:', loc(vVar1)

call vtTypeToStr(vVar1%varVal%vt,TmpStr)

print *,'vtType:',trim(TmpStr)

print *,'bstrVal:',vVar1%varVal%bstrVal

 

print *,''

print *,'Variant vVar2'

call vtTypeToStr(vVar2%varVal%vt,TmpStr)

print *,'vtType:',trim(TmpStr)

print *,'byref:',vVar2%varVal%byref

 

!copy vVar2 to vVar3 using VariantCopy

!note that vVar2 is not de-referenced

!so vVar3 also holds a reference to 

!vVar1

call VariantCopy(vVar2,vVar3,iRet)

 

!print information for vVar3

print *,''

print *,'Variant vVar3'

call vtTypeToStr(vVar3%varVal%vt,TmpStr)

print *,'vtType:',trim(TmpStr)

print *,'byref:',vVar3%varVal%byref

 

!copy vVar2 to vVar4 using VariantCopyInd

!note that vVar2 has been de-referenced and

!vVar4 has a handle to a new BString, which

!has a copy of the BString in vVar1

call VariantCopyInd(vVar2,vVar4,iRet)

 

!print information for vVar3

print *,''

print *,'Variant vVar4'

call vtTypeToStr(vVar4%varVal%vt,TmpStr)

print *,'vtType:',trim(TmpStr)

print *,'bstrVal:',vVar4%varVal%bstrVal

 

 

!print the BString in vVar1

print *,''

call StrCopy(vVar1%varVal%bstrVal,TmpStr)

print *,'BString in vVar1: ', trim(TmpStr)

call StrCopy(vVar4%varVal%bstrVal,TmpStr)

print *,'BString in vVar4: ', trim(TmpStr)

 

!clear all variants

call VariantClear(vVar1)

call VariantClear(vVar2)

call VariantClear(vVar3)

 

 

stop

end

 

 

 

subroutine vtTypeToStr(vtType,vtTypeStr)

 

    !convert a vtType value into a string

    !for easy printing

    

    use f90VBDefs

    implicit none

 

    integer(VARTYPE_KIND),intent(in)::vtType

    character(len=*),intent(out)::vtTypeStr

 

    vtTypeStr=''

    

    !process the masks

    if (iand(vtType,VT_BYREF).gt.0) then

        vtTypeStr = trim(vtTypeStr)//'VT_BYREF|'

    endif

 

    if (iand(vtType,VT_ARRAY).gt.0) then

        vtTypeStr = trim(vtTypeStr)//'VT_ARRAY|'

    endif

    

    if (iand(vtType,VT_VECTOR).gt.0) then

        vtTypeStr = trim(vtTypeStr)//'VT_VECTOR|'

    endif

    

    !process the unmasked vtType

    select case (iand(vtType,VT_TYPEMASK))

        case(VT_EMPTY) 

            vtTypeStr = trim(vtTypeStr) // 'VT_EMPTY'

        case(VT_NULL)

            vtTypeStr = trim(vtTypeStr) // 'VT_NULL'

        case(VT_I2)

            vtTypeStr = trim(vtTypeStr) // 'VT_I2'

        case(VT_I4)

            vtTypeStr = trim(vtTypeStr) // 'VT_I4'

        case(VT_R4)

            vtTypeStr = trim(vtTypeStr) // 'VT_R4'

        case(VT_R8)

            vtTypeStr = trim(vtTypeStr) // 'VT_R8'

        case(VT_CY)

            vtTypeStr = trim(vtTypeStr) // 'VT_CY'

        case(VT_DATE)

            vtTypeStr = trim(vtTypeStr) // 'VT_DATE'

        case(VT_BSTR)

            vtTypeStr = trim(vtTypeStr) // 'VT_BSTR'

        case(VT_DISPATCH)

            vtTypeStr = trim(vtTypeStr) // 'VT_DISPATCH'

        case(VT_ERROR)

            vtTypeStr = trim(vtTypeStr) // 'VT_ERROR'

        case(VT_BOOL)

            vtTypeStr = trim(vtTypeStr) // 'VT_BOOL'

        case(VT_VARIANT)

            vtTypeStr = trim(vtTypeStr) // 'VT_VARIANT'

        case(VT_UNKNOWN)

            vtTypeStr = trim(vtTypeStr) // 'VT_UNKNOWN'

        case(VT_DECIMAL)

            vtTypeStr = trim(vtTypeStr) // 'VT_DECIMAL'

        case(VT_I1)

            vtTypeStr = trim(vtTypeStr) // 'VT_I1'

        case(VT_UI1)

            vtTypeStr = trim(vtTypeStr) // 'VT_UI1'

        case(VT_UI2)

            vtTypeStr = trim(vtTypeStr) // 'VT_UI2'

        case(VT_UI4)

            vtTypeStr = trim(vtTypeStr) // 'VT_UI4'

        case(VT_I8)

            vtTypeStr = trim(vtTypeStr) // 'VT_I8'

        case(VT_UI8)

            vtTypeStr = trim(vtTypeStr) // 'VT_UI8'

        case(VT_INT)

            vtTypeStr = trim(vtTypeStr) // 'VT_INT'

        case(VT_UINT)

            vtTypeStr = trim(vtTypeStr) // 'VT_UINT'

        case(VT_VOID)

            vtTypeStr = trim(vtTypeStr) // 'VT_VOID'

        case(VT_HRESULT)

            vtTypeStr = trim(vtTypeStr) // 'VT_HRESULT'

        case(VT_PTR)

            vtTypeStr = trim(vtTypeStr) // 'VT_PTR'

        case(VT_SAFEARRAY)

            vtTypeStr = trim(vtTypeStr) // 'VT_SAFEARRAY'

        case(VT_CARRAY)

            vtTypeStr = trim(vtTypeStr) // 'VT_CARRAY'

        case(VT_USERDEFINED)

            vtTypeStr = trim(vtTypeStr) // 'VT_USERDEFINED'

        case(VT_LPSTR)

            vtTypeStr = trim(vtTypeStr) // 'VT_LPSTR'

        case(VT_LPWSTR)

            vtTypeStr = trim(vtTypeStr) // 'VT_LPWSTR'

        case(VT_RECORD)

            vtTypeStr = trim(vtTypeStr) // 'VT_RECORD'

        case(VT_FILETIME)

            vtTypeStr = trim(vtTypeStr) // 'VT_FILETIME'

        case(VT_BLOB)

            vtTypeStr = trim(vtTypeStr) // 'VT_BLOB'

        case(VT_STREAM)

            vtTypeStr = trim(vtTypeStr) // 'VT_STREAM'

        case(VT_STORAGE)

            vtTypeStr = trim(vtTypeStr) // 'VT_STORAGE'

        case(VT_STREAMED_OBJECT)

            vtTypeStr = trim(vtTypeStr) // 'VT_STREAMED_OBJECT'

        case(VT_STORED_OBJECT)

            vtTypeStr = trim(vtTypeStr) // 'VT_STORED_OBJECT'

        case(VT_BLOB_OBJECT)

            vtTypeStr = trim(vtTypeStr) // 'VT_BLOB_OBJECT'

        case(VT_CF)

            vtTypeStr = trim(vtTypeStr) // 'VT_CF'

        case(VT_CLSID)

            vtTypeStr = trim(vtTypeStr) // 'VT_CLSID'

        case default

           vtTypeStr = trim(vtTypeStr) // 'VT_ILLEGAL'

    end select

 

end subroutine vtTypeToStr

Related Topics

 

For information about: See:
Changing the type of a variant VariantChangeType
Copying a de-referenced Variant VariantCopyInd

 

 

 


VariantCopyInd