Results 1 to 29 of 29

Thread: [RESOLVED] Add to Envoirment Path

  1. #1

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Resolved [RESOLVED] Add to Envoirment Path

    Dear All,
    How can add one path to Envoirnment path
    Now the path string contains "c:\temp" .Now i want to add one values so that it look like "c:\temp;c:\test"

    Dana
    Last edited by danasegarane; Jun 26th, 2007 at 04:42 AM.
    Please mark you thread resolved using the Thread Tools as shown

  2. #2
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Add to Envoirment Path

    Danasekar,

    You need to append your data to the path value inside [HKEY_LOCAL_MACHINE] or [HKEY_CURRENT_USER]\System\CurrentControlSet\Control\Session Manager\Environment
    Last edited by Fazi; Jun 26th, 2007 at 05:22 AM.

  3. #3

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Add to Envoirment Path

    How to append that ?
    Please mark you thread resolved using the Thread Tools as shown

  4. #4
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Add to Envoirment Path

    Danasegarane,

    you probably have used it before.

    You can first read the existing values inside the path using RegQueryValueEx API. Then append your string to the returned value and use RegSetValueEx API to save the new string back.

  5. #5

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Add to Envoirment Path

    I can able to get the value by this
    Code:
    MsgBox Environ$("Path")
    .But How to add
    Please mark you thread resolved using the Thread Tools as shown

  6. #6
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Add to Envoirment Path

    Dana,

    i think you can also use GetEnvironmentVariable API and SetEnvironmentVariable API's . it seems very easy that Registry API's.

    Here are 2 examples.

    Get Env. Variable.

    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

    Set Env. Varable.

    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 "CMD.EXE"
        'In the CMD windows type SET, you'll see the Environment Variable set, just for that session.
    End Sub
    Modify the above examples to suit to your needs.

  7. #7

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Add to Envoirment Path

    Dear Fazi,
    I alreay tried this examples.But this not adding to the registry entries in the path which is mentioned by you ?

    Dana
    Please mark you thread resolved using the Thread Tools as shown

  8. #8

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Add to Envoirment Path

    Any other methods ?
    Please mark you thread resolved using the Thread Tools as shown

  9. #9
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Add to Envoirment Path

    I'm not quite sure what you're trying to do here. Are you trying to write/read REG_EXPAND_SZ registry entries ? ie so that "C:\Windows\MyFile.txt" gets written to the registry as "%windir%\MyFile.txt" and read back as "C:\Windows\MyFile.txt" ?

  10. #10

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Add to Envoirment Path

    Dear SBD,
    I want to add here as attached.
    Last edited by danasegarane; Jun 27th, 2007 at 07:46 AM.
    Please mark you thread resolved using the Thread Tools as shown

  11. #11
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Add to Envoirment Path

    Dana,

    The best solution is to use #2 and #4

    Take you msdn and find the documentation for the 2 API's mensioned in #4
    or else search the Code Bank for Registry API

  12. #12

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Add to Envoirment Path

    Dear Fazi,
    I tried this method
    Code:
    Dim spath As String
    Dim newpath As String
    newpath = "c:\Pondy1"
    spath = GetString(HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "path")
    Dim sSplitstr() As String
    sSplitstr = Split(spath, ";")
    For i = LBound(sSplitstr) To UBound(sSplitstr)
        If StrComp(sSplitstr(i), newpath, vbTextCompare) = 0 Then
           GoTo Adds
        End If
    Next
    Adds:
         If Right(spath, 1) <> ";" Then
            spath = spath & ";"
         End If
         If Right(newpath, 1) <> ";" Then
            newpath = newpath & ";"
         End If
         spath = spath & newpath
       SaveString HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "path", spath
    
    End Sub
    this add in the registry path.But when i run some exe(placed in the path added to the registry path) from the run menu, it is exectuting the exe.But it has to execute the exe from run command in start menu

    Dana
    Please mark you thread resolved using the Thread Tools as shown

  13. #13
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Add to Envoirment Path

    can you please past here the complete strings you have in the path variable after using the above code?

  14. #14

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Add to Envoirment Path

    Code:
    C:\Program Files\PHP\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\UltraEdit;C:\Program Files\Microsoft SQL Server\80\Tools\BINN;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;C:\Program Files\Microsoft SQL Server\90\DTS\Binn\;C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies\;c:\Reports;
    Here In c:\Reports Fodler I have a exe called reports.exe.When i execute the reports.exe from the run menu it is not executing
    Please mark you thread resolved using the Thread Tools as shown

  15. #15

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Add to Envoirment Path

    The problem is that when I add from the mycomptuter properties menu, I can able to execute the Exe placed in the Reports Folder from the run menu.But when I added this value from and tested exectuting the exe from run menu.But just opened the folder not the exe.

    What could be the problem
    Please mark you thread resolved using the Thread Tools as shown

  16. #16
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Add to Envoirment Path

    dana,
    what happens if you directly goto regedit and add your variable to
    HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment

    will this work?

  17. #17

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Add to Envoirment Path

    Yes I can able to append the values to path varraible in the mentioned part in Registry
    Please mark you thread resolved using the Thread Tools as shown

  18. #18
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Add to Envoirment Path

    A possibility:- http://msdn2.microsoft.com/en-us/library/ms682653.aspx
    To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates. Note that the values of the environment variables listed in this key are limited to 1024 characters.
    Also, this might apply:- http://support.microsoft.com/kb/310519
    Changes to the system environment are written to the registry, and usually require a restart to become effective.

  19. #19
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Add to Envoirment Path

    SBD,

    Thanks for this information.

  20. #20

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Add to Envoirment Path

    Is this the api to be used ?

    Code:
    Private Declare Function BroadcastSystemMessage Lib "user32" (ByVal dw As Long, pdw As Long, ByVal un As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    what are all the parameters to be passed ?
    Please mark you thread resolved using the Thread Tools as shown

  21. #21
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Add to Envoirment Path

    · dwFlags
    Option flags. Can be a combination of the following values:
    BSF_FLUSHDISK
    Flush the disk after each recipient processes the message.
    BSF_FORCEIFHUNG
    Continue to broadcast the message, even if the time-out period elapses or one of the recipients is hung..
    BSF_IGNORECURRENTTASK
    Do not send the message to windows that belong to the current task. This prevents an application from receiving its own message.
    BSF_NOHANG
    Force a hung application to time out. If one of the recipients times out, do not continue broadcasting the message.
    BSF_NOTIMEOUTIFNOTHUNG
    Wait for a response to the message, as long as the recipient is not hung. Do not time out.
    BSF_POSTMESSAGE
    Post the message. Do not use in combination with BSF_QUERY.
    BSF_QUERY
    Send the message to one recipient at a time, sending to a subsequent recipient only if the current recipient returns TRUE.

    · lpdwRecipients
    Pointer to a variable that contains and receives information about the recipients of the message. The variable can be a combination of the following values:
    BSM_ALLCOMPONENTS
    Broadcast to all system components.
    BSM_ALLDESKTOPS
    Windows NT only: Broadcast to all desktops. Requires the SE_TCB_NAME privilege.
    BSM_APPLICATIONS
    Broadcast to applications.
    BSM_INSTALLABLEDRIVERS
    Windows 95: Broadcast to installable drivers.
    Windows NT: This value is not meaningful.
    BSM_NETDRIVER
    Windows 95: Broadcast to Windows-based network drivers.
    Windows NT: This value is not meaningful.
    BSM_VXDS
    Windows 95: Broadcast to all system-level device drivers.
    Windows NT: This value is not meaningful.

    When the function returns, this variable receives a combination of these values identifying which recipients actually received the message.
    If this parameter is NULL, the function broadcasts to all components.

    · uiMessage
    Identifier of the system message.

    · wParam
    32-bit message-specific value.

    · lParam
    32-bit message-

  22. #22
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Add to Envoirment Path

    Dana,

    dot you have API Guide?

  23. #23

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Add to Envoirment Path

    I used this one and got worked.
    Code:
    Const BSF_POSTMESSAGE = &H10
    Const BSM_APPLICATIONS = &H8
    Const WM_SYSCOMMAND = &H112
    Private Const BSF_NOHANG As Long = &H8
    Private Const WM_WININICHANGE As Long = &H1A
    Private Const SPI_SETDESKWALLPAPER As Long = 20
    Private Const WM_SETTINGCHANGE As Long = WM_WININICHANGE
    Private Declare Function BroadcastSystemMessage Lib "user32" (ByVal dw As Long, pdw As Long, ByVal un As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Private Sub EnvoirnmentVarriableChanged()
     
        Call BroadcastSystemMessage(BSF_NOHANG Or BSF_POSTMESSAGE, _
                                    BSM_APPLICATIONS, _
                                    WM_SETTINGCHANGE, _
                                    WM_SYSCOMMAND, _
                                    0)
                                    
    End Sub
    Is it correct ?
    Please mark you thread resolved using the Thread Tools as shown

  24. #24
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Add to Envoirment Path

    Dana,

    i did not check this function. lit busy. if this is working, then your Envi. Variable should work. check it. if you have API Guide, you should have a working example.

    Offtopic:- Dana, Be carefull of Environment Polution while adding your Environment Variable. Global Worming !!!

  25. #25

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Add to Envoirment Path

    Dear Fazi,
    I have API Guide .It is working. But I want to verify the parameters which are passed


    Dana
    Please mark you thread resolved using the Thread Tools as shown

  26. #26

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Add to Envoirment Path

    No It is Not Working.Some thing missing in the Message BroadCast
    Last edited by danasegarane; Jun 27th, 2007 at 08:40 AM.
    Please mark you thread resolved using the Thread Tools as shown

  27. #27
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Add to Envoirment Path

    Looking at the MS link (in post 18), I think you have to redifine the last parameter of BroadcastSystemMessage as a string and call with the string "Environment". ie: (I haven't tested this)

    Code:
    Private Declare Function BroadcastSystemMessage Lib "user32" _
                (ByVal dw As Long, _
                pdw As Long, _
                ByVal un As Long, _
                ByVal wParam As Long, _
                ByVal lParam As String) As Long
    
    Call BroadcastSystemMessage _
                (BSF_NOHANG Or BSF_POSTMESSAGE, _
                BSM_APPLICATIONS, _
                WM_SETTINGCHANGE, _
                WM_SYSCOMMAND, _
                "Environment")

  28. #28

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Add to Envoirment Path

    Thanks SBD,
    It is working now
    Please mark you thread resolved using the Thread Tools as shown

  29. #29
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Add to Envoirment Path

    You're welcome

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