CanaimaSoft
f90SQL
Search
Contents
f90ADO
Links
f90VB
 
 
 
 

Using subroutine SafeArrayCreate and mapping Safe Arrays into Fortran Arrays using SafeArrayAccessData (SafeArrayCreateExamples)

This is a simple example illustrating different ways to create Safe Arrays using f90VB.

The example also illustrates how you can use subroutine SafeArrayAccessData to map a Fortran array into a Safe Array. This allows you to manipulate the data in Safe Arrays using the already familiar and efficient Fortran array manipulation capabilities.

Main points you should notice in this example include:

  • Note how you can create and initialize a new Safe Array using a Fortran array as a template:
    call SafeArrayCreate(SAHndlA, VT_I2, &
                         FortranArrayA)
  • You can also create new Safe Array indicating its dimensions and then initialize the Safe Array separately:
  • call SafeArrayCreate(SAHndlB, VT_I2, 1, 10) 
    do i=1,10
      call SafeArrayPutElement(SAHndlB, &
                  int(i,SHORT_KIND),i,iRet)     
    enddo
  • There are many other flexible and convinients way to create and represent Safe Arrays in f90VB. These are only two examples.
  • Finally, note how we map the Safe Array data into a Fortran array:
    call SafeArrayAccessData(SAHndlA,SMapArray,iRet)
    print *,'Safe Array SAHndlA:'
    print *,(SMapArray(i),i=1,10)
    call SafeArrayUnAccessData(SAHndlA)            
This is a very efficient method to access the data in the Safe Array. The Fortran Array (SMapArray) does not have a copy of the data; it shares the same memory as the Safe Array data. Any changes you make in SMapArray are automatically reflected in the Safe Array, and viceversa.

And here is the example:

 

 
program SafeArrayCreateExamples

!Demonstrates several different ways to create Safe Arrays
!Copyright 1999-2000, Canaima Software
!All rights reserved

use f90VBDefs
use f90VBSafeArrays
implicit none

!Declare several Safe Array handles and
!Fortran Safe Array Structures
integer(SAFEARRAY_KIND)::SAHndlA, SAHndlB, SAHndlC
type(FSafeArray)::FSA_A
integer(SHORT_KIND),dimension(10)::FortranArrayA
integer(HRESULT_KIND)::iRet
type(SafeArrayBound),dimension(2)::IdxBound
integer(SHORT_KIND),dimension(:),pointer::SMapArray

integer(LONG_KIND)::i

!Initialize Fortran Arrays
do i=1,10
    FortranArrayA(i)=i
enddo

!Allocate A Safe Array using Syntax Variation 4
!initializing the Safe Array using the data in FortranArrayA
call SafeArrayCreate(SAHndlA, VT_I2, FortranArrayA)

!Allocate a similar Safe Array using Syntax Variation 2
call SafeArrayCreate(SAHndlB, VT_I2, 1, 10)
!Initialize its data
do i=1,10
    call SafeArrayPutElement(SAHndlB,int(i,SHORT_KIND),i,iRet)
enddo

!Allocate a similar array using Syntax Variation 1
IdxBound(1)%LowerBound=1
IdxBound(1)%nElements=10
call SafeArrayCreate(SAHndlC, VT_I2, 1, IdxBound)
!Initialize its data
do i=1,10
    call SafeArrayPutElement(SAHndlC,int(i,SHORT_KIND),i,iRet)
enddo

!Allocate a similar SafeArray, returning the handle
!in a Fortran Safe Array structure
call SafeArrayCreate(FSA_A, VT_I2, FortranArrayA);

!Prints data in the four Safe Arrays using a
!mapped Fortran Array
call SafeArrayAccessData(SAHndlA,SMapArray,iRet)
print *,'Safe Array SAHndlA:'
print *,(SMapArray(i),i=1,10)
call SafeArrayUnAccessData(SAHndlA)

call SafeArrayAccessData(SAHndlB,SMapArray,iRet)
print *,'Safe Array SAHndlB:'
print *,(SMapArray(i),i=1,10)
call SafeArrayUnAccessData(SAHndlB)

call SafeArrayAccessData(SAHndlC,SMapArray,iRet)
print *,'Safe Array SAHndlC:'
print *,(SMapArray(i),i=1,10)
call SafeArrayUnAccessData(SAHndlC)

call SafeArrayAccessData(FSA_A,SMapArray,iRet)
print *,'Safe Array FSA_A:'
print *,(SMapArray(i),i=1,10)
call SafeArrayUnAccessData(FSA_A)

!Destroy Safe Arrays
call SafeArrayDestroy(SAHndlA,iRet)
call SafeArrayDestroy(SAHndlB,iRet)
call SafeArrayDestroy(SAHndlC,iRet)
call SafeArrayDestroy(FSA_A,iRet)

stop
end
Copyright © 1998-2000 Canaima Software
For questions regarding this site, send an e-mail to
webmaster@canaimasoft.com