NOTICE: Our Community is moving. Get more information. Updated information on a New Login Process
Article Description Micro Focus has long used environment variables in their products to define execution requirements, locate modules and enable runtime behavior. With the release of VisualCOBOL and a fully managed .NET environment though how does one enable the comparable functionality? .NET provides the answer with the “System.Environment” class.
The purpose of this article is to provide the reader with an example of how to read and set environment variables from within a managed COBOL environment. We will utilize a WinForm to provide our user interaction. The form has already been created utilizing standard labels, textboxes and a command button. The user can review the Visual COBOL documentation for an explanation of how to create the WinForm.
Let’s start by reviewing the System.Environment class within .NET.
The System.Environment class in .NET is used to retrieve information such as command-line arguments, the exit code, environment variable settings, the version of the operating system and other relevant piece of information about the environment in which the task is running in.
From the MSDN documentation the following properties are available within the class:
Gets the command line for this process.
Gets or sets the fully qualified path of the current working directory.
Gets or sets the exit code of the process.
Gets a value indicating whether the common language runtime (CLR) is shutting down.
Determines whether the current operating system is a 64-bit operating system.
Determines whether the current process is a 64-bit process.
Gets the NetBIOS name of this local computer.
Gets the newline string defined for this environment.
Gets an OperatingSystem object that contains the current platform identifier and version number.
Gets the number of processors on the current machine.
Gets current stack trace information.
Gets the fully qualified path of the system directory.
Gets the amount of memory for an operating system's page file.
Gets the number of milliseconds elapsed since the system started.
Gets the network domain name associated with the current user.
Gets a value indicating whether the current process is running in user interactive mode.
Gets the user name of the person who is currently logged on to the Windows operating system.
Gets a Version object that describes the major, minor, build, and revision numbers of the common language runtime.
Gets the amount of physical memory mapped to the process context.
As you can see there is quite a bit of information that one can obtain using the class. But we’re interested in how to either set or retrieve environment variables. The following table, again from the MSDN documentation, describes the methods available:
Terminates this process and gives the underlying operating system the specified exit code.
Replaces the name of each environment variable embedded in the specified string with the string equivalent of the value of the variable, then returns the resulting string.
Immediately terminates a process after writing a message to the Windows Application event log, and then includes the message in error reporting to Microsoft.
Immediately terminates a process after writing a message to the Windows Application event log, and then includes the message and exception information in error reporting to Microsoft.
Returns a string array containing the command-line arguments for the current process.
Retrieves the value of an environment variable from the current process.
Retrieves the value of an environment variable from the current process or from the Windows operating system registry key for the current user or local machine.
Retrieves all environment variable names and their values from the current process.
Retrieves all environment variable names and their values from the current process, or from the Windows operating system registry key for the current user or local machine.
Gets the path to the system special folder that is identified by the specified enumeration.
Gets the path to the system special folder that is identified by the specified enumeration, and uses a specified option for accessing special folders.
Returns an array of string containing the names of the logical drives on the current computer.
Creates, modifies, or deletes an environment variable stored in the current process.
SetEnvironmentVariable(String, String, EnvironmentVariableTarget)
Creates, modifies, or deletes an environment variable stored in the current process or in the Windows operating system registry key reserved for the current user or local machine.
The methods we’re interested in are the GetEnvironmentalVariable and SetEnvironmentVariable. Let’s look how these are coded up!
Retrieving Environment Variables
The button_click event of the Go button is what begins execution of the process. From the figure at the beginning of the article we’ve established two areas, a get and a set area. In the Get area we want to retrieve three environment variables, the execution path or ‘PATH’; the location of the install directory for Windows or ‘WinDir’; and the operating system that is running our application or ‘OS’. We will place the value returned for each environment variable into the text property of a textbox we’ve established for each. The code to accomplish this is:
The basic format for retrieving environment variables is
Set something to type Environment::GetEnvironmentVariable(variable name)
Where ‘something’ is a string defined variable. The ‘magic’ is available due to the keyword ‘type’. By specifying ‘type’ I am telling the VisualCOBOL runtime to access a native .NET type (i.e. a class, interface or enum etc.), in this case the class “Environment” and execute a static method, in this case “GetEnvironmentVariable” and the variable I want it to ‘get’ is contained within double quotes. Once you see the code and execute it, the process is really quite simple and straightforward. How about setting an environment variable though?
Setting Environment Variables
Before we can set an environment variable we have to ensure we’re providing a valid name. A value of spaces is not acceptable so we want to ensure we don’t provide spaces. We begin by first retrieving the value the user entered onto the screen into a working-storage variable defined in the program. The code to accomplish this is:
We then create an ‘IF’ statement to determine if the value is spaces or not. If it is not spaces we execute the method to set the environment variable the user defined.
This time we have to invoke a method to set the environment variable. We do this with the invoke statement and just as with setting an environment variable, we use the keyword ‘type’ to refer to a native .NET class directly, and thereby access a static method belonging to the class. In our case we’re going to once again use the “Environment” class and this time we’re going to execute the “SetEnvironmentVariable” method. This method takes two parameters, defined as strings. The first parameter is the name of the environment variable and the second is the value the parameter should contain. In our case we’re going to read that value directly from the Text property of the textbox where the value was entered thus saving some time and resources in having to define a working-storage field.
The ZIP file has all the necessary source code for you to follow along and see how to use environment variables in VisualCOBOL. Being able to utilize an existing .NET assembly from within VisualCOBOL will greatly expand the interoperability and life of your existing COBOL applications. While the process may seem complicated at first, spend a few minutes and analyze what we've done in this article. The steps will be basically the same for you and will greatly extend the usefulness of your existing applications to the .NET Framework. And that's what it's all about...
As COBOL items often has extra spaces at the end you may also want to consider using String::TrimEnd too.
As COBOL items often have extra spaces at the end, so you may want to consider using String::TrimEnd too.