Results 1 to 15 of 15

Thread: environ

  1. #1
    skaizer
    Guest

    environ


    I'm trying to understand how to work with "environ" in Visual Basic 6 and i can't figure it out.
    Please if someone can write me some explanation or some part of code that contained the "environ" issue.

    TNX

  2. #2
    Tygur
    Guest
    To show what's in the Path environment variable:
    MsgBox Environ("path")

    What's the problem?

  3. #3
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Hi there Tygur,
    do u now if there are any way of setting Environment vars from winthin VB ?
    -= a peet post =-

  4. #4
    jim mcnamara
    Guest
    Code:
    Private Declare Function SetEnvironmentVariable Lib "kernel32" _
    Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Long
    Private Sub Form_Load()
        
        SetEnvironmentVariable "TEST", "UGH"
        Shell "CMD.EXE"
        'Now type SET, you'll see the Environment Variable set
        ' lasts just for this process, goes away when the .EXE stops.
    End Sub

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    thanks jim mcnamara, but (there is always a but )
    it does not seem to do the job in win2k ?
    -= a peet post =-

  6. #6
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    oops.. sorry its working... I looked in here :



    not in running the SET command (as it said I should )

    assume it will be in the props panel after I reboot

    thanks jim
    Last edited by peet; Jan 17th, 2004 at 05:05 AM.
    -= a peet post =-

  7. #7
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    okokok.. I know... I'd better goto bed

    didnt read this either
    ' lasts just for this process, goes away when the .EXE stops
    any suggestions on how to make it stay there ?
    -= a peet post =-

  8. #8
    Tygur
    Guest
    There is one problem with setting environment variables from within VB. If you set an environment variable using the SetEnvironmentVariable API, the changes don't show up when you try using Environ. So I've made a function that can be used in place of Environ so you can also get the changes that were made since your app started:
    VB Code:
    1. 'This is the API declaration
    2. Private Declare Function GetEnvironmentVariable Lib "kernel32" _
    3.     Alias "GetEnvironmentVariableA" (ByVal lpName As String, _
    4.     ByVal lpBuffer As String, ByVal nSize As Long) As Long
    5.  
    6. 'Uses API to get an environment variable
    7. 'This is necessary because the Environ function will not detect changes to the environment vars
    8. Function EnvironAPI(sString As String) As String
    9. Dim EnvVar As String
    10. Dim lLength As Long
    11.  
    12. EnvVar = Space(256)
    13. lLength = GetEnvironmentVariable(sString, EnvVar, 256)
    14.  
    15. 'If the environment variable is longer than 256 characters, we didn't get the whole thing yet
    16. If lLength > 256 Then
    17.     EnvVar = Space(lLength)
    18.     lLength = GetEnvironmentVariable(sString, EnvVar, lLength)
    19. End If
    20.  
    21. EnvVar = Left(EnvVar, lLength)
    22. EnvironAPI = EnvVar
    23. End Function

  9. #9
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629

    Thumbs up

    Thanks
    -= a peet post =-

  10. #10
    Junior Member
    Join Date
    Jul 2001
    Posts
    19
    Originally posted by Tygur
    So I've made a function that can be used in place of Environ so you can also get the changes that were made since your app started:

    LIES!!!!!!

    Looks RATHER like the code here with very MINOR changes: http://www.vbapi.com/ref/g/getenvironmentvariable.html
    [H]

  11. #11
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    On defence of Tygur, the code on that site isnt in a function, its in the code for a commandbutton. So Tygur did make the function, even though he may not have written the code
    You just proved that sig advertisements work.

  12. #12
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by sbrown23



    LIES!!!!!!

    Looks RATHER like the code here with very MINOR changes: http://www.vbapi.com/ref/g/getenvironmentvariable.html
    -= a peet post =-

  13. #13
    Tygur
    Guest
    Originally posted by sbrown23



    LIES!!!!!!

    Looks RATHER like the code here with very MINOR changes: http://www.vbapi.com/ref/g/getenvironmentvariable.html
    Well, I hate to bust your bubble, but I really did write that function I posted and the code within it. For one thing, I never go to vbapi.com for api help. I always refer to the source of this information, the MSDN Library.

    I've been using VB since 1997. You'd think I ought to know how to use such a simple API function by now

    The reason why the codes look so similar is because there are only so many ways of calling such a simple API function, and they are all quite similar. Can you come up with a better way? I certainly came up with a better way than whoever wrote that code at vbapi.com. Read further.

    Is your problem the length I chose for the buffer (256)? 256 is a very common number for buffer lengths when people are making API calls. You'll find hundreds, maybe thousands of codes out there for api calls like GetWindowText and GetClassName that send strings with a length of 255 or 256. Everybody does it.

    Also, tell me. If you wanted to abreviate "Envorironment Variable", how would you do it? I'm sure most people would agree that "EnvVar" is quite common. I doubt the vbapi.com people trademarked the name or anything And if you're going to bother me about the names of my length variable, you've really got issues

    That code is not similar to the code at vbapi.com, anyway. Look closer. If the environment variable is longer than 256 characters, my function up there will handle that and still return the whole thing. The code at vbapi.com does not. In fact, if the environment variable is longer than 256 characters long, the code at vbapi.com will not even retrieve it. Something like that is simply not acceptable for any code I make.


    So, why did you think I copied that code, again?

  14. #14
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    run for cover sbrown23... the tiger is awake!!

    -= a peet post =-

  15. #15
    Lively Member
    Join Date
    Jul 2000
    Location
    Lost in the Mojave Desert
    Posts
    82
    slam, heheheheheheheheheheheheheheheheheheheheheheheheheheheheheheheheheheheheheheheheheheheh

    The API.com code looks different alot.

    Like he's the originator of coding this particular API call?
    Just look below!~
    Last edited by Bam_BamIR; Jul 15th, 2002 at 11:21 PM.
    Don't go away mad, just go away.

    Bam-Bam

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