将FORTRAN代码转换为C# [英] Converting FORTRAN code to C#

查看:214
本文介绍了将FORTRAN代码转换为C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我总是在这里搜索周围的东西,几乎所有的时间我都找到了我需要的东西。

但是知道我需要一些更具体的东西,我在谷歌也找不到它。



我在FORTRAN 77上有一个很大的代码。子程序和功能。

我想在这个新的C#应用​​程序中重用这些代码,我正在编程,我想在类或类似的东西上使这些函数和子程序可用。



正如我在网上看到的那样正确的方法是使用代码制作DLL并在Visual Studio中使用它。



但我不能这样做,我已经尝试过Lahey-Fujitsu Fortran,WATCOM和Silverfrost。



有了Lahey和WATCOM,我可以制作dll,但VS2008不接受它作为参考。

它说:请制作确保该文件是可访问的,并且它是一个有效的程序集或COM组件



使用silverfrost我可以引用DLL但是..当我运行代码时(在运行时)它说VS无法在DLL上找到任何导出。



我不知道该怎么做才知道。任何人都可以在这里发布一个例子,或者告诉我该怎么做?



我会非常感激。 :-)





谢谢,



卢卡斯

Hello guys,

I'm allways searching stuff around here, and almost all the time I find what I need.
But right know I need something a little more specific and I can't find it on google neither here.

I have a big code on FORTRAN 77. With a bunch of subroutines and functions.
And I want to re-use that code in this new C# app, that I'm programming, I would like to make those functions and subroutines avaliable on a class or something like that.

As I've seen on the web and seams that the right way is making a DLL with the code and using it in Visual Studio.

But I can't do that, I already tried Lahey-Fujitsu Fortran, WATCOM and Silverfrost.

With Lahey and WATCOM I can make the dll but VS2008 won't accept it as a reference.
It says: "Please make sure that the file is accessible, and that it is a valid assembly or COM component"

With silverfrost I can reference the DLL but.. when I run the code ( at runtime ) it says that the VS couldn't couldnt find any exports on the DLL.

I have no idea of what to do know. Can anyone post a exemple here or show me how to do that please?

I would be very gratefull. :-)


Thanks,

Lucas

推荐答案

大家好! :)



在不停的研究之后,我做了一个简单的例子。而现在我想我可以扩展到我的洞项目了!



要走的路是使用OPEN WATCOM FORTRAN(whitch是免费的,在这里下载: http://www.openwatcom.org/index.php/Main_Page [ ^ ]我使用VS2010,但我觉得没关系。



我会告诉你们怎么做!



简介:



我将在FORTRAN77中创建一个子程序,它将3个双打作为参数1作为值,其他2作为参考,我将其称为DON。



DON( double1,double2,double 3)



这个子程序将从C#代码调用,并且它可以返回(double1 + 1)和(double1 + 2)。



所以..这里是FORTRAN代码:



*
Hello there folks! :)

After non-stopping researches I just made an simple example work out. And now I think I can expand that to my hole project!

The way to go is by using OPEN WATCOM FORTRAN ( whitch is free, dowload it here: http://www.openwatcom.org/index.php/Main_Page[^] and I used VS2010 but I think it doesn't matter.

I will show you guys how to do it!

Intro:

I going to create a subroutine in FORTRAN77 that takes 3 doubles as parameters 1 as a value and other 2 as references, I will call it DON.

DON( double1, double2, double 3 )

This subroutine is gonna be called from a C# code and its supossed to return (double1+1) and ( double1+2 ).

So.. here is the FORTRAN code:

*


pragma aux DONDONexport parm(v alue * 8,参考,参考)





SUBROUTINE DON(DAA,DBB,DCC)

REAL * 8,DAA,DBB,DCC

DBB = DAA + 1

DCC = DBB + 1

返回

结束




*有关代码的重要信息是第一行!以*
pragma aux DON "DON" export parm(value*8, reference, reference)


SUBROUTINE DON(DAA,DBB,DCC)
REAL*8, DAA,DBB,DCC
DBB=DAA+1
DCC=DBB+1
RETURN
END


* Important stuff about the code is the first line! The one that starts with *

pragma ..yes开头的那个!那条线!它由OPEN WATCOM COMPILER使用,是一个指令,用于表示DLL接收double值作为值,而其他2个引用



*别忘了你的FORTRAN代码必须有正确的缩进。 (正确的空格数,我认为是6)



然后在WATCOMP中创建一个DLL项目,并将该代码添加为源代码。使用默认的make和link设置。



Build你将创建DLL,将它放在与.exe相同的文件夹中(不要尝试添加它)在你的项目中使用References的add reference,因为它不会起作用。)



现在C#代码:



我将发布我的洞类代码:



pragma ..yes! that line! It is used by the OPEN WATCOM COMPILER, is a directive used to indicate that the DLL receive a double as value, and other 2 references

* Dont forget that your FORTRAN code must have the right indentation. ( right number of spaces, I think is 6 )

Then create a DLL project in WATCOMP, and add that code as source. Use the default make and link settings.

Build and you will have created the DLL, put it in the same folder as your .exe (Don't try adding it in your project with "add reference" from References because it is not gonna work.)

Now the C# code:

I will post my hole class code:

public unsafe class Program
    {

        [DllImport("Lks.dll",
            CharSet = CharSet.Auto,
            CallingConvention = CallingConvention.StdCall)]
        public static extern void DON(
            [MarshalAs(UnmanagedType.R8)] double DAA,
            [MarshalAs(UnmanagedType.R8)] ref double DBB,
            [MarshalAs(UnmanagedType.R8)] ref double DCC
            );

        static unsafe void Main(string[] args)
        {
            //double TIME = 100.0;
            double DAA = 5.5;
            double DBB = 7;
            double DCC = 9;
            //START( ENERIN, VAL1);
            DON(DAA,ref DBB, ref DCC);

            Console.Write("val1 = " + DAA);
            Console.Write("val2 = " + DCC);
            Debug.WriteLine("VAR = " + DBB.ToString());
            Console.Write("Press any key to exit");
            Console.ReadKey(false);
        }
    }





就像Systme.Runtime.InteropServices一样,只需将其添加到顶部。



运行代码,你会得到:



val1 = 5,5val2 = 7,5Press任何退出的钥匙



这意味着一切顺利DAA = 5,5而DCC现在等于7.5。



谢谢大家!不要放弃。



Lucas



It's going to as for Systme.Runtime.InteropServices, just add that in the top.

Run the code and you will get:

val1 = 5,5val2 = 7,5Press any key to exit

Which means all went well DAA = 5,5 and DCC now equals to 7,5.

Thanks guys! And don't give up.

Lucas


这篇关于将FORTRAN代码转换为C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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