473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Calling a FORTRAN DLL with a structure parameter

Hi
As well as trying to call a fortran dll with a structure parameter from a C#
front end, we are trying to do this using AssemblyBuilder and MethodInfo to
do it dynamically. Ultimately this will be done using a configuration file,
but in the first instance I am trying to do this using code. The FORTRAN
structure looks like this:

TYPE Type1
! Scalars plus fixed dimension arrays
SEQUENCE
INTEGER :: &
iScalar1
DOUBLE PRECISION, DIMENSION(iFixDim1,iFixDim2) :: &
dArray1, &
dArray2
DOUBLE PRECISION :: &
dScalar1
INTEGER, DIMENSION(iFixDim1) :: &
iArray1
INTEGER :: &
iScalar2
END TYPE Type1

and the entry point for the dll subroutine looks like this:

!
================================================== ===================================
SUBROUTINE CreateType1 (iScalar1, dArray1, dArray2, dScalar1,
iArray1, &
iScalar2, rType)
!
================================================== ===================================
!DEC$ ATTRIBUTES DLLEXPORT :: CreateType1
!
! Purpose
! -------
! Create a Type 1 structure
!
! Arguments
! ---------
INTEGER, INTENT(IN) :: &
iScalar1, &
iScalar2

DOUBLE PRECISION, DIMENSION(iFixDim1,iFixDim2), INTENT(IN) :: &
dArray1, &
dArray2

DOUBLE PRECISION, INTENT (IN) :: &
dScalar1

INTEGER, INTENT(IN), DIMENSION(iFixDim1) :: &
iArray1

TYPE(Type1), INTENT(OUT) :: &
rType

! Begin
! -----
rType%iScalar1 = iScalar1
rType%iScalar2 = iScalar2
rType%dScalar1 = dScalar1
rType%iArray1 = iArray1
rType%dArray1 = dArray1
rType%dArray2 = dArray2

RETURN

END SUBROUTINE CreateType1
*/

The C# (finally) looks like this:

string fileName = @"C:\Data\Visual Studio
2005\projects\CallingFortranStructures\CallingFort ranStructures\recp.dll";
string entryPointName = "MDERECPRFTEST_mp_CREATETYPE1";

AssemblyBuilder dynamicAsm = null;
MethodInfo methodInfo = null;

Type[] parameterTypes = new Type[7];

parameterTypes[0] = typeof(int); //iScalar1
parameterTypes[0] = parameterTypes[0].MakeByRefType();

parameterTypes[1] = typeof(double[,]); //dArray1
parameterTypes[1] = parameterTypes[1].MakeByRefType();

parameterTypes[2] = typeof(double[,]); //dArray2
parameterTypes[2] = parameterTypes[2].MakeByRefType();

parameterTypes[3] = typeof(double); //dScalar1
parameterTypes[3] = parameterTypes[3].MakeByRefType();

parameterTypes[4] = typeof(int[]); //iArray1
parameterTypes[4] = parameterTypes[4].MakeByRefType();

parameterTypes[5] = typeof(int); //iScalar2
parameterTypes[5] = parameterTypes[5].MakeByRefType();

parameterTypes[6] = typeof(RTYPE1); //rType
parameterTypes[6] = parameterTypes[6].MakeByRefType();

// Create a dynamic assembly and a dynamic module
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "CreateType1";
dynamicAsm =
AppDomain.CurrentDomain.DefineDynamicAssembly(asse mblyName,
AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder =
dynamicAsm.DefineDynamicModule("CreateType1");

// Dynamically construct a global PInvoke signature
// using the input information
MethodBuilder methodBuilder =
moduleBuilder.DefinePInvokeMethod(entryPointName,

fileName,

MethodAttributes.Static | MethodAttributes.Public |
MethodAttributes.PinvokeImpl,

CallingConventions.Standard,

typeof(void),

parameterTypes,

CallingConvention.StdCall,

CharSet.Ansi);
methodBuilder.SetImplementationFlags(MethodImplAtt ributes.PreserveSig);
moduleBuilder.CreateGlobalFunctions();

// Get a MethodInfo for the PInvoke method and store it away for
later use:
methodInfo = moduleBuilder.GetMethod(entryPointName);

if (methodInfo != null)
{
//...Set up the parameters:
object[] parameterValues = new object[7];
//ParameterModifier parameterModifiers = new
ParameterModifier();

//...Set the parameter values ready for the call:
parameterValues[0] = 0;
parameterValues[1] = new double[5, 10];
parameterValues[2] = new double[5, 10];
parameterValues[3] = 0.0;
parameterValues[4] = new int[5];
parameterValues[5] = 0;

RTYPE1 rtype1 = new RTYPE1();
rtype1.iArray1 = new int[5];
rtype1.iScalar1 = 1;
rtype1.iScalar2 = 3;
rtype1.dArray1 = new double[5, 10];
rtype1.dArray2 = new double[5, 10];
rtype1.dScalar1 = 3.14;

parameterValues[6] = rtype1;

object returnValue = typeof(void);
returnValue = methodInfo.Invoke(null, parameterValues);
I appreciate that this is rather specific, but I was wondering whether
anyone could shed any light on this problem.

Thanks in advance.

Marek
Dec 3 '07 #1
3 1788
Hi Marek,

Honestly, I'm not familiar with Fortran so I'm not sure if I can really
help here. Could you please tell me can you currently call the exported
Fortran function using P/Invoke declaration? If this is the case, then the
question will be how to use AssemblyBuilder and MethodInfo, right?
Otherwise, we will need to deal with Fortran specific issues first such as
the specific calling convention, etc.

I'm wondering if you could send me a binary Fortran DLL to test, that would
make this more easier to work torward a solution. Thanks for your effort.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Dec 4 '07 #2
Hi Walter
Thanks for replying - I appreciate that this is a pretty esoteric post.

I have sent you my project and binaries as requested via email.

Best regards

Marek

""Walter Wang [MSFT]"" wrote:
Hi Marek,

Honestly, I'm not familiar with Fortran so I'm not sure if I can really
help here. Could you please tell me can you currently call the exported
Fortran function using P/Invoke declaration? If this is the case, then the
question will be how to use AssemblyBuilder and MethodInfo, right?
Otherwise, we will need to deal with Fortran specific issues first such as
the specific calling convention, etc.

I'm wondering if you could send me a binary Fortran DLL to test, that would
make this more easier to work torward a solution. Thanks for your effort.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Dec 4 '07 #3
Marek,

You should post to the newsgroup so that if/when a solution is
presented, others might benefit.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marek" <dn***@community.nospamwrote in message
news:D5**********************************@microsof t.com...
Hi Walter
Thanks for replying - I appreciate that this is a pretty esoteric post.

I have sent you my project and binaries as requested via email.

Best regards

Marek

""Walter Wang [MSFT]"" wrote:
>Hi Marek,

Honestly, I'm not familiar with Fortran so I'm not sure if I can really
help here. Could you please tell me can you currently call the exported
Fortran function using P/Invoke declaration? If this is the case, then
the
question will be how to use AssemblyBuilder and MethodInfo, right?
Otherwise, we will need to deal with Fortran specific issues first such
as
the specific calling convention, etc.

I'm wondering if you could send me a binary Fortran DLL to test, that
would
make this more easier to work torward a solution. Thanks for your effort.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

================================================= =
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
================================================= =

This posting is provided "AS IS" with no warranties, and confers no
rights.


Dec 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: byte biscuit | last post by:
Hi there everybody! The problem is the following: we have a DLL (H2O.dll) - compiled in Visual Fortran - depending in turn on another DLL. H2O.dll contains only one (1) function, with a known...
7
by: Anonymous | last post by:
I have a mixed-language pgm (Fortran and C++) which needs to pass a few hundred values from C++ to Fortran. One way to do this is to have a Fortran module and a C++ structure (or class) with an...
4
by: NM | last post by:
Hello All I am writing some progam that involves both C++ and Fortran. Some of the existing code is in Fortran. The main program will be in C++ and it will call some Fortran subroutine. All the...
5
by: Amit | last post by:
I tried calling a subroutine in a fortran module from C ,but couldn't.I always get the error: undefined reference in the main.o file (main is in C calling the subroutine). for calling the...
2
by: AlanL | last post by:
I am calling a Fortran DLL that has a declaration like: character * 260 variablename. I do not have the Fortran code. A path to a file is passed to the DLL. C# does not have a fixed character...
2
by: Ray J. | last post by:
I have a C++ program written and compiled on Solaris 8 with gcc. With gcc lets me compile fortran code along with the C++ program to be able to call the fortran code as a subroutine. The...
21
by: Cottonwood | last post by:
I want to call a C module from a Fortran program. Whatever I tried - the linker could not find the C module. I know about the leading underscore and switched even that off. I abstracted everything...
13
by: Mangabasi | last post by:
Howdy, I have been trying to call the following Fortran function from Python (using Windows XP, Compaq Fortran and Python 2.4). I tried F2Py, Pyfort and calldll with no success. I think I...
47
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.