Results 1 to 6 of 6

Thread: Change Environ using SetEnv..Var.A

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Far East
    Posts
    232

    Change Environ using SetEnv..Var.A

    Hi forum members

    I am trying to add an extra folder eg C:\new to the existing path in the Environment variables using VB6.

    I tried the following:

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

    Now_path$ = Environ("Path")
    SetEnvironVar "path", Now_Path$ & ";C:\new"

    but it did not work. Where did i go wrong? Help appreciated.

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Change Environ using SetEnv..Var.A

    What version of Windows are you using? If it's Vista or 7, with UAC on I think you will need admin rights for that (run application as admin).

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Far East
    Posts
    232

    Re: Change Environ using SetEnv..Var.A

    Hello Yu

    I was tinkering in xpwin.sp2 using vb6.sp5 as administrator. I came to the conclusion that the path is not changed when i look at the MyPc>properties>Environment Vars. By the way using Shell "cmd set path = ..new.." it will change the path temporary for that instance of cmd and will revert to the old path after closing that instance of cmd.

    Hope u can throw some light on the setEnv function. Cheers!
    Last edited by FuzMic; Jul 16th, 2010 at 02:21 AM.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Change Environ using SetEnv..Var.A

    i believe it will only set for the instance your program runs in
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Change Environ using SetEnv..Var.A

    Applications are not supposed to do this at runtime anyway. It is an installer function.

    If you do this programmatically you risk adding your path to the paths list once for every run. Even Adobe did this with Reader one release recently and they got flamed nastily for doing it.

    If you package with VSI 1.1 or another tool for creating MSI (Microsoft Installer) packages you can edit the Environment Table with Orca to include your path during installation and remove it upon uninstall.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Far East
    Posts
    232

    Re: Change Environ using SetEnv..Var.A

    Thanks all of you for the response. As dil.. already said, if we can access the PC we can change the environ without resorting to an Application. Cheers!

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