Skip to content

Latest commit

 

History

History
88 lines (64 loc) · 2.06 KB

sample_089.md

File metadata and controls

88 lines (64 loc) · 2.06 KB

Home

Storing the environment strings in cursor

Before you begin:

See also:


Code:

DO declare
LOCAL nProcAddr, cBuffer, nBufsize

* get the address of the environment block of the current process
nProcAddr = GetEnvironmentStrings()
IF nProcAddr = 0
	RETURN
ENDIF

* allocate sufficient buffer
nBufsize = GlobalSize(nProcAddr)
nBufsize = Iif(nBufsize=0, 4096, nBufsize)
cBuffer = Repli(Chr(0), nBufsize)
	
* copy memory block to the buffer
= CopyMemory(@cBuffer, nProcAddr, Len(cBuffer))

* the zero word marks the end of the environment block
cBuffer = Left(cBuffer, AT(Chr(0)+Chr(0), cBuffer))

* target cursor
CREATE CURSOR cs (envname C(30), sz I, envvalue C(250))

LOCAL nIndex, ss, ch, lnPos, lcName, lcValue
ss = ""
FOR nIndex=1 TO Len(cBuffer)
	ch = SUBSTR(cBuffer, nIndex, 1)
	IF ch = Chr(0)
		lnPos = AT("=", ss)
		lcName = LEFT(ss, lnPos-1)
		lcValue = SUBSTR(ss, lnPos+1)
		INSERT INTO cs VALUES (lcName, Len(lcValue), lcValue)
		ss = ""
	ELSE
		ss = ss + ch
	ENDIF
ENDFOR

* release the environment block
= FreeEnvironmentStrings(nProcAddr)

SELECT cs
GO TOP
BROW NORMAL NOWAIT
* end of main

PROCEDURE declare
	DECLARE INTEGER GlobalSize IN kernel32 INTEGER hMem
	DECLARE INTEGER GetEnvironmentStrings IN kernel32

	DECLARE INTEGER FreeEnvironmentStrings IN kernel32;
		INTEGER lpszEnvBlock

  	DECLARE RtlMoveMemory IN kernel32 As CopyMemory;
		STRING @Dest, INTEGER Source, INTEGER nLength  

Listed functions:

FreeEnvironmentStrings
GetEnvironmentStrings
GlobalSize

Comment:

This way you get all environment variables, so you dont need to provide their names as with the GetEnv() function.