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

    access to environment variables

    Hi

    Does anyone know how to access dos-environment-variables in vb?

    I want to check if there´s a certain ocx in the
    "%windir%\system32"

    Thank you


  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: access to environment variables

    To get the system path, you can use the GetSystemDirectory API

    private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (byval lpBuffer as string, byval nSize as Long) as Long

    private Sub Form_Load()

    Dim strPath as string
    strPath = Space(255)

    GetSystemDirectory strPath, 255

    MsgBox strPath

    End Sub




    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Sep 2001
    Posts
    8

    Re: access to environment variables

    Hi
    Thanks for your answer.
    But i still got a little problem.
    Using your code i´ve got "c:\winnt\system32"
    in strPath. The next thing i want to do is to check if thers a mswinsck.ocx in that directory.
    I tried it the following way:

    dim d,x as string

    x=strPath+"\mswinsck.ocx"
    d=dir(x)
    if(d="mswinsck.ocx")then
    ...
    end if

    This doesnt work. The problem is that
    d=strPath+"\mswinsck.ocx"
    doesnt work. d still only contains "c:\winnt\system32".
    I guess this is because of all the spaces at the end of strPath, but i dont know how to fix that
    (i already tried strPath=trim(strPath)).

    Thank you

    @Cakkie
    PS: I´ve also send you sent you this message as a private message by mistake. Sorry for that.


  4. #4
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: access to environment variables

    I already replied to you private message

    use & in stead of +

    x = strPath & "\mswinsock.ocx"

    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  5. #5
    Join Date
    Sep 2001
    Posts
    8

    Re: access to environment variables

    Still doesnt work.

    I can add a string in front of srtPath
    but not on the end of it.
    x="xxx" & strPath works
    x=strPath & "xxx" doesnt

    ???



  6. #6
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: access to environment variables

    I reviewed my code and discovered a bug. The GetSystemDirectory function returns a null-terminated string. this is why you cannot append, cause null and something = null.
    This should fix the problem. Place this line after the call to GetSystemDirectory

    strPath = Left(strPath, len(Trim(strPath)) - 1)




    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

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

    Re: access to environment variables

    Thats not really a bug. Most, if not all APIs that handle strings, Fill the space allocated for strings with nulls to avoid left over garbage in the data area for the string.

    John G

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

    Re: access to environment variables

    Try this to read DOS environment

    Private Sub Command1_Click()
    Dim Env As String
    Dim x As Integer
    Dim t As String

    x = 1
    Env = Environ(x)

    Do Until Env = ""
    Env = Environ(x)
    Print t; Env
    x = x + 1
    Loop

    End Sub


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

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

    Re: access to environment variables

    Also try this

    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]

  10. #10
    Join Date
    Feb 2002
    Posts
    1

    Re: access to environment variables


    How do you go about setting environment variables from VB? Does environ have a matching function to do this?


    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

    If the only tool you have is a hammer you will
    tend to see all problems as nails

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