frequently ask ? : Computers : Programming : Languages : Bbcbasic

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

Entry

BBCBASIC: Windows: How to get the value of an environment variable using a Microsoft Windows API?

Apr 30th, 2006 17:07
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 01 May 2021 - 00:01 am ------------------------
BBCBASIC: Windows: How to get the value of an environment variable 
using a Microsoft Windows API?
---
Use the Microsoft Windows API
 GetEnvironmentVariable
===
Steps: Overview:
 1. -E.g. create the following program
--- cut here: begin --------------------------------------------------
REM --- MAIN --- REM
PRINT FNStringGetEnvironmentS( "WINDIR" ) : REM gives e.g. "C:\WINNT" 
(when working on a Windows2000 machine) / or "C:\WINDOWS" (when 
working on a Microsoft Windows XP machine)
PRINT FNStringGetEnvironmentS( "APPDATA" ) : REM gives 
e.g. "c:\document and settings\administrator\application data"
END
:
:
:
REM --- LIBRARY --- REM
:
REM library: string: get: environment (searches for and returns a 
specified environment string) (filenamemacro=getstgen.bbc) [kn, ri, 
su, 30-04-2021 23:08:28]
DEF FNStringGetEnvironmentS( s$ )
 REM e.g. PRINT FNStringGetEnvironmentS( "WINDIR" ) : REM gives 
e.g. "C:\WINNT" (when working on a Windows2000 machine) / 
or "C:\WINDOWS" (when working on a Microsoft Windows XP machine)
 REM e.g. PRINT FNStringGetEnvironmentS( "APPDATA" ) : REM gives 
e.g. "c:\document and settings\administrator\application data"
 REM e.g. END
 REM e.g. :
 REM e.g. :
 REM e.g. :
 LOCAL lpNameI%
 LOCAL lpBufferI%
 LOCAL nSizeI%
 LOCAL valueS$
 LOCAL lengthI%
 nSizeI% = FNMathGetEnvironmentBufferMaxI : REM length of the buffer 
holding the result string
 DIM lpNameI% LOCAL nSizeI% : REM buffer to hold the given environment 
variable name (e.g. "WINDIR")
 DIM lpBufferI% LOCAL nSizeI% : REM buffer to hold the result
 $$lpNameI% = s$ : REM nul terminated string
 lengthI% = -1 : REM initialize lengthI% to some arbitrary value (but 
not zero, as that indicates an error, and you might also test for this)
 SYS "GetEnvironmentVariableA", lpNameI%, lpBufferI%, nSizeI% TO 
lengthI%
 valueS$ = $$lpBufferI%
 IF FNStringCheckEmptyB( valueS$ ) OR ( lengthI% = 0 ) THEN 
PROCWarnCons4( "environment variable", s$, ":", "not found" ) : 
valueS$ = FNStringGetErrorS
= valueS$
:
REM library: math: get: environment: buffer: max 
(filenamemacro=getmami.bbc) [kn, ri, su, 30-04-2021 22:35:28]
DEF FNMathGetEnvironmentBufferMaxI
 REM e.g. PRINT FNMathGetEnvironmentBufferMaxI
 REM e.g. END
 REM e.g. :
 REM e.g. :
 REM e.g. :
=  256
:
REM library: string: empty? [kn, ri, we, 09-05-2021 19:47:51]
DEF FNStringCheckEmptyB( s$ )
= FNStringCheckEqualB( s$, "" )
REM variation: = FNStringCheckEqualB( s$, FNStringGetEmptyS )
:
REM library: warn: cons4 (filenamemacro=conswawe.bbc) [kn, ri, su, 30-
04-2006 22:51:08]
DEF PROCWarnCons4( s1$, s2$, s3$, s4$ )
 REM e.g. PROCWarnCons4( "this", "is", "an", "error" ) : REM gives 
e.g. "Warning: this is an error"
 REM e.g. END
 REM e.g. :
 REM e.g. :
 REM e.g. :
 PROCWarn( FNStringGetCons4S( s1$, s2$, s3$, s4$ ) )
ENDPROC
:
REM library: error: general output string to recognize an error (e.g. 
in another routine). Central routine, only one occurrence of this 
constant string [kn, ri, fr, 11-05-2021 11:43:48]
DEF FNStringGetErrorS
= "<ERROR>"
:
REM library: string: check: equal (string1 equal to string2?) 
(filenamemacro=checstce.bbc) [kn, ri, we, 09-05-2021 19:47:51]
DEF FNStringCheckEqualB( s1$, s2$ )
 REM e.g. PRINT FNStringCheckEqualB( "test", "test" ) : REM gives 
true, because "test" equals "test"
 REM e.g. END
 REM e.g. :
 REM e.g. :
 REM e.g. :
= s1$ = s2$
:
REM library: string: return an empty string [kn, ri, we, 09-05-2021 
19:53:53]
DEF FNStringGetEmptyS
= ""
:
REM library: warn (show a warning message) (filenamemacro=wawarn.bbc) 
[kn, ri, su, 30-04-2021 22:43:08]
DEF PROCWarn( s$ )
 REM e.g. PROCWarn( "test" ) : REM gives e.g. "Warning: test"
 REM e.g. END
 REM e.g. :
 REM e.g. :
 REM e.g. :
 PRINT FNStringGetCons3S( "Warning", ":", s$ )
ENDPROC
:
REM library: string: concatenation: 4 strings [kn, ri, th, 10-05-2021 
16:44:52]
DEF FNStringGetCons4S( s1$, s2$, s3$, s4$ )
 REM e.g. PRINT; FNStringGetCons4S( "1", "2", "3", "4" )
 REM e.g. END
= FNStringGetConsS( FNStringGetCons3S( s1$, s2$, s3$ ), s4$ )
REM variation = FNStringGetConsSeparatorS( s1$, s2$, s3$, s4$, 
FNStringGetEmptyS )
:
REM library: string: concatenation: 3 strings [kn, ri, th, 10-05-2021 
16:44:45]
DEF FNStringGetCons3S( s1$, s2$, s3$ )
 REM e.g. PRINT; FNStringGetCons3S( "1", "2", "3" )
 REM e.g. END
= FNStringGetConsS( FNStringGetConsS( s1$, s2$ ), s3$ )
REM variation = FNStringGetConsSeparatorS( s1$, s2$, s3$, 
FNStringGetEmptyS )
:
REM library: string: token: concatenate two strings to one, with one 
extra space in between [kn, ri, th, 10-05-2021 14:33:30]
DEF FNStringGetConsS( s1$, s2$ )
IF s1$ = "" THEN = s2$
IF s2$ = "" THEN = s1$
= s1$ + " " + s2$
:
REM library: string: token: concatenate two strings to one, with one 
separator, with one extra space in between [kn, ri, fr, 11-05-2021 
12:40:25]
DEF FNStringGetConsSeparatorS( s1$, s2$, separator$ )
= FNStringGetConsS( FNStringGetConcatS( s1$, separator$ ), s2$ )
:
REM library: string: concatenate: two strings [kn, ri, th, 10-05-2021 
14:32:25]
DEF FNStringGetConcatS( s1$, s2$ )
= s1$ + s2$
--- cut here: end ----------------------------------------------------
 2. -Run the program
 3. -That will show a screen output similar to the following:
--- cut here: begin --------------------------------------------------
 C:\WINDOWS
--- cut here: end ----------------------------------------------------
===
Tested successfully on
Microsoft Windows XP Professional (service pack 2),
running
BBCBASIC for Windows v5.10b
===
Internet: see also:
---
BBCBASIC: Windows: Microsoft: Windows: API: Link: Overview: Can you 
give an overview of links?
http://www.faqts.com/knowledge_base/view.phtml/aid/40857/fid/768
----------------------------------------------------------------------