!
! SafeArray sample
!
!The safarray example demonstrates converting a Fortran two dimensional
!array to a COM SafeArray and reading the data back from the SafeArray.
!SafeArrays are used in COM interfaces.
!

  PROGRAM SafeArrayTest

	  USE DFCOMTY
	  USE DFCOM
	  IMPLICIT NONE                       

	  INTEGER*4 result, i, j, value
	  INTEGER*4, DIMENSION(3,3) :: array
	  DATA ((array(i,j), i=1,3), j=1,3) /11, 22, 33, 44, 55, 66, 77, 88, 99/

	  INTEGER*4 typ
	  TYPE(sa_bounds), DIMENSION(2) :: ab
	  INTEGER*4, DIMENSION(2) :: indices
	  INTEGER*4	safeArray

	! Convert the Fortran array into an OLE SafeArray
	  ab(1)%extent = UBOUND(array, 1)
	  ab(1)%lbound = 1
	  ab(2)%extent = UBOUND(array, 2)
	  ab(2)%lbound = 1
	  safeArray = SafeArrayCreate(VT_I4, 2, ab(1))
	  DO j=1,ab(2)%extent
		DO i=1,ab(1)%extent
		  indices(1) = i
		  indices(2) = j
	      result = SafeArrayPutElement(safeArray, indices(1), LOC(array(i,j)))
	    END DO
	  END DO

    ! Read the values back and print them
	  DO j=1,ab(2)%extent
		DO i=1,ab(1)%extent
		  indices(1) = i
		  indices(2) = j
	      result = SafeArrayGetElement(safeArray, indices(1), LOC(value))
	      PRINT *, value 
	    END DO
	  END DO

	! Destroy the SafeArray
	result = SafeArrayDestroy(safeArray)

	END

