! This software VARIANT.F90 is part of the Compaq Visual Fortran kit.
!
! 
! This software is provided as an example that demonstrates a particular
! feature/function of the licensed product. The software is not intended to
! provide a complete solution to any application problem but rather is provided
! as a teaching mechanism.
! 
! Users may incorporate any part of this sample into their own source after 
! making appropriate changes to make it suitable for the intended application.

!The VARIANT example demonstrates using the VARIANT manipulation routines
!in Visual Fortran.  VARIANTs are used in COM interfaces.
!

!****************************************************************************
!
!  PROGRAM: VARIANT_EXAMPLE
!
!  PURPOSE:  Variant Manipulation routine example.
!
!****************************************************************************

	program VARIANT_EXAMPLE

		USE DFCOMTY
		USE DFCOM
		IMPLICIT NONE                       

	! Variables

	    INTEGER*4 status
	    INTEGER*4 length
		CHARACTER*80 char1
		CHARACTER*80 char2

    ! Variants

		TYPE(VARIANT) V1
		TYPE(VARIANT) V2
		TYPE(VARIANT) V3

	! Body of VARIANT_EXAMPLE

		! Variants should be initialized using VariantInit
		CALL VariantInit(V1)	
		CALL VariantInit(V2)	
		CALL VariantInit(V3)	

		V1%VT = VT_BSTR
		V1%VU%PTR_VAL = ConvertStringToBSTR("Compaq Visual Fortran")
		V2%VT = VT_I4
		V2%VU%LONG_VAL = 6

		! VariantCopy{Ind] frees the destination variant and makes a copy of the 
		! source variant
		status = VariantCopy(V3, V1)

		! VariantClear releases any resources used by the VARIANT,
		! and re-initializes the VARIANT
		status = VariantClear(V3)

		! VariantChangeType[Ex] convert a VARIANT from one type to another.
		! The source and the destination can be different VARIANTs, or you
		! can use the same VARIANT and convert in place.

		! Convert to REAL
		status = VariantChangeType(V3, V2, 0, VT_R4)		
		! Convert to BSTR, in place
		status = VariantChangeTypeEx(V3, V3, 0, 0, VT_BSTR)

		length = ConvertBSTRToString(V1%VU%PTR_VAL, char1)
		length = ConvertBSTRToString(V3%VU%PTR_VAL, char2)
		WRITE(*,*) TRIM(char1) // " " // TRIM(char2)
         
		status = VariantClear(V1)
		status = VariantClear(V2)
		status = VariantClear(V3)

	end program VARIANT_EXAMPLE

