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.
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
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.
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;
}