Results 1 to 10 of 10
  1. #1
    2 Star Lounger
    Join Date
    Nov 2004
    Location
    Lexington Park, Maryland, USA
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Inconsistent results running DLL's with VB (VB & Fortran)

    We have a DLL written in Fortran 77 that does nothing more than simply read data from files and data to other files. All variables are static and initialized. We are not passing any variables to the DLL or out of the DLL. We are are not doing any pointer arithmetic. The problem is that when this parameterless subroutine is called from VB instead of Fortran or C the results are different. We do not see how this can happen. We checked to make sure that there is only one copy of the DLL and its related files on the computer. The exact same results occur on multiple computers. Is there any possible way that the same binary code could behave differently? Any suggestions are appreciated. I am asking this question for a colleague.

    Mike

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Inconsistent results running DLL's with VB (VB & Fortran)

    Hi Mike,
    Can we assume that all the steps in Q123841 have been followed? If so, what exactly do you mean by the statement "All variables are static and initialized" given that no variables are being passed to or from the dll? Also, when you say you get different results, do you mean that different data gets written to the files in question or something else? (I assume you are not getting any error messages).
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    2 Star Lounger
    Join Date
    Nov 2004
    Location
    Lexington Park, Maryland, USA
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Inconsistent results running DLL's with VB (VB & Fortran)

    The steps in Q123841 apply to Microsoft Fortran, and we were using Digital Visual Fortran 5.0, so they cannot be followed. Also, the guidelines about Visual Basic compatible data types only apply to parameters and return values, which we are not using. We are exporting the parameterless subroutine with the stdcall calling convention (that VB requires), which should be all that is needed for VB compatibility? When I say all variables are static, I am talking about local variables. The reason is that this program was originally written with all local variables static. I do not want to change that because static variables are initialized whereas automatic variables are not, which will produce unpredictable results. When I say we are getting different results, I mean that the output file contains different values. We are not getting any errors; if I did not notice that the values are different, I would have thought it was working fine. Additionally, we made sure all common blocks are defined exactly once in the program, and we enabled the subscript range checking, which produced no errors.

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Inconsistent results running DLL's with VB (VB & Fortran)

    So you have something of the format:
    SUBROUTINE FortranCall
    !DEC$ ATTRIBUTES DLLEXPORT :: FortranCall
    !DEC$ ATTRIBUTES ALIAS : "FortranCall" :: FortranCall

    and you have then declared this as something like:
    Declare Sub FortranCall Lib "Pathdllname.dll"

    Are the input files correct? (I assume you've already checked that) Additionally, is it possible that, if you are making multiple calls to the dll, the call does not wait for the dll method to finish so, in effect, the files are being partly overwritten?
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    2 Star Lounger
    Join Date
    Nov 2004
    Location
    Lexington Park, Maryland, USA
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Inconsistent results running DLL's with VB (VB & Fortran)

    Actually, we exported the function using a .def file, and imported it into VB with a declare statement as you said. The dll is only called once, and the file is only opened once, so there is no possibility of being overwritten. (If it was being overwritten, wouldn't it also happen in C as well?) The files are already there before program execution, and the driver program does not touch the files. As a procedure call, control is not returned to the caller (VB or C) until it is done. Personally, I believe there is something that VB is doing that makes the same binary code behave differently.

  6. #6
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Inconsistent results running DLL's with VB (VB & Fortran)

    I have a vague recollection of reading something in an article about Delphi, which said that if you are creating Delphi front ends using Fortran dlls, there may be an issue with IO operations not necessarily being modal, hence my question. I've never used Fortran myself, so don't know much about creating dlls for use with VB, but I don't really see how VB can affect the results of a Fortran procedure when there is no information being passed between the two: either the procedure call should run or it should fail; it should not run differently!.
    You may want to have a look at these two articles to see if they shed any light:
    http://www.canaimasoft.com/f90VB/OnlineMan...anual/TH_60.htm
    http://h21007.www2.hp.com/dspp/tech/tech_T...01,6949,00.html

    or otherwise I would suggest a Fortran forum may be more likely to yield an answer. Sorry I can't be of more use.
    Regards,
    Rory

    Microsoft MVP - Excel

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Inconsistent results running DLL's with VB (VB

    It isn't logical, is it, unless something inside the DLL presumes something about its environment that is different when called from VB than from C or Fortran.

    Can you detect any pattern to the differences?

    Can you compile the DLL down to an EXE and use VB's Shell command?

  8. #8
    2 Star Lounger
    Join Date
    Nov 2004
    Location
    Lexington Park, Maryland, USA
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Inconsistent results running DLL's with VB (VB

    I do not see how it could be different; VB and Fortran use different runtimes which use the Windows API. As far as I know, the API doesn't care whether the code is in a dll or exe; it will give you a modal file read any time you ask for one? There is no apparent pattern (to me, at least) in the different results. Every time we call the dll from VB, the output files contains the value of .27837. Every time we call the dll from C or fortran, we get .23278. Also, we tried running it from Excel (VBA), and it gave us a value of -.09476(!) We get these same values even on different machines. As for shelling it out of VB, I will try it just to see, but the point is to pass values to and from VB and C through memory. At first, I thought the reason for the different results were from improper parameter passing, which is why I made it so that it uses its own values. I'll keep you posted on what happens with the shelling.

    We are also getting a linker warning: "LNK4084: total image size 372797440 exceeds max (268435456); image may not run." This program cannot be that big; the file is only 602k, and Windows lists it as <500k executable code and ~6megs of data, most of which hasn't been written to.

    Mike

  9. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Inconsistent results running DLL's with VB (VB

    Try asking in comp.lang.fortran newsgroup.

  10. #10
    2 Star Lounger
    Join Date
    Nov 2004
    Location
    Lexington Park, Maryland, USA
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Inconsistent results running DLL's with VB (VB

    We finally solved the problem. What happened is the floating point control of windows is set by each language according to its own set of parameters. This affects how the math processor rounds numbers and other various operations. C and Fortran sets the floating point parameters equally, VB and VBA each have their own set of parameters. We had to write a bit of Assembly code to force C, Fortran, VB, and VBA to use the same parameters for the floating point control. Now all four languages now give the same answers. This was a doosie that took 4 days to solve.

    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •