The Environment Block of a process is a copy of the environment block of the parent process, or a new, default Environment Block (depending how you spawn the child process).
In this case, the Environment Block your FreeBasic Application receives is a copy of the Environment Block as it exists in the spawning CMD console.
However, because of this, changes will not persist back to the parent processes environment.
With Older MS-DOS programs this wasn't an issue since... it didn't exist. The concept of multiple processes was a foreign one, and there really was only one environment block for each command interpreter that was globally accessible to any applications run within.
Instead you have to write a value to the registry. (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment).
This does that (can be confirmed be via RegEdit) but changes don't take effect until a reboot... (Even though I Broadcast the required WM_SETTINGSCHANGE. Oh well.
#include once "windows.bi"
'Windows Master Registry Block is stored in the registry,
'at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" as REG_SZ's
Declare Function SetEnvironment(VarName As String, VarValue As String) As Boolean
Const WM_SETTINGSCHANGE = &H1A
Function SetEnvironment(VarName As String,VarValue As String) As Boolean
Dim UseRoot as HKEY,usekey As String
Dim hk as HKEY
Dim returnvalue As Boolean
returnvalue=False
UseRoot=HKEY_LOCAL_MACHINE
UseKey= "SYSTEM\CurrentControlSet\Control\Session Manager\Environment\"
'Open the key...
If RegOpenKeyEx(UseRoot,UseKey,0,KEY_ALL_ACCESS,@hk) = ERROR_SUCCESS Then
'hk is a pointer to the key, use it with RegSetValueEx()...
SetLastError(0)
if RegSetValueEx(hk,StrPtr(VarName),0,REG_SZ,StrPtr(VarValue),Len(VarValue)+1)<>ERROR_SUCCESS Then
Print "RegSetValueEx Failed." + "GetLastError returns " + Str(GetLastError())
End If
RegCloseKey(hk)
Else
Print "RegOpenKeyEx Failed. GetLastError() returns " + Str(GetLastError())
End If
PostMessage HWND_BROADCAST,WM_SETTINGSCHANGE,0,StrPtr("Environment")
return returnvalue
End Function
SetEnvironment("Testing","howdy")