Re: Parameter Passing to Fortran?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



David,

While by convention Fortran array indexes start with 1, it perfectly valid
to define a zero based (or any arbitrary) index like this:

real b(0:4)

The [In, Out] has nothing to do with COM, it's an hint for the interop
(Pinvoke) marshaler that the function argument is a in/out argument.

Willy.


"David" <drose2929@xxxxxxxxx> wrote in message
news:ODRe8xcWGHA.128@xxxxxxxxxxxxxxxxxxxxxxx
| Willy,
|
| It worked with
| static extern int TestPass ([MarshalAs(UnmanagedType.LPArray)] float []
| yields);
|
| My mistake was to assume that fortran had 0 based arrays. As Eric pointed
| out in his response, fortran arrays are 1 based by default. I did read
the
| doc on arrays, but I am not a fortran programmer and I guess that they
| assume that everyone is - it is not obvious in the documentation (just
| checked again and found no mention). Anyway, I have used fortran in the
| past; I should have remembered.
|
| However, I am curious about the [In, Out] thing. Seems very COMish to me.
| Aren't those attributes meant to be used with COM?
|
| David
|
|
| "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message
| news:OB$uinbWGHA.4652@xxxxxxxxxxxxxxxxxxxxxxx
| > Right, you don't need the the size const and the arraysubtype, nor do
you
| > need the MarshalAs at all.
| > Try with: ([In, Out] float[] yields)
| > this should work, not sure if Fortran doesn't expect a cdecl calling
| > convention though, the default is stdcall.
| >
| >
| > Willy.
| >
| > "David" <drose2929@xxxxxxxxx> wrote in message
| > news:%23Xm895aWGHA.2376@xxxxxxxxxxxxxxxxxxxxxxx
| > |I tried
| > | [In, Out, MarshalAs(UnmanagedType.LPArray, SizeConst=5,
| > | ArraySubType=UnmanagedType.R4)]
| > |
| > | but no luck. Still returned an array of 0 length. (Actually, I do
not
| > | think that you need the SizeConst and ArraySubType unless you are
being
| > | called from .NET)
| > |
| > | Also tried adding ref, but got an exception (which makes sense if it
is
| > a
| > | pointer that is being passed).
| > |
| > | Thanks
| > |
| > | David
| > |
| > | "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message
| > | news:uzfFGkaWGHA.3800@xxxxxxxxxxxxxxxxxxxxxxx
| > | > TestPass([In, Out, MarshalAs(....)]
| > | >
| > | > Willy.
| > | >
| > | > "David" <drose2929@xxxxxxxxx> wrote in message
| > | > news:OGg3peaWGHA.3624@xxxxxxxxxxxxxxxxxxxxxxx
| > | > |I am attempting to pass an array from C# to Lahey Fortran and get
| > return
| > | > | values from the array. The values are getting into the fortran,
but
| > the
| > | > | array that comes back has no values.
| > | > |
| > | > | Fortran code:
| > | > | function TestPass (floatArray) result (iRes)
| > | > | implicit none
| > | > | dll_export :: TestPass ! export function name
| > | > |
| > | > | integer :: Ires
| > | > | real, intent (in out) :: floatArray
| > | > |
| > | > | dimension floatArray(5)
| > | > |
| > | > | iRes = 0 ! Assign function result
| > | > |
| > | > | open (5,FILE='output.txt')
| > | > | write (5, 100) floatArray(3)
| > | > |
| > | > | floatArray(0) = 0.0
| > | > | floatArray(1) = 1.1
| > | > | floatArray(2) = 2.2
| > | > | floatArray(3) = 3.3
| > | > | floatArray(4) = 4.4
| > | > |
| > | > | ! correct values are written to file here...
| > | > | open (5,FILE='output.txt')
| > | > | write (5, 100) floatArray(3)
| > | > | 100 format(5X,'got here',5X,F3.3)
| > | > | close (5)
| > | > | end function
| > | > |
| > | > | C# code:
| > | > | static extern int TestPass (
| > | > | [MarshalAs(UnmanagedType.LPArray, SizeConst=5,
| > | > | ArraySubType=UnmanagedType.R4)]
| > | > | float [] yields);
| > | > |
| > | > | private void BtnTestClick(object sender, System.EventArgs e)
| > | > | {
| > | > | float [] floatArray = new float[5] {9.9F, 9.9F, 9.9F, 9.9F, 9.9F};
| > | > | TestPass(floatArray);
| > | > |
| > | > | // floatArray.Length == 0 after the function call
| > | > |
| > | > | for ( int i = 0; i < floatArray.Length; i++ )
| > | > | Trace.WriteLine(floatArray[i]);
| > | > | }
| > | > |
| > | > | How does Fortran deal with array parameters? It does not seem to
be
| > | > passing
| > | > | a pointer.
| > | > | What am I doing wrong in setting this up?
| > | > |
| > | > | Thanks for any help or suggestions.
| > | > |
| > | > | David
| > | > |
| > | > |
| > | > |
| > | >
| > | >
| > |
| > |
| >
| >
|
|


.



Relevant Pages

  • Re: passing fortran arrays
    ... name of the fortran array is a pointer, ... end program arrayfc ... end interface ...
    (comp.lang.fortran)
  • Re: passing fortran arrays
    ... name of the fortran array is a pointer, indicating the first element. ...
    (comp.lang.fortran)
  • Re: passing fortran arrays
    ... Gerry Ford wrote: ... name of the fortran array is a pointer, ...
    (comp.lang.fortran)
  • C interop with Fortran arrays
    ... The idea is that Fortran strictly avoids binary specs, so C is provided function calls to manipulate Fortran array descriptors. ... I think that BINDprocedures should have a specified binary structure for Fortran array descriptors, because the purpose is for Fortran to be able to use C function calls, and C is designed around well-defined binary interfaces. ...
    (comp.lang.fortran)
  • Re: BUFR format software?
    ... David J Taylor wrote: ... > Anyone have software for reading BUFR format? ... > I've been able to find some Fortran stuff but no example data to check it ...
    (borland.public.delphi.thirdpartytools.general)