CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2001
    Posts
    9

    Enviroment Variables

    Hi there..

    Is there any Windows API to set environment variables and they would also be viewable from the cmd.exe without restrating the system ??

    Regards

    Nabeel


  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Enviroment Variables

    Here are two examples taken from API-Guide. One to Set the other to Get the environment variables.

    private Declare Function SetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" (byval lpName as string, byval lpValue as string) as Long
    private Sub Form_Load()
    'This code was submitted by Brad McDonald ([email protected])
    SetEnvironmentVariable "API-Guide", "Rocks!"
    Shell "CMD.EXE"
    'In the CMD windows type set, you'll see the Environment Variable set, just for that session.
    End Sub
    '
    '
    '
    private Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" (byval lpName as string, byval lpBuffer as string, byval nSize as Long) as Long
    Function GetEnvironmentVar(sName as string) as string
    GetEnvironmentVar = string(255, 0)
    GetEnvironmentVariable sName, GetEnvironmentVar, len(GetEnvironmentVar)
    If InStr(1, GetEnvironmentVar, Chr$(0)) > 0 then GetEnvironmentVar = Left$(GetEnvironmentVar, InStr(1, GetEnvironmentVar, Chr$(0)) - 1)
    GetEnvironmentVar = sName + ": " + GetEnvironmentVar
    End Function
    private Sub Form_Load()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    me.AutoRedraw = true
    me.print GetEnvironmentVar("USERNAME")
    me.print GetEnvironmentVar("USERDOMAIN")
    me.print GetEnvironmentVar("PROCESSOR_IDENTIFIER")
    me.print GetEnvironmentVar("NUMBER_OF_PROCESSORS")
    me.print GetEnvironmentVar("OS")
    End Sub




    John G

  3. #3
    Join Date
    Sep 2001
    Posts
    9

    Re: Enviroment Variables

    Yes I have seen already seen this but that is for the current process, as long as the current process terminates, it also terminates.




  4. #4
    Join Date
    Feb 2000
    Posts
    71

    Re: Enviroment Variables

    This is a very nice piece of code, but it doesn't work for Win98 :-(

    Under WinNT 4 and Win2K it works very fine.

    Is there any solution to set an environment variable under Win98?

    Best regards

    Michael Hartmann


  5. #5
    Join Date
    Nov 2001
    Location
    USA, Florida
    Posts
    60

    Re: Enviroment Variables

    The environment variables sit in the follosing key path in the registry:
    HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
    You can make adjustments there via regular API calls programmatically, however unless you grant an update privilege to regular users, they will not be able to update those subkeys, only admins will.

    hope this helps...



  6. #6
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Enviroment Variables

    That will work under W98

    Code: Environ("<Param>")

    Returns a string, where <Param> can be any of the following:
    ALLUSERSPROFILE
    APPDATA
    CommonProgramFiles
    COMPUTERNAME
    ComSpec
    HOMEDRIVE
    HOMEPATH
    LOGONSERVER
    NUMBER_OF_PROCESSORS
    OS
    Os2LibPath
    Path
    PATHEXT
    PROCESSOR_ARCHITECTURE
    PROCESSOR_IDENTIFIER
    PROCESSOR_LEVEL
    PROCESSOR_REVISION
    ProgramFiles
    SystemDrive
    SystemRoot
    TEMP
    TMP
    USERDOMAIN
    USERNAME
    USERPROFILE
    windir

    If you wish to dump all of the environment variables, this simple piece
    of code will work:

    For i = 1 To 26
    Debug.Print Environ(i) & vbCrLf
    Next i


    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  7. #7
    Join Date
    Feb 2000
    Posts
    71

    Re: Enviroment Variables

    I want to SET environment variables, not GET environment variables. I know, that you can get some of the environment variables with the Environ function, but this is not what I want.

    I need to set/change environment variables for starting a 16 bit process with CreateProcess. In the MSDN-library I've found something, that you can create an environment block (???) and give a pointer to this block of strings (ABC=DEF[NULL]GHI=JKL[NULL][NULL]) as a parameter for CreateProcess.
    If this parameter is NULL the new process should inherit the environment of the calling process, but that's wrong for 16 bit processes (I seem). But also if you define a environment block and give a pointer to this, the 16 bit process doesn't know about that.

    Any help still wanted ....

    Michael Hartmann


  8. #8
    Join Date
    Feb 2000
    Posts
    71

    Re: Enviroment Variables

    This key only exists under Win2000, not Win98, so I don't have the chance to mainpulate this key. I thing there's no way to manipulate environment variables for 16 bit processes which are started with CreateProcess.

    Thanks for your help

    Michael Hartmann


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured