! Subroutine FortranType
!   Parameters:
!      array_ptr:  Array of type EmployeeInfo, passed by reference
!					This array is passed from Visual Basic as a SafeArray
!				     which contains a lot of information beyond just the
!					 the array of EmployeeInfo's.
!	   num_elem:   Number of elements in array
! 
!  Note: The EmployeeInfo type is declared in type_global.f90
!
!  The purpose of this routine is to figure out each employee's paycheck
!  for the week.  The hours they worked in each of three categories is
!  multiplied by the rate for that category.  An array is filled in with
!  each of these numbers, the grand total is returned, and a flag is set
!  saying that this has been done.
! 
!  To illustrate one way of debugging these kinds of programs, (using a
!  preprocessor definition) a console window is created and each element
!  of the EmployeeInfo type is written to it.
!
!
!
subroutine FortranType (array_ptr, num_elem)
   !DEC$ ATTRIBUTES DLLEXPORT ::  FortranType
   !DEC$ ATTRIBUTES ALIAS :'FortranType' :: FortranType
   
   use type_global
   use dfwin

! Necessary to define interface to SafeArrayxxx calls 
   use dfcom
! Necessary to define interfaces to Multibyte->String calls
   use dfnls

   implicit none
	integer num_elem

! Declare array_ptr to be a pointer to an integer.  When Cray pointers
!  are declared, they must point to something; this DUMMY integer is
!  simply a placeholder so we can declare the pointer.
	integer dummy
	pointer (array_ptr, dummy)

! Declare another pointer, which will be used as the head of the
!  array of EmployeeInfo's.
	pointer (empl_ptr, empl_data)
    type(EmployeeInfo)    :: empl_data(num_elem)

! One of the elements of EmployeeInfo is a Unicode string.  
! Declare a Fortran string into which the Unicode string will be translated
	character(25) mystring

	integer status
	logical bstatus
	real*8 running_total
	integer i, j, k			! Loop variables

! Use the SafeArray routine to get the address of the array of EmployeeInfo's.
	status = SafeArrayAccessData( array_ptr, empl_ptr )

!dec$ if defined (DEBUG)
! For debugging purposes, create a console window and write the contents
! of the array elements out to that window.
	bstatus = AllocConsole()
	write (6, *) "Info from Empl_ptr"
	do i=1,num_elem

		write (6, *) i, "): "
		status = MBconvertUnicodeToMB(empl_data(i).strName, mystring)
		write (6, *) mystring
		write (6, *) "Badge: ", empl_data(i).lngEmployeeNumber
		write (6, *) "Hours at three rates: ", (empl_data(i).intHours(k), k=1,3)
		write (6, *) "Rates in question: ", (empl_data(i).dblHourlyRate(k), k = 1,3)
	end do
!dec$ endif

! Now, fill in the fields
	do i = 1, num_elem
	running_total = 0
	   do j=1, 3
		empl_data(i).dblCatTotals(j) = empl_data(i).intHours(j)*empl_data(i).dblHourlyRate(j)
		running_total = running_total + empl_data(i).dblCatTotals(j)
	   end do
	empl_data(i).dblTotal = running_total
	end do
		

! When you are done, you must 'deaccess' the data. 
	status = SafeArrayUnaccessData(array_ptr)

!dec$ if defined (DEBUG)
	bstatus = FreeConsole()
!dec$ endif

end subroutine FortranType