Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2007

Tip: Looking for answers? Try searching our database.

Need to create a wrapper to execute a program

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tony Freixas - 14 May 2007 04:37 GMT
Hello,

I'm trying to create a wrapper for a program. I want to execute
program 'X' by running program 'Y', such that 'Y' appears to function
pretty much like 'X' both in the way command line options are handled
and the way input, output and error messages are reported. Program 'X'
is a console application.

I had it pretty well figured out, I thought, until I ran into a case
where program X spit out ZIP code. In other words, I was reading
Process.StandardOutput.ReadLine() and writing to Console.WriteLine().
These don't handle binary output very well.

So I can read X's output using
Process.StandardOutput.BaseStream.ReadByte() but I can't find the
right method to write binary to Y's standard output. How do I convert
Console.Out to a StreamWriter? I want this to work efficiently, too.

Here's the program so far. Thanks for any suggestions.

using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;

namespace TigerHeron.CoreLib
{
   public class ProgramExecute
   {
    public const int NONE = 0;
    public const int REDIRECT_IO = 1;
    private Process process;

    public ProgramExecute(string[] args) : this(args, NONE)
    {
    }

    public ProgramExecute(string[] args, int flags)
    {
       if (args.Length < 1) {
        throw new ArgumentException("The array must have at least one
argument", "args");
       }

       process = new Process();
       String program = args[0];

        string newArgs = "";
        for (int i = 1; i < args.Length; i++) {
           newArgs += "\"" + args[i] + "\" ";
        }
        process.StartInfo.FileName = program;
        process.StartInfo.Arguments = newArgs;

       if ((flags & REDIRECT_IO) != 0) {
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.RedirectStandardOutput = true;
       }

       process.StartInfo.Verb = "Open";
       process.StartInfo.UseShellExecute = (flags & REDIRECT_IO) == 0;
       process.StartInfo.ErrorDialog = (flags & REDIRECT_IO) == 0;
       process.StartInfo.CreateNoWindow = true;
       process.Start();

       if ((flags & REDIRECT_IO) != 0) {
        new Thread(new ThreadStart(this.readStdin)).Start();
        new Thread(new ThreadStart(this.readStdout)).Start();
        new Thread(new ThreadStart(this.readStderr)).Start();
        process.WaitForExit();
        Environment.Exit(process.ExitCode);
       }
    }

    private void readStdin()
    {
       try {
        string line;
        while ((line = Console.In.ReadLine()) != null) {
           process.StandardInput.WriteLine(line);
        }
       }
       catch (Exception e) {
        Console.Error.WriteLine(e.Message);
       }
    }

    private void readStdout()
    {
       try {
        int b;
        while ((b = process.StandardOutput.BaseStream.ReadByte()) != -1) {
           Console.Write((char)b);           // This doesn't work
        }
       }
       catch (Exception e) {
        Console.Error.WriteLine(e.Message);
       }
    }

    private void readStderr()
    {
       try {
        string line;
        while ((line = process.StandardError.ReadLine()) != null) {
           Console.Error.WriteLine(line);
        }
       }
       catch (Exception e) {
        Console.Error.WriteLine(e.Message);
       }
    }
   }
}
Moty Michaely - 14 May 2007 06:07 GMT
> Hello,
>
[quoted text clipped - 114 lines]
>
> }

Dear Tony,

stdout/stdin can't output or input binary data. It's purpose is to
output/input charecters, depending on encoding settings.

Using BaseStream is not a good way since the StandardOutput is a
wrapper to the base stream. You can't be sure the wrapper doe's
exactly what the base stream does.

If you need to redirect binary data, you should use other methods to
achieve this purpose.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.