473,734 Members | 2,859 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

P/Invoke to Fortran DLL - weird parameters...

Hi all,

we're currently developing an application in the .NET environment that
needs to access a DLL implemented in Fortran. There is one procedure
with about 17 parameters, most of them arrays. After some trial and
error experiments, we've found out that

a) the parameters must be ordered by type, i.e. we cannot mix double,
integers and Strings (char-Arrays) - instead, we first need to put
all the float[]-Arrays and Int32[]-Arrays, and after that, all the
byte[]-Arrays
b) each byte[] parameter but the first must exist twice in our C#
representation of the DLL procedure ("DllImport. .. public extern
static void...")
c) we couldn't pass two-dimensional byte-arrays (byte[][]) representing
one-dimensional arrays of Strings. Instead, we're serializing these
two-dimensional arrays to one-dimensional arrays.

Doing it this way, the parameters get to the DLL as intended, and where
needed they also get back to our .NET context (in/out) - but we're a
little puzzled why this is the case...

Possibly, a) is just an artefact of not paying attention to b) (it may
be that when each byte[] parameter is given twice, float[] can be given
in between, however, since the first byte[] parameter must exist only
once, this is not very probable...)

Any ideas or pointers to documentation on this behavior? We've found
some information on p/invoke, but nothing about complex cases as the one
we are facing. Also, this behavior may be specific to Fortran DLLs...

kind regards,
david
--
Auch das geht vorüber. (Sufi-Weisheit)

Holger (David) Wagner Tel: +49 (89) 890 50 962
Dewetstrasse 1 Mobil: +49 (177) 274 12 45
D-80807 München Fax: +49 (177) 992741245

Nov 15 '05 #1
3 5821
David,

There definitely seems to be something wrong. Can you give the actuall
declarations (C equivalent) for the functions exported from the DLL? Also,
what is the calling mechanism for the functions exported from this DLL?

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Holger (David) Wagner" <da***@purple-sunshine.de> wrote in message
news:bm******** **@svr8.m-online.net...
Hi all,

we're currently developing an application in the .NET environment that
needs to access a DLL implemented in Fortran. There is one procedure
with about 17 parameters, most of them arrays. After some trial and
error experiments, we've found out that

a) the parameters must be ordered by type, i.e. we cannot mix double,
integers and Strings (char-Arrays) - instead, we first need to put
all the float[]-Arrays and Int32[]-Arrays, and after that, all the
byte[]-Arrays
b) each byte[] parameter but the first must exist twice in our C#
representation of the DLL procedure ("DllImport. .. public extern
static void...")
c) we couldn't pass two-dimensional byte-arrays (byte[][]) representing
one-dimensional arrays of Strings. Instead, we're serializing these
two-dimensional arrays to one-dimensional arrays.

Doing it this way, the parameters get to the DLL as intended, and where
needed they also get back to our .NET context (in/out) - but we're a
little puzzled why this is the case...

Possibly, a) is just an artefact of not paying attention to b) (it may
be that when each byte[] parameter is given twice, float[] can be given
in between, however, since the first byte[] parameter must exist only
once, this is not very probable...)

Any ideas or pointers to documentation on this behavior? We've found
some information on p/invoke, but nothing about complex cases as the one
we are facing. Also, this behavior may be specific to Fortran DLLs...

kind regards,
david
--
Auch das geht vorüber. (Sufi-Weisheit)

Holger (David) Wagner Tel: +49 (89) 890 50 962
Dewetstrasse 1 Mobil: +49 (177) 274 12 45
D-80807 München Fax: +49 (177) 992741245

Nov 15 '05 #2
Nicholas Paldino [.NET/C# MVP] wrote:
There definitely seems to be something wrong. Can you give the actuall
declarations (C equivalent) for the functions exported from the DLL? Also,
what is the calling mechanism for the functions exported from this DLL?


Unfortunately, I don't have access to the Fortran-Declarations -
however, we finally fixed the problem(s). One major problem was that
..NET automatically adds a parameter to byte[]-Arrays or multidimensiona l
bytearrays. While in the original Fortran-Signature, these parameters
(e.g. outParam1) exist only once - the C#-declaration must have
outParam1 and another outParam1X which is probably used to return the
length of the array.

We're ignoring this parameter, but if it's missing in the declaration,
everything gets messed up (without an error message or exception).

The float/int problem has "disappeared".. . probably, that was an
artefact of the problem with the strings.

What remains is that we cannot use LOGICAL on the Fortran side. We *can*
send arrays of bool/logical to the DLL - but when we invert the values,
we get back weird results (e.g. everything becomes true). By using
integers, we found out that Fortran seems to simply change the sign and
subtract 1, so 0 becomes -1, 1 becomes -2, 2 becomes -3 and so on...
seems like Fortran is only interested in the last bit while C#/.NET
compares with 0. We fixed this by using integers in the Fortran-DLL an
converting them to logicals 1=true, 0=false.

The import-statement on the C#/.NET side now looks as follows:

[DllImport("dllN ame.DLL",
BestFitMapping= false,
CallingConventi on=CallingConve ntion.Winapi,
CharSet=CharSet .Ansi,
EntryPoint="PRO CEDURENAME",
ExactSpelling=t rue,
PreserveSig=tru e,
SetLastError=fa lse,
ThrowOnUnmappab leChar=true
)]
public extern static void wstksdll(
[In] float[] inParam1, // REAL (4), 200
[In] Int32[] inParam2, // INTEGER (4), 21
[In] float[] inParam3, // REAL (4), 21
[In, Out] byte[] outParam1, // CHARACTER 34, 40, 34
[In, Out] Int32 outParam1Length ,// not in Fortran-Signature!!!
[In, Out] Int32[] outParam2, // INTEGER (4), 40
[In, Out] float[] outParam3, // REAL (4), 40
[In, Out] float[] outParam4, // REAL (4), 40
[In, Out] byte[] outParam5, // CHARACTER 14, 40, 14
[In, Out] Int32 outParam6, // CHARACTER 34, 20, 34
[In, Out] Int32[] outParam7, // INTEGER (4), 40
[In, Out, MarshalAs(Unman agedType.LPArra y,
ArraySubType=Un managedType.I1)]
bool[] outParam8, // LOGICAL (4), 40
[In, Out] byte[] outParam9, // CHARACTER 34, 20, 34
[In, Out] Int32 outParam9Length ,// CHARACTER 30, 20, 30
);
--
Auch das geht vorüber. (Sufi-Weisheit)

Holger (David) Wagner Tel: +49 (89) 890 50 962
Dewetstrasse 1 Mobil: +49 (177) 274 12 45
D-80807 München Fax: +49 (177) 992741245

Nov 15 '05 #3
Holger,
Unfortunatel y, I don't have access to the Fortran-Declarations -
however, we finally fixed the problem(s). One major problem was that
.NET automatically adds a parameter to byte[]-Arrays or multidimensiona l
bytearrays. While in the original Fortran-Signature, these parameters
(e.g. outParam1) exist only once - the C#-declaration must have
outParam1 and another outParam1X which is probably used to return the
length of the array.
It's good to hear that you got it working. But I can assure you that
C# or the .NET runtime doesn't magically add parameters like that.

I don't know enough about Fortran to even guess how it exports
functions or why your changes made it work, but there certainly aren't
any hidden parameters being added on the managed side.

What remains is that we cannot use LOGICAL on the Fortran side. We *can*
send arrays of bool/logical to the DLL - but when we invert the values,
we get back weird results (e.g. everything becomes true). By using
integers, we found out that Fortran seems to simply change the sign and
subtract 1, so 0 becomes -1, 1 becomes -2, 2 becomes -3 and so on...


That's a binary NOT operation (~ in C#).

~0 == -1
~2 == -3

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #4

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

Similar topics

3
3981
by: lamar_air | last post by:
I have a fortran executable which when run from cmd it asks for a series of parameters which you enter then hit enter. From my python cgi script i want to be able to run the executable. Enter the 4 or 5 parameters needed then end the executable and redirect to another web page which will display some results given from an output file from the fortran executable. When the user clicks submit on the form it seems to hang up on the...
7
2173
by: To Forum | last post by:
hi, how can i use existing library in Fortran for my C++ program. I have try f2c but it is rather cubersome... Can I compile the Fortran code into Object file and then link to C++ code? My thanks TF
1
6432
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of the array in fortran and then accessing it in my c++ code. Say in my c++ code I have; extern "C" { void foo_(float **, int &, int &); }
5
2222
by: google | last post by:
first, a little background... i have a C program which preprocesses some data, and then outputs the results into a text file. that text file, in turn, is used as input to a FORTRAN computational fluid dynamics (CFD) program. the FORTRAN program goes to work on the provided data, and outputs a text file which i read back in with another C program to postprocess the data and analyze the results of the CFD run. now on to the problem. ...
9
3820
by: travisperkins03 | last post by:
Hi, I have read somewhere that C code sometimes cannot be compiled to be as efficient as FORTRAN, eg for matrix multiplication, because a C compiler cannot make the assumptions about arrays that a FORTRAN compiler can. But I don't understand the example, not least because I don't understand FORTRAN. I also don't understand why it is more efficient in this case for a compiler to choose the order of evaluation (or whatever it is that it...
14
7350
by: stic | last post by:
Hi, I'm in a middle of writing something like 'exception handler wraper' for a set of different methodes. The case is that I have ca. 40 methods form web servicem, with different return values (and types), and with out parmeters. What I want to do is to support each method call with exception (http 404, soap exception, and other types of exceptions) and wrap it with try & catch (a lots of catch ;-)
4
3584
by: Charles Law | last post by:
Hi guys. I have two threads: a main thread and a background thread. Lots of stuff happens in the background thread that means I have to update several (lots) of controls on a form. It is quite tiresome to have to write code to call MyControl.Invoke for each control on the form, along with the delegates that are required for each. Is there a better way to do this? What I mean is, if I could marshal the
2
4433
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p * len(id) # id is a list of strings Id_dat=StringVector() for i in range(len(Id)): ....Id_dat=id
5
4187
by: rafal.m | last post by:
Hi This code fails: class K2 { public function increment(&$obj) { $obj += 1; }
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9452
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6737
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6033
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3262
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.