- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
IMPLICIT NONE
! Expose subroutine FTOCCONVERT to users of this DLL
!Dec$ ATTRIBUTES DLLEXPORT, STDCALL :: FTOCCONVERT
!Dec$ ATTRIBUTES ALIAS:'FTOCCONVERT' :: FTOCCONVERT
!!DEC$ ATTRIBUTES REFERENCE::VOUT
REAL(8), INTENT(OUT)::VOUT(4)
write(*,*)'subroutine VOUT=',VOUT(1)
VOUT(2) = 999.0
write(*,*)'subroutine Vin=',VIN(1)
write(*,*)'subroutine Vout=',VOUT(1)
END
The C# console code is like this:
using System.Runtime.InteropServices; //for PInvoke
{
class Class1
{
[DllImport("FTOCCONVERT.dll")]
public static extern double FTOCCONVERT(double VIN);
[STAThread]
static void Main(string[] args)
{
System.Console.Write("Enter a value and enter: ");
string str = System.Console.ReadLine();
double[] VIN = new double[4];
double[] VOUT = new double[4];
VIN[2]= 200.0;
System.Console.WriteLine("The result is: {0} ",VOUT[1]);
}
}
}
I already tested the fortran Dll with a fortran code and with a VB.NET with sucess!
What is wrong with the Fortran or C# code?
And if the arguments changed? per example, without arrays?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
First of all you must know that the indexes of arrays in C# begin with 0 while they begin with 1 in Fortran. Therefore you have to initialize your VIN with VIN[0] and VIN[1], and get VOUT with index [0] on C# side.
I changed the following lines in C# as you see them here:
public static extern double FTOCCONVERT(ref double VIN, ref double VOUT);
VIN[0]=Convert.ToDouble(str);
VIN[1]= 200.0;
VOUT[0]=FTOCCONVERT(ref VIN[0], ref VOUT[0]);
And on Fortran side added:
!DEC$ ATTRIBUTES REFERENCE::VIN
!DEC$ ATTRIBUTES REFERENCE::VOUT
And it worked for me. There are some problems with your last 2 lines in C# but I don't know what,becausemy knowledge in C# is almost 0! I commented out those lines and got the answer in the console anyway.
Hope this helps,
Sabalan.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
SUBROUTINE
FTOCCONVERT(VIN,VOUT)IMPLICIT NONE
!Dec$ ATTRIBUTES DLLEXPORT, STDCALL :: FTOCCONVERT
!Dec$ ATTRIBUTES ALIAS:'FTOCCONVERT' :: FTOCCONVERT
!DEC$ ATTRIBUTES REFERENCE::VIN
!DEC$ ATTRIBUTES REFERENCE::VOUT
REAL(8), INTENT(IN)::VIN(4)
REAL(8), INTENT(OUT)::VOUT(4)
VOUT(1) = ((VIN(1)-32.0)*5.0/9.0)VOUT(2) = 999.0
RETURN
END
using System;
using System.Runtime.InteropServices; //for PInvoke
namespace Testconversion
{
class Class1
{
[DllImport("FTOCCONVERT.dll")]
public static extern double FTOCCONVERT(ref double VIN, ref double VOUT);
[STAThread]
static void Main(string[] args)
{
System.Console.Write("Enter a value and enter:");
string str = System.Console.ReadLine();
double[] VIN = new double[4];
double[] VOUT = new double[4];
VIN[0]=Convert.ToD ouble(str);
VIN[1]= 200.0;
VOUT[0]= FTOCCONVERT(ref VIN[0],ref VOUT[0]);
System.Console.WriteLine("The result Vout is: {0} ",VOUT[0]);
}
}
}
VIN(2)200.000000000000REAL(8)
VIN(3)0.000000000000000E+000REAL(8)
VIN(4)0.000000000000000E+000REAL(8)
VOUT(2)999.000000000000REAL(8)
VOUT(3)0.000000000000000E+000REAL(8)
VOUT(4)0.000000000000000E+000REAL(8)
[0]300.0double
[1]200.0double
[2]0.0double
[3]0.0double
[0]NaNdouble
[1]999.0double
[2]0.0double
[3]0.0double
I hope you can help me...
regards,
gilac
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
As I said my knowledge in C# is nothing to count with. But What I can see is that the problem shows to be the way you declare and call the DLL. I changed the DLL declaration to "public static extern bool", declared a new variable "bool Ret;" and changed the call to:
Ret = FTOCCONVERT(
ref VIN[0], ref VOUT[0]);
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page