Highlighted
Absent Member.
Absent Member.
489 views

How to direct output to a Windows Form using Visual Cobol

I have a simple Cobol program that calculates prime numbers sequentially.

By default the output is directed to a DOS window.

I would like to direct the output to a Windows Form.

All of the Visual Cobol forms examples I have seen accept input from the user and display a predetermined result.

Is there a method to use the display command in Cobol to direct output to a Window form ?

Any program or project examples would be appreciated.

0 Likes
2 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

The display and accept statements are used for console I-O and there is no way that I know of to direct the output to a Windows Form. You can use a P/Invoke call to the WINAPI function AllocConsole and then you would have a console window displayed as well as your form and the output of display statements would be sent to the console.

If you add the following directive to the top of your source:
$set ilpinvoke"kernel32"

and then issue the following in your form code (perhaps in the Form_Load method)
01 mybool condition-value.
call "AllocConsole" using mybool

then DISPLAY statements will appear in the console.

The normal way to handle data in a Windows Form application is to direct the output to one of the controls such as a TextBox, Label or other text based control.

You can set the value of a textbox to a data item such as a string or PIC X field like:
01 myfield pic x(10) value "Hello".
set txtMyTextBox::Text to myfield
0 Likes
Highlighted
Micro Focus Contributor
Micro Focus Contributor

Chris and Christopher,

One way to do this is to leave the console app unchanged and write a new Windows Forms (or WPF or ASP.NET app, for that matter) with the appropriate text field you want to populate. You then create a new Systems.Diagnostics.ProcessInfo with RedirectStandardOutput = true and RedirectStandardError = true. You can then create a new Systems.Diagnostics.Process using that ProcessInfo. You then Start the process, capture the screen output using  StandardOutput.ReadToEnd and you need to wait till the process ends before trying to read it using WaitForExit. Sorry, I only have a c# sample (to execute the COBOL compiler, ironically!) that puts the console writes into the output variable:

           ProcessStartInfo startInfo = new ProcessStartInfo("cobol.exe")

                                {

                                    Arguments = arg,

                                    UseShellExecute = false,

                                    RedirectStandardOutput = true,

                                    RedirectStandardError = true,

                                    WorkingDirectory = tempDirectory,

                                    CreateNoWindow = true

                                };

           Process p = new Process { StartInfo = startInfo };

           string output = String.Empty;

           bool result = false;

           try

           {

               p.Start();

               output = p.StandardOutput.ReadToEnd();

               p.WaitForExit();

               Directory.SetCurrentDirectory(savedDir);

           }

           catch (Exception e)

           {

              do something interesting we had a process crash;

           }

           if (p.ExitCode < 5)

           {

             well, surprise, surprise  return code 4 or less!;

           }

           else

           {

               report return code 5 or greater;

           }

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.