对于 Fortran 中的字符类型,我应该使用什么 MarshalAs?
我正在从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FORTRAN 中的
字符
类型是无符号的 8 位数量。会工作的。
非标准 FORTRAN
byte
类型是有符号的。它将是UnmanagedType.I1
编辑:C# char 类型是 Unicode(16 位)类型。 C#
byte
类型是与 FORTRAN 字符类型匹配的类型。另外,如果我没记错的话,所有 FORTRAN 函数参数都是通过引用传递的,所以您可能需要这个。
我认为
[MarshalAs(UnmanagementType.U1)]
对于字节来说是多余的。The
character
type in FORTRAN is an unsigned 8 bit quantity.Will work.
The non-standard FORTRAN
byte
type is signed. it would beUnmanagedType.I1
Edit: C# char type is a Unicode (16 bit) type. The C#
byte
type is the one that matches the FORTRAN character type.Also, if I remember correctly all FORTRAN function arguments are passed by reference, so you may need this instead.
And I think that
[MarshalAs(UnmanagedType.U1)]
is redundant for byte.