Skip to main content

Problem:

Often there is a need to read and set operating system environment variables from COBOL programs.  How can this be done?

Resolution:

Micro Focus COBOL provides syntax that permits the reading and setting of environment variables within the Windows system.

Please note the following points though:

1)  Creating/changing the value environment variable it will only remain current for the duration of the COBOL run unit.  Other applications will still see the original setting.

2) To call MFEXTMAP to enable the setting of environment variables.

3) To use ON EXCEPTION to test for errors when manipulating environment variables.

The following example illustrates the syntax required to manipulate an environment variable valled TESTENV:

      $set xopen

       working-storage section.

       01  ws-env                          pic x(40).

       procedure division.

           call "mfextmap".

           display "TESTENV" upon environment-name.

           accept ws-env from environment-value

                 on exception

                    display "TESTENV not found!" at 0420

                    stop run

                 not on exception

                    display ws-env at 0420

           end-accept.

           display "TESTENV" upon environment-name.

           display "James Dean" upon environment-value.

           display "TESTENV" upon environment-name.

           accept ws-env from environment-value

                 on exception

                    display "TESTENV not found!" at 0520

                    stop run

                 not on exception

                    display ws-env at 0520

           end-accept.

           stop run.

Old KB# 4397