本文介绍了如何从C#传递的参数类型为CHARACTER * 50的参数FORTRAN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下FORTRAN:

I have the following FORTRAN:

  SUBROUTINE MYSUB(MYPARAM)
  !DEC$ ATTRIBUTES DLLEXPORT::SetPaths

  CHARACTER*50 MYPARAM

  WRITE(6, *) MYPARAM

  END SUBROUTINE

然后我有在C#以下

class Program
{
    static void Main(string[] args)
    {
        StringBuilder sb = new StringBuilder(50);
        sb.Append(@"something");
        MYSUB(sb);

        Console.ReadLine();
    }

    [DllImport(@"myCode.dll", EntryPoint = "MYSUB")]
    public static extern void MYSUB(StringBuilder input);

}



不过,在我FORTRAN的WRITE显示了一堆垃圾后的东西。看起来像一个字符串结束没有被兑现。救命啊!

However, the WRITE in my FORTRAN shows a bunch of junk after "something." Looks like the string terminator is not being honored. Help!

推荐答案

字符串是不同语言之间进行交换的最棘手的数据类型。

Strings are the trickiest datatype to interchange between different languages.

基本Fortran语言的字符串长度是固定的,用空格填充结束。 (Fortran语言现在已经变长字符串,但那些将更难交汇处。)特性微调提供给抑制尾随空白; LEN_TRIM提供长度不尾随空白。

The basic Fortran string is fixed length, padded on the end with blanks. (Fortran now has variable length strings, but those would be harder to interchange.) The intrinsic "trim" is provided to suppress trailing blanks; "len_trim" to provide the length less trailing blanks.

C标记以空字符的字符串的结束。

C flags the end of a string with a null character.

我不知道C#如何处理字符串 - 为长度的内部变量?终结者??

I don't know how C# handles strings -- an internal variable for the length?? a terminator??

但Fortran语言是不会理解C#的代表,它只会看到字符串的全长,所申报,其中包括,在这种情况下,未初始化的内存。最好的解决办法可能是初始化字符串在C#中空白的剩余部分。

But Fortran isn't going to understand C#'s representation, it will just see the full length of the string, as declared, including, in this case, uninitialized memory. The best solution is probably to initialize the remainder of the string to blanks in C#.

这篇关于如何从C#传递的参数类型为CHARACTER * 50的参数FORTRAN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:13