Silverfrost Forums

Welcome to our forums

Generic procedure interface using external procedures

21 Feb 2013 8:22 #11594

How to properly define interface for a generic procedure using external 'alien' procedures?

What I have now is the following:

module ExcelSupport

  interface ExcelGetCell
    module procedure GetCellInt
    module procedure GetCellDouble
    module procedure GetCellStr
  end interface ExcelGetCell

  contains
  	subroutine GetCellInt(row,col,value)
      integer (SELECTED_INT_KIND(9)), intent(in) :: row, col
      integer (SELECTED_INT_KIND(9)), intent(out) :: value
      C_EXTERNAL ExcelGetCellInt 'ExcelGetCellInt' (val,val,ref)

      call ExcelGetCellInt(row,col,value)
    end subroutine GetCellInt

    
  	subroutine GetCellDouble(row,col,value)
      integer (SELECTED_INT_KIND(9)), intent(in) :: row, col
      real (SELECTED_REAL_KIND(15,307)), intent(out) :: value
      C_EXTERNAL ExcelGetCellDouble 'ExcelGetCellDouble' (val,val,ref)

      call ExcelGetCellDouble(row,col,value)
    end subroutine GetCellDouble


  	subroutine GetCellStr(row,col,value)
      integer (SELECTED_INT_KIND(9)), intent(in) :: row, col
      character(len=*) , intent(out) :: value
      C_EXTERNAL ExcelGetCellStr 'ExcelGetCellStr' (val,val,outstring)

      call ExcelGetCellStr(row,col,value)
    end subroutine GetCellStr
	
end module ExcelSupport

The problem is in the ExcelGetCellStr part. If I directly call C_EXTERNAL ExcelGetCellStr procedure, it works ok. As a part of generic procedure, I get garbage instead of a proper character string. Any ideas?

21 Feb 2013 8:39 #11595

Just figured it out myself! 😄

This seems to work:

  	subroutine GetCellStr(row,col,value)
      integer (SELECTED_INT_KIND(9)), intent(in) :: row, col
      character(len=*) , intent(out) :: value
      character (len=256) :: buf
      C_EXTERNAL ExcelGetCellStr 'ExcelGetCellStr' (val,val,outstring)

      call ExcelGetCellStr(row,col,buf)
      value = buf
    end subroutine GetCellStr
Please login to reply.