Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

Environment Variables in VisualCOBOL

Environment Variables in VisualCOBOL

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.

System.Environment

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:

Name

Description

CommandLine

Gets the command line for this process.

CurrentDirectory

Gets or sets the fully qualified path of the current working directory.

ExitCode

Gets or sets the exit code of the process.

HasShutdownStarted

Gets a value indicating whether the common language runtime (CLR) is shutting down.

Is64BitOperatingSystem

Determines whether the current operating system is a 64-bit operating system.

Is64BitProcess

Determines whether the current process is a 64-bit process.

MachineName

Gets the NetBIOS name of this local computer.

NewLine

Gets the newline string defined for this environment.

OSVersion

Gets an OperatingSystem object that contains the current platform identifier and version number.

ProcessorCount

Gets the number of processors on the current machine.

StackTrace

Gets current stack trace information.

SystemDirectory

Gets the fully qualified path of the system directory.

SystemPageSize

Gets the amount of memory for an operating system's page file.

TickCount

Gets the number of milliseconds elapsed since the system started.

UserDomainName

Gets the network domain name associated with the current user.

UserInteractive

Gets a value indicating whether the current process is running in user interactive mode.

UserName

Gets the user name of the person who is currently logged on to the Windows operating system.

Version

Gets a Version object that describes the major, minor, build, and revision numbers of the common language runtime.

WorkingSet

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:

Name

Description

Exit

Terminates this process and gives the underlying operating system the specified exit code.

ExpandEnvironmentVariables

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.

FailFast(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.

FailFast(String, Exception)

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.

GetCommandLineArgs

Returns a string array containing the command-line arguments for the current process.

GetEnvironmentVariable(String)

Retrieves the value of an environment variable from the current process.

GetEnvironmentVariable(String, EnvironmentVariableTarget)

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.

GetEnvironmentVariables()

Retrieves all environment variable names and their values from the current process.

GetEnvironmentVariables(EnvironmentVariableTarget)

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.

GetFolderPath(Environment.SpecialFolder)

Gets the path to the system special folder that is identified by the specified enumeration.

GetFolderPath(Environment.SpecialFolder, Environment.SpecialFolderOption)

Gets the path to the system special folder that is identified by the specified enumeration, and uses a specified option for accessing special folders.

GetLogicalDrives

Returns an array of string containing the names of the logical drives on the current computer.

SetEnvironmentVariable(String, String)

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.


Wrap-Up

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

Happy Coding!

 

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Comments

As COBOL items often has extra spaces at the end you may also want to consider using String::TrimEnd too.

ref: msdn.microsoft.com/.../system.string.trimend.aspx

As COBOL items often have extra spaces at the end, so you may want to consider using String::TrimEnd too.

ref: msdn.microsoft.com/.../system.string.trimend.aspx

Version history
Revision #:
1 of 1
Last update:
‎2012-06-21 16:58
Updated by:
 
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.