0

Here is the situation. We have a very old COBOL exe that will only run on Windows XP. In order to run this exe we have set up a virtual XP machine. Each week the user runs a series of BAT files.

I'm trying to write a C# Windows app that will run on the VM and then execute the COBOL exe. The plan is to use Process.Start to call the EXE. Everything works fine IF MY WINDOWS C# EXE runs in the same directory as the COBOL exe.

When I move the C# exe out of the folder, I get a bad return code and the cobol does not get executed.

Without going into a lot of detail, I want to be able to run the COBOL from outside of the folder. The reason is that we have several regional offices and each one has its own folder and its own copy of the cobol exe inside of each folder. The goal here is to let the user pick each regional office and then the C# program will execute each program in each directory.

In summary. If I run C# inside the folder where the COBOL exe lives, IT WORKS. BUT if I move C# outside of that folder it DOES NOT

I hope all this makes sense. Here are some additional details:

  1. Created in Visual Studio 2012 as a C# Windows Application
  2. Platform target x86 (I've also used Any CPU)
  3. Target framework .NET Framework 4
  4. COBOL is Microfocus from 1997 (yes I know. We need to get rid of it)
  5. VM is Windows XP
  6. If I change the exe to Notepad it runs fine
  7. If I do start run from windows and paste this: K:\AMSapp\MYTESTFOLDER\AMSBLD.exe it runs fine.

  8. C# code for button click event (note: if I change the filename to this K:\AMSapp\MYTESTFOLDER\AMSBLD.exe i get the same results. It does not run ) Also the message box is showing 255 for a return code when it does not work. If the C# program is moved inside of the cobol folder it returns a zero.

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
    
            int exitCode;
    
    
                Process process = new Process();
                process.StartInfo.FileName = @"\\SERVER23021\PRODDATA\AMSapp\MYTESTFOLDER\AMSBLD.exe";
    
    
    
                process.StartInfo.ErrorDialog = true;
    
                process.Start();
                process.WaitForExit(1000 * 60 * 5);    // Wait up to five minutes.
    
                exitCode = process.ExitCode;
                MessageBox.Show("My exit code = " + exitCode.ToString());
    
                MessageBox.Show("my path and file name: " + process.StartInfo.FileName);
    
    
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
    
        }
    }
    
stuartd
  • 70,509
  • 14
  • 132
  • 163
  • 1
    Try `process.StartInfo.WorkingDirectory = Path.GetFullPath(process.StartInfo.FileName);` – L.B Oct 11 '16 at 19:52
  • Sounds more like you may have an execution permission issue. I would look at what account you are running these under and if that account is allowed to execute both applications. Also, if you are crossing any time of boundaries you may need to look at impersonation when making the call. – Kirby Oct 11 '16 at 20:33
  • >>>THIS WORKED. At first it didn't using the full network path. I changed it to K:\ then the file path. Also I instead of using Path.GetFullPath(process.StartInfo.FileName) I just hard coded the K:\ path minus the actual file name and it worked. Thanks so much. – P. Mennetti Oct 12 '16 at 14:28

2 Answers2

-1

Try setting the process working directory to the same path of the cobol exe file.

process.StartInfo.WorkingDirectory = System.IO.Path.GetFullPath(process.StartInfo.FileName);
aperezfals
  • 1,341
  • 1
  • 10
  • 26
  • My comment was just to give a hint to OP. But is not a correct answer. What about if this code is called before setting `process.StartInfo.FileName`? Syntatically correct but logically wrong.. So don't copy someone else's codes, Write a correct one by yourself.. – L.B Oct 11 '16 at 21:03
-2

I think, you should use ProcessStartInfo.Domain to set the domain and after that path to exe within ProcessStartInfo.FileName. Instance of ProcessStartInfo pass into Process.Start method

ProcessStartInfo psi = new ProcessStartInfo();
psi.Domain = @"SERVER23021";
psi.FileName = @"\PRODDATA\AMSapp\MYTESTFOLDER\AMSBLD.exe";

Process.Start(psi);

If you need to personate your calling you can set Username and Password. But you must remember that you need to provide them as a pair. UseShellExecute must be set to false and WorkingDirectory must be also provided. More: https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.password(v=vs.110).aspx

Artur Siwiak
  • 352
  • 3
  • 14
  • What should be set to `ProcessStartInfo.Domain`? Why do you think it should solve OP's problem? Why do you describe it instead of posting a few lines of compilable/working code ? – L.B Oct 11 '16 at 21:59
  • @L.B: Look at yours comment first. No reason why it might help. No any tip what it does. If you down voted please don't be such a hypocrite. I didn't paste any code because it is in documentation with explain. I gave exacly what class should be used, what property should be used of this class and where to pass instance of that class – Artur Siwiak Oct 11 '16 at 22:28
  • `Look at yours comment first` Yes, It is **comment**, not an answer. If you post something as answer, it should be an *answer* ... – L.B Oct 12 '16 at 16:42
  • I was unable to comment until now. I was able to do so only in my response. You should keep that in mind that new users cannot comment everything – Artur Siwiak Oct 13 '16 at 14:38