• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 636
  • Last Modified:

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?
0
NeillyC
Asked:
NeillyC
  • 5
  • 4
  • 2
1 Solution
 
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++
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could try the "ConsoleHaker" project posted by METALHEAD1992 here:
http://www.3dbuzz.com/vbforum/showthread.php?t=127871&page=2
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.

=(
0
 
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?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
 
Bob LearnedCommented:
What version of .NET are you using?

Bob
0
 
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?
0
 
NeillyCAuthor Commented:
I investigated that one, i'm using .net 1.1

however i believe we can upgrade to .net 2 without any trouble
0
 
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now