Link to home
Start Free TrialLog in
Avatar of riceman0
riceman0

asked on

Carriage return if run from VB.NET??


Okay, this is a tough one.  I have an old EXE (compiled from FORTRAN) that dumps binary data to a file.  When I run it from a DOS window, it does not put carriage returns after every WRITE statement.  When I run it from my VB.NET program (see code below), it DOES put carriage returns after every statement!!  (This observation is confirmed, wrote a second program to read the binary data with and w/o CRs, and it depends on whether I start it from a DOS window or with the code below.)

This is shocking!  There is something very strange going on.  It's the same compiled EXE, does the VB.NET direct towards a different write DLL or something???  The FORTRAN WRITE commands, by the way, are simple unformatted binary to an open file.

Would MUCH appreciate any ideas as to how this could be happening...  

*****



        Private Sub Go(ByVal bRestart As Boolean)

            Dim t As New Threading.Thread(AddressOf ThreadRun)
            t.Start()

        End Sub


        Private Sub ThreadRun()

            Try

                p.StartInfo.FileName = My.Application.Info.DirectoryPath + "\" + ENGINE_FILE
                p.StartInfo.Arguments = " " + m_Project.INPFile.FileName
                p.StartInfo.UseShellExecute = False
                p.StartInfo.WorkingDirectory = m_Project.ProjectPath
                p.StartInfo.RedirectStandardOutput = True
                p.StartInfo.CreateNoWindow = True
                p.StartInfo.RedirectStandardError = True

                p.Start()
                p.WaitForExit()

                m_LastExitCode = p.ExitCode

                RaiseEvent OnEngineDone()

            Catch ex As Exception

                RaiseEvent OnError(ex.Message, "cEngine.ThreadRun")

            End Try

        End Sub
Avatar of Jose Parrot
Jose Parrot
Flag of Brazil image

Hi,

This is not strange, this is Microsoft making things easy to program and difficult to integrate or migrate to other platform. One day, young programmers will find it as a FORTRAN issue, not a Microsoft decision on what is good or not for us. In few years, I think even C++ (the last allowed non-managed Microsoft's programming environment) will disappear, substituted by the slow C#.

About the question, the redirection to the console is made on a line-by-line basis, so the result isn't a surprise at all.

You can use the shell method to avoid such undesirable behavoir.
For example:

' Second argument set to 1 to open the application window normally, with focus
Dim RetVal
Dim MyApp As String
MyApp = My.Application.Info.DirectoryPath + "\" + ENGINE_FILE + _
             " " + m_Project.INPFile.FileName
RetVal = Shell(MyApp, 1)

Jose
Avatar of riceman0
riceman0

ASKER


I will try this as soon as possible -- and if it works I will thank you and ask you more about what the heck happened --  but first can I get the same effect by changing to:

p.StartInfo.UseShellExecute = True


Bad news, using a Shell command from my VB.NET program did not resolve the difference in behavior, still get a CRLF from VB.NET, no CRLF from a DOS window.

Still, I didn't quite follow your explanation, what do you think is going on?  How does this discrepancy make sense?  I would think the machine code that corresponds to a WRITE command is buried in my FORTRAN EXE, and I don't see how .NET could change how it operates... !

Hi,

Looks like the mistery is clearing...

FORTRAN appends a NewLine at the end of each WRITE command, as its terminator. Different to C programs of the DOS era, as stated at
http://www.tat.physik.uni-tuebingen.de/~kley/lehre/ftn77/tutorial/format.html

DOS waits for both CR and LF (Carriage Return and Line Feed) to go to begin of next line. So, both C and FORTRAN don't start a new line when find a CR.

VB.NET appends its own CR/LF when find a NewLine in the FORTRAN program, as a result of its own environment.

Note that using just a carriage return (Chr(13)) would cause two lines to be overwritten on the screen, since carriage return doesn't advance to a new line. You can use the Environment.NewLine property if you want both a carriage return and a line feed:

"First line" & Chr(10) & "Second line" ' LF only
"First line" & Environment.NewLine & "Second line" ' CR/LF

That is, internally, VB.NET uses Environment.NewLine.
Take a look at
http://msdn2.microsoft.com/en-us/library/system.environment.newline.aspx

Jose

That rings true, but is there any solution??  I need this EXE to run the same way from DOS and from my .NET process.  That NewLine is readonly, is there no way to tweak the environment of my process?

ASKER CERTIFIED SOLUTION
Avatar of Jose Parrot
Jose Parrot
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Well, the program output is to files explicitly opened and closed by the FORTRAN program itself, I'm guessing that's not affected by RedirectStandardOutput?

Also, for binary output I won't even be able to tell the excedent CRLFs (i.e., CHR(10) and (13)) from the true data byte=10 and byte=13.  So I'll corrupt my data if I search for and remove them, correct?  (The record size for some files is not uniform.)

How amazingly frustrating.  This is absolutely friggin' absurd.