Fortran到C ++到VB.NET并返回 [英] Fortran to C++ to VB.NET and back

查看:116
本文介绍了Fortran到C ++到VB.NET并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在尝试将2个系统集成在一起,第一个系统基于FORTRAN和C,第二个系统是.NET。

第一个系统,FORTRAN代码调用子程序,我试图构建一个.NET DLL来替换一些FORTRAN子程序,所以我发现最好的方法是在C / C ++中创建一个包装器,这个包装器是一组将由FORTRAN调用的extern C函数,这些函数将参数从FORTRAN传递给.NET,因为所有参数都通过引用传递,FORTRAN应该获取新值。



嗯,这并不是那么简单,FORTRAN将char *传递给C,这是非托管类型,C中的字符串也不受管理,因此我无法将其传递给.NET,因为.NET需要托管类型。



我不熟悉不同平台之间的数据类型转换,所以我需要实现的是以下之一:

1-找到一种方法来链接.NET DLL与FORTRAN,.NET子程序shou ld全局公开,以便FORTRAN应该能够直接调用它们而无需创建对象。

OR

2-找到一种从C传递非托管字符串的方法/ C ++到VB.NET dll,这样FORTRAN应该能够引用变量(使用正确的编码)。



如果我之前的解释,我道歉对一些读者来说没有意义,我知道.NET,但这是我的第一个C / C ++程序,我根本不熟悉旧的东西。



我会尝试解释更多...,我需要的是一个带有两个参数(char *,int)的extern C子例程,这个子例程应该将char *转换为托管类型和通过引用.NET子例程传递它,.NET子应该更改参数的值。

现在C子例程需要将托管类型(从.NET返回)转换为非托管类型键入并将新值放在原始字符数组中,以便FORTRAN可以获取新价值!



请帮忙。

谢谢

Hello,
I am trying to integrate 2 systems together, the first system is built on FORTRAN and C, the second system is .NET.
In the first system, FORTRAN code calls subroutines, I am trying to build a .NET dll to replace some of the FORTRAN subroutines, so I found that the best way to do it is to create a wrapper in C/C++, this wrapper is a set of extern C functions that will get called by FORTRAN, these functions pass the parameters from FORTRAN to .NET, since all parameters are passed by reference, FORTRAN should get the new values.

Well, it''s not that simple, FORTRAN passes char* to C, which is unmanaged type, string in C also is not managed so I can''t pass it to .NET, because .NET expects managed types.

I am not so familiar with datatype conversions between different platforms, so what I need to achieve is one of the following:
1- Find a way to link .NET dll with FORTRAN, .NET subroutines should be globally exposed, so that FORTRAN should be able to call them directly without the need to create objects.
OR
2- Find a way to pass unmanaged string from C/C++ to VB.NET dll, so that FORTRAN should be able to have a reference to the variables (with the correct encoding).

I Apologize if my previous explanation doesn''t make sense to some of the readers, I know .NET, but this is my first C/C++ program, I''m not familiar with the old stuff at all.

I''ll try to explain more..., what I need is a an extern C subroutine that has tow parameters (char* , int), this subroutine should convert the char* to a managed type and pass it by reference to a .NET subroutine, the .NET sub should change the value of the parameter.
Now the C subroutine needs to convert the managed type (that is returned from .NET) to unmanaged type and put the new value in the original character array, so that FORTRAN can get the new value!

Please help.
Thanks

推荐答案

你的C ++ DLL应该以混合模式(/ clr)编译。 C ++ / CLI允许您使用常规C / C ++和.NET托管类型。



您的C ++ / CLI DLL应该可以从Fortran调用(假设您正确导出C样式函数)并且它应该能够调用VB.NET代码(前提是如果有必要,你可以找到装配。



您也可以使用回调和正确的转换进入另一个方向。



欲了解更多信息,您可以使用Google。一些链接:



http:// msdn.microsoft.com/en-us/library/ms973872.aspx [ ^ ]



从非托管代码调用托管代码,反之亦然 [ ^ ]
Your C++ DLL should be compiled in mixed mode (/clr). C++/CLI will allows you to uses both regular C/C++ and .NET managed types.

Your C++/CLI DLL should be callable from Fortran (assuming that your properly export C style functions) and it should be able to call VB.NET code (provided the assembly can be found but you can hook into that if necessary).

You can also go in the other direction using callbacks and proper conversion.

For more information, you can uses Google. A few links:

http://msdn.microsoft.com/en-us/library/ms973872.aspx[^]

Calling Managed Code from Unmanaged Code and vice-versa[^]


Sameer Alomari写道:
Sameer Alomari wrote:

不,我们需要DLL是.NET,这样做的目的是开始将系统迁移到.NET,一步一步,并且功能按功能。

No, We need the DLL to be .NET, the purpose of doing this is to start migrating the system to .NET, step by step, and function by function.

不是。您还可以使用本机(非托管)DLL。有两种方法:使用P / Invoke或使用混合模式C ++ / CLI项目(托管+非托管),您可以使用它来生成DLL模块,以用作.NET可以引用的常规.NET程序集。汇编。



我在过去的答案中描述了这两种方法:

处理Windows窗体应用程序 [ ^ ],

如何在ASP.NET中调用C ++ DLL? [ ^ ]。



-SA

Not really. You also can use native (unmanaged) DLL. There are two ways: using P/Invoke or using mixed-mode C++/CLI project (managed + unmanaged) which you can use to produce a DLL module, to be used as a regular .NET assembly which can be referenced by your .NET assembly.

I described these two approaches in my past answers:
Dealing with windows form application[^],
How to invoke C++ DLL in ASP.NET?[^].

—SA


>>我对这些旧东西并不熟悉

嗯,我一直在阅读,有点同情你的困境 - 在这里我几乎失去了任何兴趣...



您使用的是哪个Fortran编译器?英特尔,GNU,其他?这很重要,因为有些东西被称为调用约定,编译器可能会有所不同:调用约定 [< a href =http://en.wikipedia.org/wiki/Calling_conventiontarget =_ blanktitle =New Window> ^ ]



现在,如果您使用的是英特尔编译器:



在C和C ++中,您可以使用 __ stdcall指定__stdcall调用约定函数定义中的关键字。这是用于调用大多数Windows API函数的调用约定。



以下C语言函数定义允许您使用__stdcall调用约定调用fortran子例程:

>> I''m not familiar with the old stuff at all
Hmm, I was reading along, somewhat sympathetic to your plight - and here I nearly lost any interest ...

Which fortran compiler are you using? Intel,GNU, other? This is important because there is something that is called a "calling convention" that might differ between the compilers: Calling convention[^]

Now, if you are using the Intel compiler:

In C and C++, you can specify the __stdcall calling convention by using the __stdcall keyword in a function definition. This is the calling convention used to call most of the Windows API functions.

The following C language function definition allows you to call to a fortran subroutine using the __stdcall calling convention:
extern void __stdcall CALCULTE_SOMETHING_IN_FORTRAN (int n); 





还有ATTRIBUTES指令,允许使用c调用约定调用fortran子例程:



There is also the ATTRIBUTES directive, which allows a fortran subroutine to be called using the c calling convention:

SUBROUTINE CALCULTE_SOMETHING_IN_FORTRAN (I)
  !DEC


这篇关于Fortran到C ++到VB.NET并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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