Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

System Variable 1

Status
Not open for further replies.

OzWolf

Programmer
May 22, 2002
52
AU
This is probably a simple thing to do, but be stuffed if I can figure it out.

I have a system variable that Windows 2000 stores under the variable %clientname%. I can retrieve this variable name via DOS prompt.

What I want to do is get this variable and use it in a Visual Basic application. How can I get Visual Basic to reference this?

Cheers.
 
Hi, OzWolf...

Check the GetEnvironmentVariable function in the API Text Viewer... It should solve that problem...

' In a module...
Public Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long

Sample code:

Function GetValue() As String
On Local Error Resume Next
Dim lBuffer As String, lVarName As String, lSize As Long, lResult As Long

' Initialize the variables
lSize = 255
lBuffer = Space$(lSize)
lVarName = "clientname"

' Call the API function
lResult = GetEnvironmentVariable(lVarName, lBuffer, lSize)
If lResult > 0 Then
lBuffer = Left$(lBuffer, lResult)
End If

' Returns the function
GetValue = Trim$(lBuffer)
End Function

 
Thanks sglugove,

This worked perfectly.
 
If you really want to check the environment variables, then you could also just do this:

?Environ("SomeVariableName")
 
This works too:

Dim SysName As String
Dim icount As Integer
While SysName <> &quot;&quot;
SysName = Environ(icount)

If UCase(Mid(SysName, 1, 15)) = &quot;ECU_DATA_SERVER&quot; Then
gServer = Mid(SysName, 17, Len(SysName))
ElseIf UCase(Mid(SysName, 1, 7)) = &quot;FILEDIR&quot; Then
gFileDir = Mid(SysName, 9, Len(SysName))
ElseIf UCase(Mid(SysName, 1, 9)) = &quot;PROD_STEP&quot; Then
gStep_Machine = Mid(SysName, 11, Len(SysName))
End If

icount = icount + 1
Wend
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top