0

I am attempting to call the following FORTRAN method:

subroutine MYMETHOD(THEARRAY)

with the parameter

CHARACTER*200  :: THEARRAY(2)

This seems simple enough but I ran into trouble because it is an array of character arrays. I have tried several approaches to this including marshaling (which runs in to problems because there are multiple strings.) The best I can come up with is:

[DllImport("theFortranDLL.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void MYMETHOD(THEARRAY);

static void Main(string[] args)
{
    StringBuilder[] theArray = new StringBuilder[2];

    theArray[0] = new StringBuilder("DataA",200);
    theArray[1] = new StringBuilder("DataB",200);

    MYMETHOD(theArray);
}

However, this does not work. How would I call such a method from C#? Thanks!

4
  • Have you tried one StringBuilder with 400 chars? Commented Mar 14, 2013 at 3:43
  • Just tried it. The program I run it in produced the same output as my code here does. My code does run but FORTRAN complains when the data comes in. I think the data is arriving in a form FORTRAN wasn't expecting. Commented Mar 14, 2013 at 3:51
  • Did you try to pass string instead of StringBuilder? Commented Mar 14, 2013 at 3:55
  • Just tried that as well. No luck ): I have tried string[], string on its own, StringBuilder[], and StringBuilder on its own also some ref's with string[], etc. Does my code as it stands look correct? If so, I might be barking up the wrong tree. I don't currently have access to the FORTRAN's source code so it's possible the C# code is correct. The method is calling: it gets into the FORTRAN code and FORTRAN is printing a program specific warning that suggests the incoming data wasn't what it expected. Commented Mar 14, 2013 at 4:02

1 Answer 1

1

In Fortran, CHARACTER*200 :: THEARRAY(2) is not an array of arrays. It is a two element array of scaler strings of length 200. In other languages it might be an array of arrays. Each Fortran string is padded on the end with blanks. I don't know how C# handles strings ... C terminates strings with a null character. For Fortran, that null character would have to be replaced with blanks, all the way to position 200.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.