Scraping Output from a console application


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Scraping Output from a console application

  1. #1
    Christopher Lutz Guest

    Scraping Output from a console application


    Basically my goal in an code experiment that I am messing around with would
    to be to extract the output of a text application running in a dos window
    using the windows API. I already have something working using the DOS > redirection
    operator, but I think the most "elegant" solution would be to use the windows
    API and not have to create an intermediate file to capture the output.

    After going through the various white papers, code snippets, and L.J. Johnson's
    vb console application (VBPJ March 1998), I can dynamically create a console
    window, write text out to it and receive user key input (using the AllocConsole(),
    WriteConsole(), etc... APIs ). However I can not figure out how to "scrape"
    static text outputed by a dos application in this console window.

    If anyone knows of an API, or has done this and give me a quick hint. (I'm
    not looking for a complete solution, I just need a quick hint to point me
    in the right direction so I can puzzle it out myself and learn something
    in the process)

    Thanks in advance for any Input....

    Chris Lutz

  2. #2
    L.J. Johnson Guest

    Re: Scraping Output from a console application

    Christopher,

    > After going through the various white papers, code snippets, and L.J.

    Johnson's
    > vb console application (VBPJ March 1998), I can dynamically create a

    console
    > window, write text out to it and receive user key input (using the

    AllocConsole(),
    > WriteConsole(), etc... APIs ). However I can not figure out how to

    "scrape"
    > static text outputed by a dos application in this console window.



    > If anyone knows of an API, or has done this and give me a quick hint. (I'm
    > not looking for a complete solution, I just need a quick hint to point me
    > in the right direction so I can puzzle it out myself and learn something
    > in the process)


    I haven't done this, but according to MS documentation, you would need to
    use the CreateFile API to get a handle of the console's active screen buffer
    (it can have many buffers, but the active screen buffer is the one you see
    on the screen). The screen buffer has its own 2-dimmed array of character
    info records, and each record is stored in a CHAR_INFO structure (which
    consists of two items -- see below). So, you should be able to iterate thru
    the records and reconstruct the screen, particularly if you use the
    GetConsoleScreenBufferInfo call to figure out the "size" of the console (#
    lines, #columns, etc).

    Char
    Specifies either the Unicode (wide-character) or ANSI character of a screen
    buffer character cell, depending on whether the Unicode or ANSI version of
    the function is used.
    Attributes
    Specifies the foreground (text) and background colors in which a screen
    buffer character cell is drawn. This member can be zero, or it can be any
    combination of the following values:
    BACKGROUND_BLUE
    FOREGROUND_BLUE
    BACKGROUND_GREEN
    FOREGROUND_GREEN
    BACKGROUND_RED
    FOREGROUND_RED
    BACKGROUND_INTENSITY
    FOREGROUND_INTENSITY

    --
    L.J. Johnson, Slightly Tilted Software
    Microsoft MVP (Visual Basic)
    LJJohnsn@Flash.Net or LJJohnson@mvps.org
    <http://www.flash.net/~ljjohnsn>
    Ask The NT Pro at <http://www.inquiry.com>




  3. #3
    L.J. Johnson Guest

    Re: Scraping Output from a console application

    Christopher,

    > After going through the various white papers, code snippets, and L.J.

    Johnson's
    > vb console application (VBPJ March 1998), I can dynamically create a

    console
    > window, write text out to it and receive user key input (using the

    AllocConsole(),
    > WriteConsole(), etc... APIs ). However I can not figure out how to

    "scrape"
    > static text outputed by a dos application in this console window.



    > If anyone knows of an API, or has done this and give me a quick hint. (I'm
    > not looking for a complete solution, I just need a quick hint to point me
    > in the right direction so I can puzzle it out myself and learn something
    > in the process)


    I haven't done this, but according to MS documentation, you would need to
    use the CreateFile API to get a handle of the console's active screen buffer
    (it can have many buffers, but the active screen buffer is the one you see
    on the screen). The screen buffer has its own 2-dimmed array of character
    info records, and each record is stored in a CHAR_INFO structure (which
    consists of two items -- see below). So, you should be able to iterate thru
    the records and reconstruct the screen, particularly if you use the
    GetConsoleScreenBufferInfo call to figure out the "size" of the console (#
    lines, #columns, etc).

    Char
    Specifies either the Unicode (wide-character) or ANSI character of a screen
    buffer character cell, depending on whether the Unicode or ANSI version of
    the function is used.
    Attributes
    Specifies the foreground (text) and background colors in which a screen
    buffer character cell is drawn. This member can be zero, or it can be any
    combination of the following values:
    BACKGROUND_BLUE
    FOREGROUND_BLUE
    BACKGROUND_GREEN
    FOREGROUND_GREEN
    BACKGROUND_RED
    FOREGROUND_RED
    BACKGROUND_INTENSITY
    FOREGROUND_INTENSITY

    --
    L.J. Johnson, Slightly Tilted Software
    Microsoft MVP (Visual Basic)
    LJJohnsn@Flash.Net or LJJohnson@mvps.org
    <http://www.flash.net/~ljjohnsn>
    Ask The NT Pro at <http://www.inquiry.com>




  4. #4
    Mattias Sjögren Guest

    Re: Scraping Output from a console application

    Christopher,

    >If anyone knows of an API, or has done this and give me a quick hint. (I'm
    >not looking for a complete solution, I just need a quick hint to point me
    >in the right direction so I can puzzle it out myself and learn something
    >in the process)


    I have a sample of how to redirect the output from a console app into
    a VB string at my site. If it's too much of a "complete solution" for
    you, you could start looking at the CreatePipe API, and the
    STARTUPINFO structure used in a call to CreateProcess.


    Mattias

    ____________________________________________
    Mattias Sjögren - mattiass @ hem.passagen.se
    http://hem.spray.se/mattias.sjogren/
    Docendo discimus

  5. #5
    Mattias Sjögren Guest

    Re: Scraping Output from a console application

    Christopher,

    >If anyone knows of an API, or has done this and give me a quick hint. (I'm
    >not looking for a complete solution, I just need a quick hint to point me
    >in the right direction so I can puzzle it out myself and learn something
    >in the process)


    I have a sample of how to redirect the output from a console app into
    a VB string at my site. If it's too much of a "complete solution" for
    you, you could start looking at the CreatePipe API, and the
    STARTUPINFO structure used in a call to CreateProcess.


    Mattias

    ____________________________________________
    Mattias Sjögren - mattiass @ hem.passagen.se
    http://hem.spray.se/mattias.sjogren/
    Docendo discimus

  6. #6
    L.J. Johnson Guest

    Re: Scraping Output from a console application

    Mattias,

    > I have a sample of how to redirect the output from a console app into
    > a VB string at my site.


    Cool.

    --
    L.J. Johnson, Slightly Tilted Software
    Microsoft MVP (Visual Basic)
    LJJohnsn@Flash.Net or LJJohnson@mvps.org
    <http://www.flash.net/~ljjohnsn>
    Ask The NT Pro at <http://www.inquiry.com>




  7. #7
    L.J. Johnson Guest

    Re: Scraping Output from a console application

    Mattias,

    > I have a sample of how to redirect the output from a console app into
    > a VB string at my site.


    Cool.

    --
    L.J. Johnson, Slightly Tilted Software
    Microsoft MVP (Visual Basic)
    LJJohnsn@Flash.Net or LJJohnson@mvps.org
    <http://www.flash.net/~ljjohnsn>
    Ask The NT Pro at <http://www.inquiry.com>




  8. #8
    Christopher Lutz Guest

    Re: Scraping Output from a console application


    "L.J. Johnson" <ljjohnsn@flash.net> wrote:
    >Mattias,
    >
    >> I have a sample of how to redirect the output from a console app into
    >> a VB string at my site.

    >
    >Cool.
    >
    >--
    >L.J. Johnson, Slightly Tilted Software
    >Microsoft MVP (Visual Basic)
    >LJJohnsn@Flash.Net or LJJohnson@mvps.org
    ><http://www.flash.net/~ljjohnsn>
    >Ask The NT Pro at <http://www.inquiry.com>
    >
    >
    >



    Thanks for all your help!




  9. #9
    Christopher Lutz Guest

    Re: Scraping Output from a console application


    "L.J. Johnson" <ljjohnsn@flash.net> wrote:
    >Mattias,
    >
    >> I have a sample of how to redirect the output from a console app into
    >> a VB string at my site.

    >
    >Cool.
    >
    >--
    >L.J. Johnson, Slightly Tilted Software
    >Microsoft MVP (Visual Basic)
    >LJJohnsn@Flash.Net or LJJohnson@mvps.org
    ><http://www.flash.net/~ljjohnsn>
    >Ask The NT Pro at <http://www.inquiry.com>
    >
    >
    >



    Thanks for all your help!




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center