Jump to content

Recommended Posts

Posted

I'm setting a system environment variable by right clicking my computer, select properties, click Advanced tab, then click Environment Variables at the bottom.

I'm setting the variable PRODUCT=SOMETHING.

I then use:

$prod = envget("PRODUCT")

msgbox(0, "Results", $prod)

But $prod is always empty.

I also tried to set the variable then run this:

envupdate()

$prod = envget("PRODUCT")

msgbox(0, "Results", $prod)

$prod is still empty.

If I set the variable by using:

envset("PRODUCT", "SOMETHING")

then run the above lines, $prod is populated with SOMETHING as expected.

My question is, why doesn't envget see the variable when I set it manually via the System Environment variables window.

Is there a way to set a variable outside of an au3 script and still have envget work correctly?

Posted (edited)

The system environment is kind of a legacy thing, left over from the days of MS-DOS.

Some of those settings are volite. For instance, if you open a Command Prompt window and set a value to a new variable like this:

Set LikelySuitor=Tim

...then close that window, if you open the window again, and go

Set L

which should show all the environment setting whose variable names begin with L, there is no LikelySuitor variable anymore.

EnvSet is not very useful, as it won't set user-defined variables in the system environment. And you can only get some of the values in the system environment using EnvGet. These aren't very useful because the system environment is not very useful.

But if you have a legacy application you are trying to accomodate, there are solutions.

If you are trying to store information for other applications to read, try RegWrite and RegRead, but be careful with your registry - a key like "HKEY_CURRENT_USER\Software\Squirrely Business" should be safe to use.

These registry keys usually contain some of the environment variables, and you can use RegWrite on them, but the new values would only show up in the environment after one reboot:

"HKCU\Environment"

"HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"

; Author:   MsCreatoR
_SetEnvironment("MyCD", "C:\", 1, 0)
Func _SetEnvironment($Name, $Value, $Replace = 0, $KeyVal = 0)
    If $Name = "" Then Return SetError(1)
    If $Value = "" Then Return SetError(2)
    If $Replace <> 0 And $Replace <> 1 Then Return SetError(3)
    Local $ReadEnv
    Local $SystemRegKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
    Local $UserRegKey = "HKEY_CURRENT_USER\Environment"
    Local $RegKey = $SystemRegKey
    If $KeyVal = 1 Then $RegKey = $UserRegKey
    If $Replace = 1 Then
        RegWrite($RegKey, $Name, "REG_SZ", $Value)
    Else
        If RegRead($RegKey, $Name) = "" Then RegWrite($RegKey, $Name, "REG_SZ", $Value)
    EndIf
EndFunc;==>_SetEnvironment

But there are ways - I have seen them in the forums - to write to and read from the system environment if that is really your only choice of strategies. So if you search the forums this way "+environment +system" you will get lots of thread results.

What are you working with or on?

Edited by Squirrely1

Das Häschen benutzt Radar

Posted

Thanks for the info, this is exactly what I needed to know.

Thanks for giving me something productive to do.

And I am happy I understood well enough what you were trying to do - I sometime don't understand a guy's concern here at AutoIt and this thread is certain evidence of that fact.

Das Häschen benutzt Radar

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...