Absent Member.
Absent Member.
1512 views

Unable to capture console screen

Hi , 

Like how we can drag and select in putty , I am unable to capture the console screen output ( created using screen section ) using select all from the mouse click.

Please advise if any suggestions or alternative to capture the contents of screen in Visual COBOL .Net Environment ( Managed Code).

Regards,

Zoeb

0 Likes
4 Replies
Micro Focus Expert
Micro Focus Expert

You can use the standard Windows Print Screen key to copy the desktop image to the clipboard or if you use Alt-Print Screen it will copy the current active window instead. You can also use the Windows Snipping tool to mark a partial area of the screen to copy or there are countless other 3rd party tools to do the same.

Is this what you are looking for?

0 Likes
Absent Member.
Absent Member.

No , I want to capture/read the context of the screen for Automation validations.

0 Likes
Absent Member.
Absent Member.

We are trying to read the console output of the desired screen . Is there a way to read the console screen output through c# programatically .  

0 Likes
Micro Focus Expert
Micro Focus Expert

I found one example of how to read the console buffer programmatically in C# that also appears to work for COBOL. In this example I left the C# piece in C# instead of translating it to COBOL. I believe there are other ways of doing this as well but this seemed to work OK.

COBOL Console application:

       program-id. Program1 as "readconsolebuffer.Program1".
       data division.
       working-storage section.
       01 field-1  pic x(10) value "test".
       01 field-2  pic x(10) value "chris".
       screen section.
       01 myscreen blank screen.
          05       pic x(5) value "Name:" line 5 column 1.
          05       pic x(10) using field-1 line 5 column 8.   
          05       pic x(8) value "Company:" line 7 column 1.
          05       pic x(10) using field-2 line 7 column 10.   
       procedure division.
       
           display myscreen
           accept myscreen
         *> read 10 lines from the top of the console buffer
           perform varying myline as string thru type csReadBuffer.ConsoleReader::ReadFromBuffer(0, 0, type Console::BufferWidth as binary-short, 10)
              display myline
           end-perform
        
           goback.
           
       end program Program1.



C# utility class:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace csReadBuffer
{
    public class ConsoleReader
    {
        public static IEnumerable ReadFromBuffer(short x, short y, short width, short height)
        {
            IntPtr buffer = Marshal.AllocHGlobal(width * height * Marshal.SizeOf(typeof(CHAR_INFO)));
            if (buffer == null)
                throw new OutOfMemoryException();

            try
            {
                COORD coord = new COORD();
                SMALL_RECT rc = new SMALL_RECT();
                rc.Left = x;
                rc.Top = y;
                rc.Right = (short)(x + width - 1);
                rc.Bottom = (short)(y + height - 1);

                COORD size = new COORD();
                size.X = width;
                size.Y = height;

                const int STD_OUTPUT_HANDLE = -11;
                if (!ReadConsoleOutput(GetStdHandle(STD_OUTPUT_HANDLE), buffer, size, coord, ref rc))
                {
                    // 'Not enough storage is available to process this command' may be raised for buffer size > 64K (see ReadConsoleOutput doc.)
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                IntPtr ptr = buffer;
                for (int h = 0; h < height; h++)
                {
                    StringBuilder sb = new StringBuilder();
                    for (int w = 0; w < width; w++)
                    {
                        CHAR_INFO ci = (CHAR_INFO)Marshal.PtrToStructure(ptr, typeof(CHAR_INFO));
                        char[] chars = Console.OutputEncoding.GetChars(ci.charData);
                        sb.Append(chars[0]);
                        ptr += Marshal.SizeOf(typeof(CHAR_INFO));
                    }
                    yield return sb.ToString();
                }
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct CHAR_INFO
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public byte[] charData;
            public short attributes;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct COORD
        {
            public short X;
            public short Y;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct SMALL_RECT
        {
            public short Left;
            public short Top;
            public short Right;
            public short Bottom;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct CONSOLE_SCREEN_BUFFER_INFO
        {
            public COORD dwSize;
            public COORD dwCursorPosition;
            public short wAttributes;
            public SMALL_RECT srWindow;
            public COORD dwMaximumWindowSize;
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool ReadConsoleOutput(IntPtr hConsoleOutput, IntPtr lpBuffer, COORD dwBufferSize, COORD dwBufferCoord, ref SMALL_RECT lpReadRegion);

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern IntPtr GetStdHandle(int nStdHandle);
    }
}

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.