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
Printable View
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
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
How to append that ?
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.
I can able to get the value by this.But How to addCode:MsgBox Environ$("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.
Modify the above examples to suit to your needs.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
Dear Fazi,
I alreay tried this examples.But this not adding to the registry entries in the path which is mentioned by you ?
Dana
Any other methods ?
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" ?
Dear SBD,
I want to add here as attached.
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
Dear Fazi,
I tried this method
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 menuCode: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
Dana
can you please past here the complete strings you have in the path variable after using the above code?
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 :)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;
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
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?
Yes I can able to append the values to path varraible in the mentioned part in Registry
A possibility:- http://msdn2.microsoft.com/en-us/library/ms682653.aspxAlso, this might apply:- http://support.microsoft.com/kb/310519Quote:
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.
Quote:
Changes to the system environment are written to the registry, and usually require a restart to become effective.
SBD,
Thanks for this information.
Is this the api to be used ?
what are all the parameters to be passed ?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
· 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-
Dana,
dot you have API Guide?
I used this one and got worked.
Is it correct ?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
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 !!! :D
Dear Fazi,
I have API Guide .It is working. But I want to verify the parameters which are passed :)
Dana
No It is Not Working.Some thing missing in the Message BroadCast
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")
Thanks SBD,
It is working now :)
You're welcome :wave: