FORTRAN getting started -- C # Calling Fortran DLL

Source: Internet
Author: User

First, create a FORTRAN dynamic Connection Library Project and write a function to calculate the sum of the two numbers. The Code is as follows:

1 function mysum (x, y)
2 implicit none
3! Dec $ attributes dllexport: mysum
4! Dec $ attributes alias: 'mysum': mysum
5 integer x, y, mysum
6 mysum = x + y
7 end Function

After realse compilation, copy the generated DLL.

Create a C # console project, paste the copied DLL to the DEBUG directory, and add the code.

1 class Program
2 {
3 [dllimport ("dll1.dll", setlasterror = true, charset = charset. Unicode, callingconvention = callingconvention. stdcall)]
4 public static extern int mysum (ref int X, ref int y );
5 static void main (string [] ARGs)
6 {
7 int x = 3;
8 int y = 4;
9 int result = mysum (ref X, ref y );
10 console. writeline (result );
11 console. readkey ();
12}
13}

You need to add the namespace: system. runtime. interopservices;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.