Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Getting Console Window Text

Posted on 2006-04-17
13
Medium Priority
?
603 Views
Last Modified: 2012-02-03
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
Comment
Question by:NeillyC
  • 5
  • 4
  • 2
11 Comments
 

Author Comment

by:NeillyC
ID: 16475493
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16478021
You could try the "ConsoleHaker" project posted by METALHEAD1992 here:
http://www.3dbuzz.com/vbforum/showthread.php?t=127871&page=2
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:NeillyC
ID: 16483538
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16483858
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
 

Author Comment

by:NeillyC
ID: 16484806
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16487905
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16490291
What version of .NET are you using?

Bob
0
 

Author Comment

by:NeillyC
ID: 16493586
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
 

Author Comment

by:NeillyC
ID: 16494181
I investigated that one, i'm using .net 1.1

however i believe we can upgrade to .net 2 without any trouble
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 16506990
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question