473,466 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Need Help Calling A Fortran 77 (.for) DLL

4 New Member
Hey Everyone,

I am doing a VB6 to C# conversion and everything was going smoothly until I realized that I needed to call a Fortran 77 (.for) .dll inside my code. I have looked through everything I have found using Google and have still been unable to get it to successfully work. Currently the following call works about 75% of the time, but the other 25% of the time my program just calls the .dll and then it simply exits; no error/exception occurs, it just quits without executing anymore code. As you can see I have tried multiple ways to catch/generate error messages. I have no Fortran experience and I was told to leave the Fortran code alone, since it has been around and working since the 70’s.

From what I can tell the Fortran code only has one subroutine. I have included the header/parameters below:

SUBROUTINE FORTRANCALLA(INPUTL, TABLEL, NAMEL2, COMMAL, LOADL)
EXTERNAL F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F20

!DEC$ ATTRIBUTES DLLEXPORT :: FORTRANCALLA
!DEC$ ATTRIBUTES STDCALL, ALIAS: 'FORTRANCALLA' :: FORTRANCALLA
!DEC$ ATTRIBUTES REFERENCE :: INPUTL
!DEC$ ATTRIBUTES REFERENCE :: TABLEL
!DEC$ ATTRIBUTES REFERENCE :: NAMEL2
!DEC$ ATTRIBUTES REFERENCE :: COMMAL
!DEC$ ATTRIBUTES REFERENCE :: LOADL

CHARACTER(80), INTENT(IN) :: INPUTL
CHARACTER(80), INTENT(IN) :: TABLEL
CHARACTER(80), INTENT(IN) :: NAMEL2
CHARACTER(4), INTENT(IN) :: COMMAL
CHARACTER(80), INTENT(IN) :: LOADL


It appears C++ was used to wrap the .for file and generate a .dll which was called in the VB6 program using the following code:

Call Fortran.FORTRANCALLA(infilecalc, tablecalc, destination2, format, loadcond)

Public Declare Sub FORTRANCALLA Lib "Name.dll" _
(ByVal INPUTL As String, ByVal TABLEL As String, ByVal NAMEL2 As String, ByVal COMMAL As String, ByVal LOADL As String)

It appears that the VB6 program also has problems with the call, but not as frequently. However, it also handles the failed .dll call much better and does not crash. If possible I would just like my program not to fatally crash.

Here is the code I am currently working with and I am calling the exact same .dll that is currently being used in the VB6 version:

Expand|Select|Wrap|Line Numbers
  1. static class Fortran
  2.       {
  3.             [DllImport("Name.dll")]
  4.             private static extern bool FORTRANCALLA(
  5.                   StringBuilder infilecalc,
  6.                   StringBuilder tablecalc,
  7.                   StringBuilder destination2,
  8.                   StringBuilder format,
  9.                   StringBuilder loadcond);
  10.  
  11.             public static void Calculate(string infilecalc, string tablecalc, string destination2, string format, string loadcond)
  12.             {
  13.                   string tempOutputPath = FileMod.GetTemporaryDirectory() + "NameTemp.tmp";
  14.  
  15.                   StringBuilder bldrInFileCalc = new StringBuilder(infilecalc, 80);
  16.                   StringBuilder bldrTableCalc = new StringBuilder(tablecalc, 80);
  17.                   StringBuilder bldrDestination2 = new StringBuilder(tempOutputPath, 80);
  18.                   StringBuilder bldrFormat = new StringBuilder(format, 4);
  19.                   StringBuilder bldrLoadCond = new StringBuilder(loadcond, 80);
  20.  
  21.                   try
  22.                   {
  23.                         if (!FORTRANCALLA(bldrInFileCalc, bldrTableCalc, bldrDestination2, bldrFormat, bldrLoadCond))
  24.                               MessageBox.Show(new System.ComponentModel.Win32Exception().ToString());
  25.                   }
  26.                   catch (Exception e)
  27.                   {
  28.                         MessageBox.Show(e.Message);
  29.                   }
  30.  
  31.                   //FORTRANCALLA(bldrInFileCalc, bldrTableCalc, bldrDestination2, bldrFormat, bldrLoadCond);
  32.  
  33.                   File.Copy(tempOutputPath, destination2, true);
  34.                   File.Delete(tempOutputPath);
  35.             }
  36.       }
Right now I cannot find a pattern to the crashes. I could work with the same files, making identical calls to the .dll, and sometimes it works sometimes it doesn't. At first I thought it was something to do with memory allocation, but I don't think that is the case because sometimes it crashes on the first call. Sometimes it only crashes once, sometimes it crashes multiple uses in a row, and sometimes you can make many calls before it starts crashing again.

Does anyone have any ideas? Because I am officially out of ideas...

Thanks,
Nitsua
Aug 12 '08 #1
4 2291
Plater
7,872 Recognized Expert Expert
Are any of the parameters you pass to the fortran dll "return" or "out" parameters (or even passed by ref)?
If a StringBuilder is going to be modified by the function in an unmanaged DLL, you need to supply a default size for it, a size big enough to handle whatever text would be coming back. I see you appear to be giving sizes but I wonder if they are big enough?

EDIT: ok so you pass everything by value...and all that work just to get a true/false? Seems like a big waste, unless your strings become floating point numbers, fortran's not real usefull for anything other then floating point calculations
Aug 12 '08 #2
nitusa
4 New Member
The strings I pass in are actually file paths and one format paramater (txt, doc, ect) and the strings are never altered. The fortran is actually taking values from a couple of input files and doing calculations and then writing them to an output file in the request format. The function never actually passes out a bool, but if you make the call a bool then it lets you determine (sometimes) if the call failed or not.
Aug 12 '08 #3
Plater
7,872 Recognized Expert Expert
Hmm yes on closer look you are making everything to sizes matching that of the fortran header.
Is there any common pattern between filenames(and directories) or file types that could be the cause of the failure?
Such as fortran cannot handle a character in a special path or anything like that?
Aug 12 '08 #4
nitusa
4 New Member
I think I may have figured it out. One of the paths was a server path (\\server\share\...) and when I moved the file locally it seems to be working correctly. However, I don't know why a server location would cause the program to only fail some of the times. The old program appears to be working smoothly with the same server path.

Oh well, whatever works. At least now I can stop pulling my rapidly thinning hair out.

Thanks for the help,
Nitsua
Aug 12 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: vishnu mahendra | last post by:
Hello to all, I need some suggestions for programs for our java lab cycle. The topics are as follows OOPS MULTITHREADING FILE IO PACKAGES INTERFACES
3
by: Helene Day | last post by:
I am trying to access the Word Objects from a .NET project. I have some sample from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/WordObject.asp and I...
12
by: Bigdakine | last post by:
I don't know if this is the right forum for this, and if not please suggest one which fits. I have to call a fortran sub routine from a C main program. The fortran subroutine statement is ...
3
by: Charleees | last post by:
Hi all, I need C# code for Implementing MD5 Algorithm.. Hope all would have heard of MD5 Algorith... Does any one have the C# coding for that Algorithm.. please Send... ITs URgent..... Thanks...
2
by: Marc | last post by:
Hi, I am writing a program which invloves dragging and dropping things around a form. I have so far chosen to use Buttons to drag and drop which works fine. These button actually represent...
2
by: lovesehuang | last post by:
need some help for source codes about Keep Data Secret Management System baseed on C/S Architecture.The other thnics we need is SQL,C++,VC++,can u tell me where can i get them or download them.it's...
5
by: Karthik D V | last post by:
Hello All, I have a table like this ID CHARACTER ----------- --------- 1 A 2 A 3 B 4 B
2
by: tomamil | last post by:
does anyone has an idea how to do it?
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...
6
by: emre esirik(hacettepe computer science and enginee | last post by:
I need to function for polynom device, there are 2 polynom and its power max=5, and polynom array is integer I need to function which device two polynom , (two polynom device with not remaining
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.