vb6parse 1.0.0

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="VB6Parse Library Reference - environ - Environment">
    <title>environ - Environment - VB6Parse Library Reference</title>
    <link rel="stylesheet" href="../../../assets/css/style.css">
    <link rel="stylesheet" href="../../../assets/css/docs-style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
    <script src="../../../assets/js/theme-switcher.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
    <script>hljs.highlightAll();</script>
</head>
<body>
    <header class="docs-header">
        <div class="container">
            <h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/environment/index.html">Environment</a> / environ</h1>
            <p class="tagline">VB6 Library Reference</p>
        </div>
    </header>

    <nav class="docs-nav">
        <div class="container">
            <a href="../../../index.html">Home</a>
            <a href="../../../library/index.html">Library Reference</a>
            <a href="../../../documentation.html">Documentation</a>
            <a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
            <a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
            <button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
                <span class="theme-icon">🌙</span>
            </button>
        </div>
    </nav>

    <main class="container">
        
        <article class="library-item">
            <h1 id="environ-function">Environ Function</h1>
<p>Returns the String value associated with an operating system environment variable.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Environ(envstring | number)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><strong>envstring</strong>: Optional (if number provided). String expression containing the name of an
  environment variable.</li>
<li><strong>number</strong>: Optional (if envstring provided). Numeric expression corresponding to the
  numeric order of an environment string in the environment-string table. The number argument
  can be any numeric expression, but is rounded to a whole number before it is evaluated.</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a String containing the value assigned to envstring or the environment variable at
position number. Returns a zero-length string ("") if envstring is not found or if there is
no environment string at position number.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>Environ</code> function retrieves values from the operating system's environment variables.
Environment variables are system-level or user-level settings that provide information about
the operating system environment.
<strong>Important Characteristics:</strong>
- Reads environment variables from the operating system
- Can access by name (string) or position (number)
- Case-insensitive on Windows
- Returns empty string if variable not found
- Position-based access starts at 1 (not 0)
- Number of environment variables varies by system
- Environment changes during execution are not reflected
- Snapshot taken at application start
- Cannot modify environment variables (read-only)
- Different users may have different environment variables</p>
<h2 id="common-environment-variables">Common Environment Variables</h2>
<p><strong>Windows:</strong>
- <code>PATH</code> - Executable search path
- <code>TEMP</code> or <code>TMP</code> - Temporary files directory
- <code>USERNAME</code> - Current user name
- <code>USERPROFILE</code> - User's profile directory
- <code>COMPUTERNAME</code> - Computer name
- <code>SYSTEMROOT</code> - Windows system directory
- <code>PROGRAMFILES</code> - Program Files directory
- <code>HOMEDRIVE</code> - User's home drive (e.g., C:)
- <code>HOMEPATH</code> - User's home directory path
- <code>APPDATA</code> - Application data directory
- <code>WINDIR</code> - Windows directory
- <code>PROCESSOR_ARCHITECTURE</code> - CPU architecture
- <code>NUMBER_OF_PROCESSORS</code> - Number of CPU cores
- <code>OS</code> - Operating system name</p>
<h2 id="examples">Examples</h2>
<h3 id="basic-usage">Basic Usage</h3>
<pre><code class="language-vbnet">&#x27; Get environment variable by name
Dim userName As String
userName = Environ(&quot;USERNAME&quot;)
MsgBox &quot;Current user: &quot; &amp; userName
&#x27; Get temp directory
Dim tempDir As String
tempDir = Environ(&quot;TEMP&quot;)
&#x27; Get by position
Dim firstEnvVar As String
firstEnvVar = Environ(1)</code></pre>
<h3 id="check-if-variable-exists">Check if Variable Exists</h3>
<pre><code class="language-vbnet">Function EnvironVarExists(varName As String) As Boolean
    EnvironVarExists = (Len(Environ(varName)) &gt; 0)
End Function
&#x27; Usage
If EnvironVarExists(&quot;JAVA_HOME&quot;) Then
    MsgBox &quot;Java is configured&quot;
Else
    MsgBox &quot;Java not found&quot;
End If</code></pre>
<h3 id="build-file-paths">Build File Paths</h3>
<pre><code class="language-vbnet">Function GetTempFilePath(fileName As String) As String
    Dim tempDir As String
    tempDir = Environ(&quot;TEMP&quot;)
    If Right(tempDir, 1) &lt;&gt; &quot;\&quot; Then
        tempDir = tempDir &amp; &quot;\&quot;
    End If
    GetTempFilePath = tempDir &amp; fileName
End Function</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="get-user-directories">Get User Directories</h3>
<pre><code class="language-vbnet">Function GetUserProfile() As String
    GetUserProfile = Environ(&quot;USERPROFILE&quot;)
End Function
Function GetAppDataPath() As String
    GetAppDataPath = Environ(&quot;APPDATA&quot;)
End Function
Function GetDesktopPath() As String
    GetDesktopPath = Environ(&quot;USERPROFILE&quot;) &amp; &quot;\Desktop&quot;
End Function
Function GetMyDocuments() As String
    GetMyDocuments = Environ(&quot;USERPROFILE&quot;) &amp; &quot;\Documents&quot;
End Function</code></pre>
<h3 id="system-information">System Information</h3>
<pre><code class="language-vbnet">Function GetComputerName() As String
    GetComputerName = Environ(&quot;COMPUTERNAME&quot;)
End Function
Function GetProcessorCount() As Integer
    Dim procCount As String
    procCount = Environ(&quot;NUMBER_OF_PROCESSORS&quot;)
    If IsNumeric(procCount) Then
        GetProcessorCount = CInt(procCount)
    Else
        GetProcessorCount = 1
    End If
End Function
Function GetSystemArchitecture() As String
    GetSystemArchitecture = Environ(&quot;PROCESSOR_ARCHITECTURE&quot;)
End Function</code></pre>
<h3 id="list-all-environment-variables">List All Environment Variables</h3>
<pre><code class="language-vbnet">Sub ListAllEnvironmentVariables()
    Dim i As Integer
    Dim envVar As String
    i = 1
    Do
        envVar = Environ(i)
        If envVar = &quot;&quot; Then Exit Do
        Debug.Print i &amp; &quot;: &quot; &amp; envVar
        i = i + 1
    Loop
End Sub</code></pre>
<h3 id="parse-environment-variable">Parse Environment Variable</h3>
<pre><code class="language-vbnet">Function GetEnvironVarName(envString As String) As String
    Dim equalPos As Integer
    equalPos = InStr(envString, &quot;=&quot;)
    If equalPos &gt; 0 Then
        GetEnvironVarName = Left(envString, equalPos - 1)
    Else
        GetEnvironVarName = &quot;&quot;
    End If
End Function
Function GetEnvironVarValue(envString As String) As String
    Dim equalPos As Integer
    equalPos = InStr(envString, &quot;=&quot;)
    If equalPos &gt; 0 Then
        GetEnvironVarValue = Mid(envString, equalPos + 1)
    Else
        GetEnvironVarValue = &quot;&quot;
    End If
End Function</code></pre>
<h3 id="safe-path-construction">Safe Path Construction</h3>
<pre><code class="language-vbnet">Function BuildSafePath(envVar As String, subPath As String) As String
    Dim basePath As String
    basePath = Environ(envVar)
    If basePath = &quot;&quot; Then
        BuildSafePath = &quot;&quot;
        Exit Function
    End If
    &#x27; Ensure path ends with backslash
    If Right(basePath, 1) &lt;&gt; &quot;\&quot; Then
        basePath = basePath &amp; &quot;\&quot;
    End If
    &#x27; Remove leading backslash from subPath if present
    If Left(subPath, 1) = &quot;\&quot; Then
        subPath = Mid(subPath, 2)
    End If
    BuildSafePath = basePath &amp; subPath
End Function</code></pre>
<h3 id="configuration-file-paths">Configuration File Paths</h3>
<pre><code class="language-vbnet">Function GetConfigFilePath(appName As String, fileName As String) As String
    Dim appDataPath As String
    Dim configDir As String
    appDataPath = Environ(&quot;APPDATA&quot;)
    configDir = appDataPath &amp; &quot;\&quot; &amp; appName
    &#x27; Create directory if it doesn&#x27;t exist
    If Dir(configDir, vbDirectory) = &quot;&quot; Then
        MkDir configDir
    End If
    GetConfigFilePath = configDir &amp; &quot;\&quot; &amp; fileName
End Function</code></pre>
<h3 id="search-path-variable">Search PATH Variable</h3>
<pre><code class="language-vbnet">Function FindInPath(executable As String) As String
    Dim pathVar As String
    Dim paths() As String
    Dim i As Integer
    Dim testPath As String
    pathVar = Environ(&quot;PATH&quot;)
    paths = Split(pathVar, &quot;;&quot;)
    For i = LBound(paths) To UBound(paths)
        testPath = paths(i) &amp; &quot;\&quot; &amp; executable
        If Dir(testPath) &lt;&gt; &quot;&quot; Then
            FindInPath = testPath
            Exit Function
        End If
    Next i
    FindInPath = &quot;&quot;
End Function</code></pre>
<h3 id="check-operating-system">Check Operating System</h3>
<pre><code class="language-vbnet">Function IsWindows() As Boolean
    Dim osVar As String
    osVar = UCase(Environ(&quot;OS&quot;))
    IsWindows = (InStr(osVar, &quot;WINDOWS&quot;) &gt; 0)
End Function
Function GetWindowsDirectory() As String
    GetWindowsDirectory = Environ(&quot;WINDIR&quot;)
End Function
Function GetSystemRoot() As String
    GetSystemRoot = Environ(&quot;SYSTEMROOT&quot;)
End Function</code></pre>
<h3 id="program-files-paths">Program Files Paths</h3>
<pre><code class="language-vbnet">Function GetProgramFilesPath() As String
    GetProgramFilesPath = Environ(&quot;PROGRAMFILES&quot;)
End Function
Function GetProgramFilesX86Path() As String
    GetProgramFilesX86Path = Environ(&quot;PROGRAMFILES(X86)&quot;)
End Function
Function FindProgramPath(programName As String) As String
    Dim progFiles As String
    Dim testPath As String
    &#x27; Check Program Files
    progFiles = Environ(&quot;PROGRAMFILES&quot;)
    testPath = progFiles &amp; &quot;\&quot; &amp; programName
    If Dir(testPath, vbDirectory) &lt;&gt; &quot;&quot; Then
        FindProgramPath = testPath
        Exit Function
    End If
    &#x27; Check Program Files (x86)
    progFiles = Environ(&quot;PROGRAMFILES(X86)&quot;)
    If progFiles &lt;&gt; &quot;&quot; Then
        testPath = progFiles &amp; &quot;\&quot; &amp; programName
        If Dir(testPath, vbDirectory) &lt;&gt; &quot;&quot; Then
            FindProgramPath = testPath
            Exit Function
        End If
    End If
    FindProgramPath = &quot;&quot;
End Function</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="environment-variable-dictionary">Environment Variable Dictionary</h3>
<pre><code class="language-vbnet">Function GetEnvironmentDictionary() As Collection
    Dim dict As New Collection
    Dim i As Integer
    Dim envVar As String
    Dim varName As String
    Dim varValue As String
    Dim equalPos As Integer
    i = 1
    Do
        envVar = Environ(i)
        If envVar = &quot;&quot; Then Exit Do
        equalPos = InStr(envVar, &quot;=&quot;)
        If equalPos &gt; 0 Then
            varName = Left(envVar, equalPos - 1)
            varValue = Mid(envVar, equalPos + 1)
            On Error Resume Next
            dict.Add varValue, UCase(varName)
            On Error GoTo 0
        End If
        i = i + 1
    Loop
    Set GetEnvironmentDictionary = dict
End Function</code></pre>
<h3 id="expand-environment-variables-in-string">Expand Environment Variables in String</h3>
<pre><code class="language-vbnet">Function ExpandEnvironmentString(inputString As String) As String
    Dim result As String
    Dim startPos As Integer
    Dim endPos As Integer
    Dim varName As String
    Dim varValue As String
    result = inputString
    &#x27; Find %VAR% patterns
    Do
        startPos = InStr(result, &quot;%&quot;)
        If startPos = 0 Then Exit Do
        endPos = InStr(startPos + 1, result, &quot;%&quot;)
        If endPos = 0 Then Exit Do
        varName = Mid(result, startPos + 1, endPos - startPos - 1)
        varValue = Environ(varName)
        result = Left(result, startPos - 1) &amp; varValue &amp; Mid(result, endPos + 1)
    Loop
    ExpandEnvironmentString = result
End Function
&#x27; Usage
expandedPath = ExpandEnvironmentString(&quot;%TEMP%\myfile.txt&quot;)</code></pre>
<h3 id="create-application-log-file">Create Application Log File</h3>
<pre><code class="language-vbnet">Function CreateLogFile(appName As String) As String
    Dim logDir As String
    Dim logFile As String
    Dim dateStamp As String
    logDir = Environ(&quot;TEMP&quot;) &amp; &quot;\Logs&quot;
    &#x27; Create logs directory
    If Dir(logDir, vbDirectory) = &quot;&quot; Then
        MkDir logDir
    End If
    dateStamp = Format(Date, &quot;yyyy-mm-dd&quot;)
    logFile = logDir &amp; &quot;\&quot; &amp; appName &amp; &quot;_&quot; &amp; dateStamp &amp; &quot;.log&quot;
    CreateLogFile = logFile
End Function</code></pre>
<h3 id="check-development-environment">Check Development Environment</h3>
<pre><code class="language-vbnet">Function IsDevelopmentEnvironment() As Boolean
    &#x27; Check for common development environment variables
    IsDevelopmentEnvironment = (Len(Environ(&quot;VSCODE_PID&quot;)) &gt; 0) Or _
                              (Len(Environ(&quot;TERM_PROGRAM&quot;)) &gt; 0) Or _
                              (Len(Environ(&quot;VSAPPIDDIR&quot;)) &gt; 0)
End Function
Function GetJavaHome() As String
    GetJavaHome = Environ(&quot;JAVA_HOME&quot;)
End Function
Function GetPythonPath() As String
    GetPythonPath = Environ(&quot;PYTHONPATH&quot;)
End Function</code></pre>
<h3 id="build-connection-string">Build Connection String</h3>
<pre><code class="language-vbnet">Function BuildConnectionString() As String
    Dim server As String
    Dim database As String
    server = Environ(&quot;DB_SERVER&quot;)
    database = Environ(&quot;DB_NAME&quot;)
    If server = &quot;&quot; Then server = &quot;localhost&quot;
    If database = &quot;&quot; Then database = &quot;default&quot;
    BuildConnectionString = &quot;Server=&quot; &amp; server &amp; &quot;;Database=&quot; &amp; database
End Function</code></pre>
<h3 id="export-environment-to-file">Export Environment to File</h3>
<pre><code class="language-vbnet">Sub ExportEnvironmentToFile(filePath As String)
    Dim fileNum As Integer
    Dim i As Integer
    Dim envVar As String
    fileNum = FreeFile
    Open filePath For Output As #fileNum
    Print #fileNum, &quot;Environment Variables&quot;
    Print #fileNum, &quot;Generated: &quot; &amp; Now
    Print #fileNum, String(80, &quot;-&quot;)
    i = 1
    Do
        envVar = Environ(i)
        If envVar = &quot;&quot; Then Exit Do
        Print #fileNum, envVar
        i = i + 1
    Loop
    Close #fileNum
End Sub</code></pre>
<h3 id="portable-path-builder">Portable Path Builder</h3>
<pre><code class="language-vbnet">Function GetPortableAppPath(relativePath As String) As String
    Dim basePath As String
    &#x27; Try to get from environment first
    basePath = Environ(&quot;APP_BASE_PATH&quot;)
    &#x27; Fall back to current directory
    If basePath = &quot;&quot; Then
        basePath = App.Path
    End If
    If Right(basePath, 1) &lt;&gt; &quot;\&quot; Then
        basePath = basePath &amp; &quot;\&quot;
    End If
    GetPortableAppPath = basePath &amp; relativePath
End Function</code></pre>
<h2 id="error-handling">Error Handling</h2>
<pre><code class="language-vbnet">Function SafeEnviron(varName As String, Optional defaultValue As String = &quot;&quot;) As String
    On Error Resume Next
    SafeEnviron = Environ(varName)
    If Err.Number &lt;&gt; 0 Or SafeEnviron = &quot;&quot; Then
        SafeEnviron = defaultValue
    End If
End Function
Function GetEnvironWithFallback(preferredVar As String, fallbackVar As String) As String
    GetEnvironWithFallback = Environ(preferredVar)
    If GetEnvironWithFallback = &quot;&quot; Then
        GetEnvironWithFallback = Environ(fallbackVar)
    End If
End Function</code></pre>
<h3 id="common-errors">Common Errors</h3>
<ul>
<li><strong>Error 5</strong> (Invalid procedure call): Invalid number argument (&lt; 1)</li>
<li>No error for missing variables (returns empty string)</li>
</ul>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>Environ</code> is relatively fast (direct OS call)</li>
<li>Environment snapshot taken at application start</li>
<li>Cache frequently used values to avoid repeated calls</li>
<li>Position-based enumeration stops at first empty string</li>
<li>String comparison is case-insensitive on Windows</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<h3 id="always-check-for-empty-string">Always Check for Empty String</h3>
<pre><code class="language-vbnet">&#x27; Good - Check before using
tempDir = Environ(&quot;TEMP&quot;)
If tempDir = &quot;&quot; Then
    tempDir = &quot;C:\Temp&quot;  &#x27; Fallback
End If
&#x27; Avoid - Assuming variable exists
tempDir = Environ(&quot;TEMP&quot;)  &#x27; May be empty!</code></pre>
<h3 id="use-constants-for-variable-names">Use Constants for Variable Names</h3>
<pre><code class="language-vbnet">&#x27; Good - Constants for maintainability
Const ENV_TEMP = &quot;TEMP&quot;
Const ENV_USERNAME = &quot;USERNAME&quot;
tempDir = Environ(ENV_TEMP)
userName = Environ(ENV_USERNAME)</code></pre>
<h3 id="provide-defaults">Provide Defaults</h3>
<pre><code class="language-vbnet">Function GetTempDir() As String
    GetTempDir = Environ(&quot;TEMP&quot;)
    If GetTempDir = &quot;&quot; Then GetTempDir = Environ(&quot;TMP&quot;)
    If GetTempDir = &quot;&quot; Then GetTempDir = &quot;C:\Temp&quot;
End Function</code></pre>
<h3 id="case-insensitive-on-windows">Case Insensitive on Windows</h3>
<pre><code class="language-vbnet">&#x27; All equivalent on Windows
userName = Environ(&quot;USERNAME&quot;)
userName = Environ(&quot;username&quot;)
userName = Environ(&quot;UserName&quot;)</code></pre>
<h2 id="comparison-with-other-methods">Comparison with Other Methods</h2>
<h3 id="environ-vs-registry">Environ vs Registry</h3>
<pre><code class="language-vbnet">&#x27; Environ - Quick, read-only access to environment
tempDir = Environ(&quot;TEMP&quot;)
&#x27; Registry - More control, can read/write, more complex
&#x27; (Requires Windows API or Registry object)</code></pre>
<h3 id="environ-vs-command-line">Environ vs Command Line</h3>
<pre><code class="language-vbnet">&#x27; Environ - Environment variables
userName = Environ(&quot;USERNAME&quot;)
&#x27; Command - Command line arguments
args = Command()</code></pre>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Read-only access (cannot modify environment variables)</li>
<li>Snapshot at application start (changes not reflected)</li>
<li>Position-based enumeration order not guaranteed</li>
<li>Cannot create or delete environment variables</li>
<li>Limited to process environment (not system-wide)</li>
<li>No wildcard or pattern matching</li>
<li>Case-sensitive on Unix/Linux (VB6 primarily Windows)</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Command</code>: Returns command-line arguments</li>
<li><code>CurDir</code>: Returns current directory</li>
<li><code>ChDir</code>: Changes current directory</li>
<li><code>App.Path</code>: Returns application path</li>
<li><code>Shell</code>: Executes external programs (can set environment)</li>
<li><code>GetSetting</code>: Reads application settings from registry</li>
<li><code>SaveSetting</code>: Writes application settings to registry</li>
</ul>
        </article>
        
        <div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
            <p>
                <a href="index.html">← Back to Environment</a> |
                <a href="../index.html">View all functions</a>
            </p>
        </div>

    </main>

    <footer>
        <div class="container">
            <p>&copy; 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
        </div>
    </footer>
</body>
</html>