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!

Are there API calls to read and/or create Environment Variables 2

Status
Not open for further replies.

SBendBuckeye

Programmer
May 22, 2002
2,166
US
I can use the Environ function in VB to read a value, but are there API calls to read and/or create them?

Thanks for any help you can give me!

Have a great day!

j2consulting@yahoo.com
 
Try these.

SetEnvironmentVariable
GetEnvironmentVariable


 
Thanks, since I don't have easy access to the tech-net CD or MSDN, can someone post the Declare statement?

Have a great day!

j2consulting@yahoo.com
 
Private Declare Function SetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Long
Private Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long
 
Thanks, Doc. Enjoy your star!

Have a great day!

j2consulting@yahoo.com
 
Hello, I wrote the following code to Get and Set environment variables using API calls. The Set piece doesn't appear to work. It almost acts like its reading a temporary copy of something. I tried it as a user with admin rights and as an administrator on the local machine.

It acts like it works, but it never shows up in the list in System Environment variables or in the old DOS set.

Are these APIs obsolete?

Code:
Option Explicit

Private Declare Function SetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Long

Private Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long

Private mstrNullChar As String
Private Const BufferSize As Long = 255

Public Function GetEnvironmentVar(ByVal EnvironVar As String) As String
Dim lngSize As Long
Dim lngReturn As Long
Dim strBuffer As String
    mstrNullChar = Chr$(0)
    EnvironVar = Trim$(EnvironVar) & mstrNullChar
    strBuffer = Space$(BufferSize) & mstrNullChar
    lngReturn = GetEnvironmentVariable(EnvironVar, strBuffer, BufferSize)
    If lngReturn Then
        GetEnvironmentVar = Trim$(Replace$(strBuffer, mstrNullChar, vbNullString)) 'Strip Chr$(0)
    End If
End Function 'GetEnvironmentVar

Public Function SetEnvironmentVar(ByVal EnvironVar As String, ByVal VarValue As String) As String
Dim lngSize As Long
Dim lngReturn As Long
Dim strTemp As String
Dim strBuffer As String
Dim strMessage As String
    mstrNullChar = Chr$(0)
    strMessage = "Environment Variable: " & Trim$(EnvironVar) & " - already exists with" & vbCrLf & _
                "Value: "
    EnvironVar = Trim$(EnvironVar) & mstrNullChar
    strBuffer = Space$(BufferSize) & mstrNullChar
    VarValue = Trim$(VarValue)
    lngReturn = GetEnvironmentVariable(EnvironVar, strBuffer, BufferSize)
    If lngReturn Then
        strTemp = Trim$(Replace$(strBuffer, mstrNullChar, vbNullString)) 'Strip Chr$(0)
        EnvironVar = Replace$(EnvironVar, mstrNullChar, vbNullString)
        strMessage = strMessage & strTemp
        If UCase$(strTemp) = UCase$(VarValue) Then
            MsgBox strMessage, vbInformation, "SetEnvironmentVar"
            Exit Function
        Else
            strMessage = Replace$(strMessage, "Value:", "Old Value:")
            strMessage = strMessage & " - Change to" & vbCrLf & "New Value: " & VarValue
            If vbOK = MsgBox(strMessage, vbOKCancel + vbInformation + vbDefaultButton2, "SetEnvironmentVar") Then
                EnvironVar = Trim$(EnvironVar) & mstrNullChar 'Reset during Get above
            Else
                MsgBox "Operation Cancelled", vbInformation, "SetEnvironmentVar"
                Exit Function
            End If
        End If
    End If
    strBuffer = Trim$(VarValue) & mstrNullChar
    lngReturn = SetEnvironmentVariable(EnvironVar, strBuffer)
    If lngReturn Then
        SetEnvironmentVar = Trim$(Replace$(strBuffer, mstrNullChar, vbNullString)) 'Strip Chr$(0)
    Else
        MsgBox "Operation Failed"
    End If
End Function 'SetEnvironmentVar


Have a great day!

j2consulting@yahoo.com
 
I'm afraid that SetEnvironmentVariable only sets the specified variable for the calling process. It does not affect System environment variables nor the environment variables of other processes
 
Thanks, is there one that does system environment variables. I'm trying to put together a process that would save having to do it manually.


Have a great day!

j2consulting@yahoo.com
 
Sure - here's the way MS advise doing it:

Add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message
 
Thanks for the help! Unfortunately, I am fairly new to APIs and haven't tinkered with the Registry at all, so unless you have some VB code laying around that does what you suggested I am out of luck. Its no big deal, I was just trying to automate some setup tasks which can still be done the old way without a problem.


Have a great day!

j2consulting@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top