Calling CVF-Fortran-dll with ctypes and simple structure

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Apr 7 10:26:23 EDT 2008


En Mon, 07 Apr 2008 09:19:03 -0300, Michael Schäfer <mcat.schaefer at gmx.de>  
escribió:

> Hi all,
>
> I deal with the old problem passing characters from python to a
> fortran dll build with CFV6.6c.
> I reduced our complex structure to a simple one.  Here is the Fortran
> code:
>
>       SUBROUTINE DEMO2L(s)
>
> C     sample for calling CVF6.6c-DLLs from
> C     vb/vba/python with simple structure
>
> 	!DEC$ ATTRIBUTES DLLEXPORT::DEMO2L
>
> 	TYPE rcDEMO
> 	   INTEGER a
> 	   REAL b
> 	   CHARACTER c*9
> 	END TYPE
>
> 	TYPE(rcDEMO) :: s
> C
> 	WRITE(*,*) s.a, s.b, s.c
> C     sample calculation:
> 	s.a = s.a*10
> 	s.b = s.b**2.3
>         s.c = 'Sample'
>
>       RETURN
>       END
>
> From VB the calling is very simple:
>
> Declare Sub DEMO2L Lib "release\demo1l.dll" (ByRef k As rcDEMO)
>
> Type rcDEMO
>   a As Long
>   b As Single
>   c As String * 9
> End Type
>
> Dim k As rcDEMO
>
> Sub run()
>
>     k.a = 2
>     k.b = 3#
>     k.c = "Test"
>
>     Call DEMO2L(k)
>
>     Debug.Print k.a, k.b, k.c
>
> End Sub
>
> and result to: "  20            12,5135      Sample"
>
> When I try this from python:
>
> from ctypes import *
>
> class rcDemo(Structure):
>   _fields_ = [
> 				('a', c_int),
> 				('b', c_float),
> 				('c', c_char_p),

Try with ('c', c_char*9). You may have alignment issues with such odd  
size, but in this case it doesnt matter so much as it's the last field in  
the struct.
(If both Fortran and VB say "char*9", why did you choose a pointer here?)

-- 
Gabriel Genellina




More information about the Python-list mailing list