我应该怎么对的MarshalAs在Fortran语言字符类型? [英] What Should I MarshalAs for Character Type in Fortran?

查看:236
本文介绍了我应该怎么对的MarshalAs在Fortran语言字符类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话从C#,Fortran子例程。我有通过在参数是性格.IE,在FORTRAN该参数被声明为

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

现在的问题是,在C#代码,我应该怎么封它为?我知道,整数,我应该元帅为 [的MarshalAs(UnmanagedType.I4)] ,但对于< ?code>字符

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?

编辑:这是我的Fortran代码:

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

这是我的互操作代码:

    [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);



我得到的结果是没有结果,说明如果不满足。

推荐答案

在FORTRAN的字符类型是一个无符号的8位数。

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

[MarshalAs(UnmanagedType.U1)]

将工作。

非标准FORTRAN 字节类型签。这将是 UnmanagedType.I1

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

编辑:C#char类型是一个Unicode(16位)类型。 C#的字节类型是FORTRAN字符类型相匹配的。

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);



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

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);

和我认为, [的MarshalAs(UnmanagedType.U1)] 是多余的字节。

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

这篇关于我应该怎么对的MarshalAs在Fortran语言字符类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆