Results 1 to 8 of 8

Thread: DOS Environment Variables

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Posts
    1

    Question

    I am new to VB. I am building a utility that needs to grab a current environment variable (ie "CLIENTNAME"), manipulate that string and return a value back to "CLIENTNAME".

    I need a good resource or a code snippit that will interact with DOS Environment variables (NT OS specifically).

    Could it be as easy as utilizing %Clientname% within VB?

  2. #2
    Hyperactive Member
    Join Date
    Oct 2000
    Posts
    400
    MsgBox Environ("CLIENTNAME")

  3. #3
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362
    Originally posted by jmcswain
    MsgBox Environ("CLIENTNAME")
    But that only returns the variable, it doesn't allow you to set it.
    Donald Sy - VB (ab)user

  4. #4
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362
    craighope,
    I would suggest that instead of using the environment to store information that
    needs to be modified and restored, that you use the registry. This is what the regestry
    was intended to do.
    I just don't think that the environment can be changed easily from VB. Even if you
    shell to DOS and change an environment variable, the change is only in the current
    running MS-DOS prompt. Once that Window is closed, the variable is destroyed if
    created or reverts to the previous value if changed.
    Donald Sy - VB (ab)user

  5. #5
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    Perhaps he's trying to communicate with a dos program.
    Assuming that this is the case, you could use an Environmental variable to signify change to a file that both the dos program and your program read/writes to.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  6. #6
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362
    True, if VB would allow you to change the environment variable; which it doesn't(at least not in VB 5).

    Perhaps a search of the APIs may turn up something...

    ...ah, yes!

    Code:
    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 "command.com"
        'In the command windows type SET, you'll see the Environment Variable set, just for that session.
        'Didn't show it in my Window since it starts a new seesion - dsy5
    End Sub
    and

    Code:
    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
    But again, it only sets it for the current process.
    Last edited by dsy5; Mar 16th, 2001 at 09:25 PM.
    Donald Sy - VB (ab)user

  7. #7
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    Dammit he beat me

    are you sure about your "that task only" info? You didnt even create the shell until after you set the variable.

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

    Parameter Type/Description
    Code:
    lpName	String—The name of the environment variable to set. If an environment variable with this name does not yet exist, this function will create it.
    lpValue	String—The new value for the variable. NULL to clear an existing value (use the vbNullString constant to pass a null to this function).
    Last edited by Lord Orwell; Mar 16th, 2001 at 09:30 PM.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  8. #8
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362
    It seems that you couldn't communicate with a DOS program using this API technique.
    Since each time you run a DOS program it creates its own instance of DOS, the
    environment variable won't exist. Try this:

    In your autoexec.bat file add a line:
    Set TEST=junk

    save and reboot.

    Then start a new project, add a command button and put this code behind the form:

    Code:
    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
    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 Command1_Click()
        Me.AutoRedraw = True
        Me.Print GetEnvironmentVar("TEST")
    
    End Sub
    '
    Private Sub Form_Load()
        'KPD-Team 2000
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        Me.AutoRedraw = True
        Me.Print GetEnvironmentVar("TEST")
    
        'This code was submitted by Brad McDonald ([email protected])
        SetEnvironmentVariable "TEST", "Rocks!"
        Shell "Command.com"
        'In the CMD windows type SET, you'll see the Environment Variable set, just for that session.
    End Sub
    Now run it.

    In the MS-Dos Prompt box, type set and press return.
    You will see that when you shell to DOS, the environment variable doesn't
    change - but it does within the VB project for the entire time it is running.
    And if you just run it from the IDE, not making an EXE, it remains until VB is closed.
    So if you have a DOS program running, it can't see your change.
    Last edited by dsy5; Mar 16th, 2001 at 09:45 PM.
    Donald Sy - VB (ab)user

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