对于 Fortran 中的字符类型,我应该使用什么 MarshalAs?

发布于 2024-08-23 16:44:47 字数 935 浏览 5 评论 0原文

我正在从 C# 调用 fortran 子例程。我必须传入的参数之一是字符 .ie,在 fortran 中该参数被声明为

character, intent(in)  ::  bmat*1

现在的问题是,在 C# 代码中,我应该将其编组为什么?我知道对于整数,我应该将其编组为[MarshalAs(UnmanagedType.I4)],但是字符呢?

编辑: 这是我的 fortran 代码:

  subroutine chartest(bmat)
    !DEC$ ATTRIBUTES DLLEXPORT::chartest
    !DEC$ ATTRIBUTES ALIAS:'chartest'::chartest
    !DEC$ ATTRIBUTES VALUE ::bmat
    character, intent(in)  ::  bmat*1
    if(bmat .eq. 'G')then
        print *, bmat
    else
        print *, ' no result '
    endif
   end

这是我的互操作代码:

    [DllImport(@"eigensolver_win32.dll")]
    public static extern void chartest( [MarshalAs(UnmanagedType.U1)] char bmat);

这是我调用例程的方式:

    char bmat = 'G';
    EigenSolver32.chartest(bmat);

我得到的结果是“无结果”,表明 if 未实现。

I am calling a fortran subroutine from C#. One of the parameter I have to pass in is character .i.e, in fortran that parameter is declared as

character, intent(in)  ::  bmat*1

The issue now is, in C# code, what should I marshaled it as? I know that for integer, I should marshal it as [MarshalAs(UnmanagedType.I4)], but what about character?

Edit: This is my fortran code:

  subroutine chartest(bmat)
    !DEC$ ATTRIBUTES DLLEXPORT::chartest
    !DEC$ ATTRIBUTES ALIAS:'chartest'::chartest
    !DEC$ ATTRIBUTES VALUE ::bmat
    character, intent(in)  ::  bmat*1
    if(bmat .eq. 'G')then
        print *, bmat
    else
        print *, ' no result '
    endif
   end

And this is my interop code:

    [DllImport(@"eigensolver_win32.dll")]
    public static extern void chartest( [MarshalAs(UnmanagedType.U1)] char bmat);

This is how I call the routine:

    char bmat = 'G';
    EigenSolver32.chartest(bmat);

The result I got was "no result", indicating that the if is not fulfilled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抠脚大汉 2024-08-30 16:44:47

FORTRAN 中的字符类型是无符号的 8 位数量。

[MarshalAs(UnmanagedType.U1)]

会工作的。

非标准 FORTRAN byte 类型是有符号的。它将是 UnmanagedType.I1

编辑:C# char 类型是 Unicode(16 位)类型。 C# byte 类型是与 FORTRAN 字符类型匹配的类型。

[DllImport(@"eigensolver_win32.dll")]
public static extern void chartest( [MarshalAs(UnmanagedType.U1)] byte bmat);

另外,如果我没记错的话,所有 FORTRAN 函数参数都是通过引用传递的,所以您可能需要这个。

[DllImport(@"eigensolver_win32.dll")]
public static extern void chartest( [MarshalAs(UnmanagedType.U1)] ref byte bmat);

我认为 [MarshalAs(UnmanagementType.U1)] 对于字节来说是多余的。

The character type in FORTRAN is an unsigned 8 bit quantity.

[MarshalAs(UnmanagedType.U1)]

Will work.

The non-standard FORTRAN byte type is signed. it would be UnmanagedType.I1

Edit: C# char type is a Unicode (16 bit) type. The C# byte type is the one that matches the FORTRAN character type.

[DllImport(@"eigensolver_win32.dll")]
public static extern void chartest( [MarshalAs(UnmanagedType.U1)] byte bmat);

Also, if I remember correctly all FORTRAN function arguments are passed by reference, so you may need this instead.

[DllImport(@"eigensolver_win32.dll")]
public static extern void chartest( [MarshalAs(UnmanagedType.U1)] ref byte bmat);

And I think that [MarshalAs(UnmanagedType.U1)] is redundant for byte.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文