Skip to main content

Problem:

Retrieving ALL the Environment variables of the current process with ONE call to the Microsoft API

GetEnvironmentStrings.

Resolution:

      $SET  case defaultbyte"00"

       special-names.

           call-convention 74 is winapi.

       working-storage section.

       01 ptr              pointer value null.

       01 i pic 9(18) value 0.

       01 j pic 9(18) value 0.

       01 k pic 9(18) value 0.

       01 l pic 9(18) value 0.

       01 WSEnvtStrings    pic x(150000) value spaces.

       01 bool pic s9(9) comp-5.

       linkage section.

       01 LKEnvtStrings      pic x.

       procedure division.

           call "cob32api"

      *_________________________________________________________________

       *> STEP1 use of GetEnvironmentStrings   retrieve data without using a temporary WSS data

           call winapi "GetEnvironmentStrings"

               returning ptr

           display "*--> GetEnvironmentStrings " with no advancing

           if ptr =  null display "KO" STOP RUN

           else           display "OK"

                          set address of LKEnvtStrings  to ptr

                          end-if

           perform varying i from 1 by 1

               until LKEnvtStrings(i:2) = low-value

               if LKEnvtStrings(i:1) not = "="

               and LKEnvtStrings(i:1) not = low-value

                   display LKEnvtStrings(i:1) with no advancing

               else if LKEnvtStrings(i:1) = "="

                           display " >>> "    with no advancing

                    else   display " "

                    end-if

               end-if

               end-perform

           display " "

           display "*--> length of EnvironmentStrings: " i "/"

           call winapi "FreeEnvironmentStringsA"

                   using by value ptr

                   returning bool

           end-call

           display "*--> FreeEnvironmentStringsA " with no advancing

           if bool = 0 display "KO" else display "OK" end-if

      *_________________________________________________________________

      *_________________________________________________________________

           STOP RUN.

      *_________________________________________________________________

      *_________________________________________________________________

       *> STEP2 use of GetEnvironmentStrings   retrieve data using a temporary WSS data

           call winapi "GetEnvironmentStrings"

               returning ptr

           display "*--> GetEnvironmentStrings " with no advancing

           if ptr =  null display "KO" stop run

           else           display "OK"

                          set address of LKEnvtStrings  to ptr

           end-if

           perform varying i from 1 by 1

               until LKEnvtStrings(i:2) = low-value

      *        display "*--> " i " -> " LKEnvtStrings(i:1)

               move LKEnvtStrings(i:1) to WSEnvtStrings(i:1)

           end-perform

      *    display "*--> " i "/"  WSEnvtStrings(1:i)

           display "*--> length of EnvironmentStrings: " i "/"

           call winapi "FreeEnvironmentStringsA"

                   using by value ptr

                   returning bool

           end-call

           display "*--> FreeEnvironmentStringsA " with no advancing

           if bool = 0 display "KO" else display "OK" end-if

           Display "*--> List of all Envt vars of the process"

           perform varying j from 1 by 1 until j > i

               perform varying k from j by 1

                   until WSEnvtStrings(k:1)  = "="

               end-perform

               perform varying l from k  by 1

                   until WSEnvtStrings(l:1)  = low-value

                         or l > i

               end-perform

               display "*--> " WSEnvtStrings(j:(k - j )) " >>> "

                       with no advancing

               display         WSEnvtStrings((k 1 ):(l - k))

               move l to  j

           end-perform

           stop run.

Old KB# 7056