Call to Fortran EXE from VB Express 2010 not working

Answered Call to Fortran EXE from VB Express 2010 not working

  • 27. dubna 2012 19:03
     
     

    Can anyone recommend a solution to my problem - I have a button on a form that is supposed to run an executable (compiled Intel Fortran v11) on the click event. Here is the code:

    Private Sub RunButton_Click() Handles RunButton.Click

        Try

            System.Diagnostics.Process.Start("C:\testDir\testCode.exe")

        Catch ex As Exception

            MessageBox.Show("Unable to locate desired file")

        End Try

    End Sub

    I'm certain the path is correct and I have verified that running the testCode.exe from the cmd window works as it should (testCode.out file is updated). When I try to run the process via the RunButton it appears to work...no errors are reported or caught. However, the expected output file is not updated indicating testCode.exe did not execute. All of the online support I have researched indicates this should work. Please help.

Všechny reakce

  • 27. dubna 2012 19:23
    Moderátor
     
     

    calling Process.Start() is more like double clicking the exe than running it from a command prompt. If you double click the fortran exe in windows explorer, does the testCode.out file get updated there too?

    You may need to try launching the process using a ProcessStartInfo object, and messing with the properties of that, like setting UseShellExecute to False.


    Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com

  • 27. dubna 2012 23:08
     
     
    Yes, the testCode.out is generated if I simply double click the EXE. I will look into the second part of your reply over the weekend and I'll repost Monday. Thanks and have a nice weekend.

    D.R. Komar

  • 30. dubna 2012 12:55
     
     

    It sounds like you are running a DOS app. See if the below link helps:

    http://www.vb-helper.com/howto_net_run_dos.html


    Paul ~~~~ Microsoft MVP (Visual Basic)

  • 30. dubna 2012 17:44
     
      Obsahuje kód

    It is a DOS app (compiled Intel Fortran code I normally run manually from the command prompt). I've tried creating the ProcessStartInfo object and setting the properties in different combinations...this hasn't worked. Several sites, including the vb-helper site, recommend this as the solution to running s DOS EXE from VB2010. So, here is where I am although it still isn't working:

        Sub RunButton_Click() Handles RunButton.Click
    
            Dim start_info As New ProcessStartInfo("C:\testCode.exe")
            start_info.UseShellExecute = False
            start_info.CreateNoWindow = True
            start_info.RedirectStandardOutput = True
            start_info.RedirectStandardError = True
    
            ' Make the process and set its start information.
            Dim proc As New Process()
            proc.StartInfo = start_info
    
            ' Start the process.
            proc.Start()
    
        End Sub
    Since there are no input arguments or screen output I didn't use the StreamReader. Any suggestions?

    D.R. Komar

  • 30. dubna 2012 18:16
     
     Odpovědět

    I suspect that the working directory was the problem.

    Try this:

            Dim exePath As String = "C:\testDir\testCode.exe"
            Dim start_info As New ProcessStartInfo()
            With start_info
                .FileName = exePath
                .WorkingDirectory = IO.Path.GetDirectoryName(exePath)
            End With
            Process.Start(start_info)

    Note that, if you're running Win7, because of UAC you most likely won't be able to access the root drive directly (i.e "C:\testCode.exe"). In this case, you need to put the exe in a directory, i.e C:\testDir\testCode.exe as shown in the code.
    • Označen jako odpověď D.R. Komar 30. dubna 2012 18:30
    • Zrušeno označení jako odpověď D.R. Komar 30. dubna 2012 18:31
    • Označen jako odpověď D.R. Komar 30. dubna 2012 18:31
    •  
  • 30. dubna 2012 18:36
     
     

    Looked like that took care of it.

    Just so I feel like I learned something here, can you tell me WHY we needed to specify the WorkingDirectory property when we already specified the full path via the FileName property?

    Thank you, Stanav (and others).


    D.R. Komar

  • 30. dubna 2012 19:26
     
     

    The is what the doc says:

    The WorkingDirectory property behaves differently when UseShellExecute is true than when UseShellExecute is false. When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable. If WorkingDirectory is an empty string, the current directory is understood to contain the executable.

    When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. Instead, it is used by the process that is started and only has meaning within the context of the new process.

    If you are using Stanav's code then UseShellExecute defaults to True and the executable can be properly located.

    I'm not sure why the link to the code I posted wouldn't work. That is, unless the Fortran app was trying to write to a folder that was protected by UAC (not the app folder).


    Paul ~~~~ Microsoft MVP (Visual Basic)

  • 30. dubna 2012 19:46
     
     
    Thanks for the additional info re: the WorkingDirectory property. I again tried setting .UseShellExecute = False and verified .WorkingDirectory = "" and again it did not run. Several other sites suggest this should work so I am stumped. However, VB2010 is rather new to me so perhaps there is something else I haven't considered or am failing to do. In any event, it seems to be working with .UseShellExecute = True and specifying the .WorkingDirectory.

    D.R. Komar