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
- static class Fortran
- {
- [DllImport("Name.dll")]
- private static extern bool FORTRANCALLA(
- StringBuilder infilecalc,
- StringBuilder tablecalc,
- StringBuilder destination2,
- StringBuilder format,
- StringBuilder loadcond);
- public static void Calculate(string infilecalc, string tablecalc, string destination2, string format, string loadcond)
- {
- string tempOutputPath = FileMod.GetTemporaryDirectory() + "NameTemp.tmp";
- StringBuilder bldrInFileCalc = new StringBuilder(infilecalc, 80);
- StringBuilder bldrTableCalc = new StringBuilder(tablecalc, 80);
- StringBuilder bldrDestination2 = new StringBuilder(tempOutputPath, 80);
- StringBuilder bldrFormat = new StringBuilder(format, 4);
- StringBuilder bldrLoadCond = new StringBuilder(loadcond, 80);
- try
- {
- if (!FORTRANCALLA(bldrInFileCalc, bldrTableCalc, bldrDestination2, bldrFormat, bldrLoadCond))
- MessageBox.Show(new System.ComponentModel.Win32Exception().ToString());
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message);
- }
- //FORTRANCALLA(bldrInFileCalc, bldrTableCalc, bldrDestination2, bldrFormat, bldrLoadCond);
- File.Copy(tempOutputPath, destination2, true);
- File.Delete(tempOutputPath);
- }
- }
Does anyone have any ideas? Because I am officially out of ideas...
Thanks,
Nitsua