Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I know about and use GetEnv() all t 1

Status
Not open for further replies.
Feb 14, 2000
425
US
I know about and use GetEnv() all the time, now I have the need to programatically SET a Environment String. does anyone know of any API call to "Set an environment variable" ?

only need win2k and above solution.

[bigears]Thank you in advance[bigears]

Steve Bowman
steve.bowman@ultraex.com

 
Typically, it is done with the DOS SET command.

However, I tried it in a batch file and using all 3 methods in aircons FAQ. The problem, I believe, is that it does not retain the SET.

I thought I would throw this out so others know this is not as simple as it may first appear.

Jim Osieczonek
Delta Business Group, LLC
 
Environment variables are stored in the registry under H_KEY_CURRENT_USER/Environment. You can read and write to the registry from within VFP, and there's a registry.prg that comes with VFP in the Samples directory that shows how to do it.



-BP
 
steveb7

An alternative approach, especially if you want to keep it simple, would be to create a text file instead.

SET TEXTMERGE ON TO ADDBS(GETENV([TEMP])) ;
[tab]+ environstring.txt
\\<<variablename>>
SET TEXTMERGE OFF
SET TEXTMERGE TO

There are various ways to recover the info from the text file and it also allows the info to be shared with a variety of different Windows applications where you may not know the right code to recover the info from the environment.

FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Jim,

The problem, I believe, is that it does not retain the SET

That's right. It's because the SET is scoped to the DOS session which creates it. As soon as the RUN command finishes, the DOS session is closed and the setting is lost.

Mike


Mike Lewis
Edinburgh, Scotland
 
Hi Steveb7,

Jimoo is correct. Using DOS SET or using SetEnvironmentVariable API does not retain it's setting. But you can use API and inherit the new environment to the child process using CreateProcess API.

If you want to change system (global) environment, you must run &quot;Control Panel->System&quot; or edit the registry programmatically (as BPeisch already suggested)

I don't have an example for this. But you can look here:

C++ :
VB : thread713-675968

-- AirCon --
 
Mike,

Arghh.. You were faster than me :-D


-- AirCon --
 
As always, execlent input. I knew this would not be straight forward. I did know about the scope issues, me and dos go way back.

What I have is a DLL file that was written in C so I can pass data back and forth between me win2kServer and a Unix box deamon via sockets the DLL looks for an Environment String to detirmine several bits of info like the port,clientIP,serverIP..... I would not have written it this way but this is the way it is so I was attempting to reduce setup issues by having my wrapper class around the DLL first look for the Environment String and create it if needed before DECLAREing the DLL.

The way I think I will handle this is by using the registry entry method, after some testing I do see that a reboot of the win2k server will be required post creation and I will document [bigsmile] the need for the EnvString this solution will work, just not as clean as &quot;on the fly&quot;. The root issue is the whole IN SCOPE thing. Even VFP inherets the environment only at startup start VFP create a new environment string via System Applett then try to read it with GETENV() it's not there quit and restart it is there Thanks to all

Steve Bowman
steve.bowman@ultraex.com

 
Good feedback from the group. This was a great thread. I'd like to make one other note.

Of course, I don't know the reason why you want to update the environment variables programmatically, but unless there is a reason you must I'd suggest using an INI file. They are easy to create and easy to view or edit if you must.

I have one system in particular, that each user has their own ini file (based on user id) and there is a system ini that they all share as well. This works out very well.

I believe there are some FAQ on reading / writing INIs, but if not - let me know and I'll post them.



Jim Osieczonek
Delta Business Group, LLC
 
the DLL I am DECLAIRing is looking for the environment, I have no control over how this DLL works thus the WANT not NEED for a programatic solution to environment modification &quot;on the fly&quot;. More precisiely the DLL reads an ini file for the info it needs but uses the environment to know the path to the ini [bluegreedy]

Steve Bowman
steve.bowman@ultraex.com

 
Steve,

Sorry I didn't say this from the first, because I didn't know what do you want to achieve with it.

So, based on your last explanation, I suggest you try SetEnvironmentVariable. But it depends on how the DLL read the environment. It will work if the function use GetEnvironmentVariable API to read the environment. If it is using other technique, then you are out of luck :)

Try this:
--------------
Declare Long SetEnvironmentVariable in Kernel32 String cName, String cValue

Declare Long GetEnvironmentVariable in Kernel32 ;
String cName, String @cBuffer, Long nBufSize

SetEnvironmentVariable('NewEnv', 'Test')
?GetEnv('NewEnv')
cBuffer = replicate(chr(0), 255) && give enough buffer
GetEnvironmentVariable('NewEnv', @cBuffer, 32)
?cBuffer
SetEnvironmentVariable('NewEnv', Null) && Reset
--------------

-- AirCon --
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top