Getting Console Window Text

Hey Guys

I'm trying to run a dos business app in a console window and capture the screen display that it presents to the user.
Ultimately i'd like to have the contents of the screen inside a string[] or whatever, but i'm not quite sure how i can get this happening.
I'm launching a cmd window, and getting the handle to it just fine, but from there i'm not sure how to get the screen.
It's a graphical app, so it should work for edit.com for example.

Any ideas?
NeillyCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NeillyCAuthor Commented:
Idle thanks for posting but your links are for standard input/output, this needs to be for graphical interface, tests i tried could get these to work.

It seems the answer is here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/reading_and_writing_blocks_of_characters_and_attributes.asp

However it's in c++, and i cannot seem to convert it sucessfully to c#

Sorry this has kind of turned in to a conversion question, but it's hard to do this kind of stuff when all the samples around are in c++
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You could try the "ConsoleHaker" project posted by METALHEAD1992 here:
http://www.3dbuzz.com/vbforum/showthread.php?t=127871&page=2
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

NeillyCAuthor Commented:
hehe yeah, seen that, but it seems the attach console function only works in XP. is this the case?  I really need it to work on our win2k systems also
On 2k it throws an exception:
Unable to find an entry point named 'AttachConsole' in DLL 'Kernel32.dll'.

Here is the import statement
[DllImport("Kernel32.dll")]
public static extern Boolean AttachConsole(IntPtr dwProcessId);

I've tried rebuilding the dll without the attachconsole reference, but all i seem to be able to return is 0s, so it seems important. Have you heard of anyone who has got this to work on 2k?

Thanks for your work so far idle mind
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Apparently it is a very new API:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/attachconsole.asp

At the bottom in the requirements section:
Client Requires Windows Vista or Windows XP.
Server Requires Windows Server "Longhorn" or Windows Server 2003.

=(
NeillyCAuthor Commented:
I tried to convert the msdn sample above from c++ to c# because it doesn't require attachconsole, but no go yet, what do you think?

Does anyone else know how i could get this working?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
I've never tried to grab the contents from this type of console window.  I'm not even sure I have any applications on my system that do this type of window that I could test with.  I would have to write one from scratch just so can screen scrape it...lol.
Bob LearnedCommented:
What version of .NET are you using?

Bob
NeillyCAuthor Commented:
Idle, a good example would be "cmd /c edit"!

Actually i don't know that bob, i've never thought to look cos i haven't needed it, and i don't know how to check.

I'm using a pretty fresh install of vs 2003, so what comes standard with that?

What are you thinking of anyway?
NeillyCAuthor Commented:
I investigated that one, i'm using .net 1.1

however i believe we can upgrade to .net 2 without any trouble
Bob LearnedCommented:
Here is a sample Console application, using ReadConsoleOutput API call:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class ReadConsole
{

  private enum STDHANDLES
  {
    STDINPUTHANDLE = -10,
    STDOUTPUTHANDLE = -11,
    STDERRORHANDLE = -12
  }

  [StructLayout(LayoutKind.Explicit, CharSet=CharSet.Auto)]
  private struct CHARTYPE
  {
    [FieldOffset(0)] public short UnicodeChar;
    [FieldOffset(0)] public byte AsciiChar;
  }

  [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
  private struct CHARINFO
  {
    public CHARTYPE Char;
    public short Attributes;
  }

  [StructLayout(LayoutKind.Sequential)]
  private struct COORD
  {
    public short X;
    public short Y;
    public COORD(short x, short y)
    {
      this.X = x;
      this.Y = y;
    }
  }

  [StructLayout(LayoutKind.Sequential)]
  private struct SMALLRECT
  {
    public short Left;
    public short Top;
    public short Right;
    public short Bottom;
 
    public SMALLRECT(short left, short top, short right, short bottom)
    {
      this.Left = left;
      this.Top = top;
      this.Right = right;
      this.Bottom = bottom;
    }
  }

  [DllImport("Kernel32.dll")]
  private static extern IntPtr GetStdHandle (STDHANDLES nStdHandle);

  [DllImport("Kernel32.dll")]
  private static extern bool ReadConsoleOutput
    (IntPtr hConsoleOutput, ref CHARINFO lpBuffer, COORD dwBufferSize,
    COORD dwBufferCoord, ref SMALLRECT lpReadRegion);
                                 
  [STAThread]
  static void Main(string[] args)
  {
    Console.WriteLine("*-*-*-*");
    Console.WriteLine("*-*-*-*");
    Console.WriteLine("*-*-*-*");
    Console.WriteLine("*-*-*-*");

    IntPtr hConsoleOutput= GetStdHandle(STDHANDLES.STDOUTPUTHANDLE);
    CHARINFO[] buffer = new CHARINFO[28];
    COORD bufferSize = new COORD(7, 4);
    COORD bufferCoord = new COORD(0, 0);
    SMALLRECT readRegion = new SMALLRECT(0, 0, 6, 3);

    Console.WriteLine(ReadConsoleOutput(hConsoleOutput, ref buffer[0],
      bufferSize, bufferCoord, ref readRegion));

    for (int i = 0; i <= 27; i++)
    {
      Console.Write((char)buffer[i].Char.UnicodeChar);
    }
    Console.ReadLine();
  }
}

Bob

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.