Rational Developer for System z, Version 7.6

Accessing environment variables in a COBOL sample program

Sometimes it is necessary to dynamically retrieve the value of an environment variable at run time. This topic describes one way to modify the code in one of the COBOL sample projects to enable environment variable access.

This topic involves COBOL Sample 1, which is available through the workbench examples interface. The sample includes two COBOL files, StartApp.cbl and PrintApp.cbl. Complete the following instructions to modify a project containing this sample so that StartApp.cbl reads an environment variable and displays its value.

  1. Switch to the z/OS® Projects perspective.
  2. Create a sample project for COBOL Sample 1.
  3. Edit StartApp.cbl.
  4. In the Working-Storage Section, add the following lines:
    01 P pointer.
    01 ENVVAR pic x(5) value Z"PATH".
    01 var-ptr pointer.
    01 var-len pic 9(4) binary.
  5. In the Linkage Section, add the following line:
    01 var pic x(1000).
  6. In the Procedure Division, add the following lines after the Initialize statement:
    Set P to address of ENVVAR.
    >>CALLINT OPTLINK
    call "getenv" using by value P returning var-ptr.
    >>CALLINT
    if var-ptr = null then
    	 Display "PATH is null"
    else
    	 Set address of var to var-ptr
    	 Move 0 to var-len
    	 Inspect var tallying var-len for characters 
    	 	before initial x"00"
    	 Display "PATH = " var (1:var-len)
    end-if.
  7. To modify the project's properties, do these steps:
    1. Open the Local COBOL Build Options Properties page and add the following in the Compile Options field:
      ,NODYNAM,PGMNAME(LONGMIXED)  
      Ensure that you maintain the leading comma to separate these options from the current set of options.
    2. Click Apply.
    3. Select Local Link Options and add the following to the Link Options:
      cpprmi36.dll
      This file is located in the bin directory of the Rational® Developer for System z® installation path, for example, c:\Program Files\IBM\SDP\bin.
    4. Click Apply.
  8. In the z/OS Projects view, right-click StartApp.cbl and click Nominate as Entry Point. If this action does not appear in the menu and the Clear Entry Point action appears instead, no further action is necessary.
  9. Right-click the project name and select Rebuild Project. If there are no errors, an executable is created in the BuildOutput folder. The executable is named StartApp.exe.
  10. Test that the program works, double-click StartApp.exe. A command shell opens with the prompt Enter a name or Q to quit.

Troubleshooting problems

After building the project, if StartApp.exe is not created, you may see a message in the task list like the following:
Error LNK2029: "?GETENV": unresolved external
This normally occurs when the .dll file specified above cannot be found. The required .dll file cpprmi36.dll is normally found in the bin folder of the Rational Developer for System z installation path. For example, C:\Program Files\IBM\SDP\bin. You may specify the complete path to the file in the Link Options field. In this case, it would be C:\Program Files\IBM\SDP\bin\cpprmi36.dll.

If other errors occur, ensure that the Compile Options property was correctly updated, as described above.


Terms of use | Feedback

This information center is powered by Eclipse technology. (http://www.eclipse.org)