Setting an environment variable

forum.sourcecode (2000-2005) & forum.td.sourcecode (2005-2010)
Philip Hautekiet

Setting an environment variable

Post by Philip Hautekiet » 27 May 2008, 12:54

 Posted by:  Philip Hautekiet 


The function to get the value of an environment variable is :
VisDosGetEnvString.

Is there a (visual toolchest) function to SET the value of an environment
variable?

Thanks in advance.

Christof Helm

Re: Setting an environment variable

Post by Christof Helm » 28 May 2008, 08:42

 Posted by:  Christof Helm 

You can use

BOOL SetEnvironmentVariable( LPCTSTR lpName, LPCTSTR lpValue )

from kernel32.dll. I don't know why VT doesn't provide something like this.

Christof

Philip Hautekiet

Re: Setting an environment variable

Post by Philip Hautekiet » 28 May 2008, 09:41

 Posted by:  Philip Hautekiet 

I tried this function.

!!CB!! 145
Function: SetEnvironmentVariableA
Description: BOOL SetEnvironmentVariableA( LPCSTR lpName, LPCSTR lpValue );
Export Ordinal: 0
Returns
Boolean: BOOL
Parameters
String: LPCSTR
String: LPCSTR

After the function call, the environment variable can be retrieved within
the appliction with VisDosGetEnvString('VARX')

Problem :

The environment variable is only available within the Centura
application.
In Control Panel (or in a dos box) the variable doesn't exist.

Is it possible to create an everywhere known eternal system variable?

____________________

Lumir Paul

Re: Setting an environment variable

Post by Lumir Paul » 28 May 2008, 10:49

 Posted by:  Lumir Paul 

Hi Philip,

I think that the explanation can be found in the MSDN - from the following
feature.

Every process has an environment block that contains a set of environment
variables and their values.....

*Setting it*
BOOL SetEnvironmentVariable(
LPCTSTR lpName -> pointer to a null-terminated string that specifies the
name of the environment variable
LPCTSTR lpValue -> pointer to a null-terminated string that specifies the
contents of the environment variable (max. 32,767 chars)
)
SetEnvironmentVariable function sets the contents of the specified
environment variable for the current process only !!!
This function has no effect on the system environment variables or the
environment variables of other processes !!!

*Getting it*
LPVOID GetEnvironmentStrings(void)
GetEnvironmentStrings function retrieves the environment variables for the
current process.
If the function succeeds, the return value is a pointer to the environment
block of the current process.
If the function fails, the return value is NULL

DWORD GetEnvironmentVariable(
LPCTSTR lpName -> pointer to a null-terminated string that specifies the
name of the environment variable.
LPTSTR lpBuffer -> pointer to a buffer that receives the contents of the
specified environment variable (max. 32,767 chars)
DWORD nSize -> size of the buffer pointed to by the lpBuffer parameter
)
GetEnvironmentVariable function retrieves the contents of the specified
variable from the environment block of the calling process.
The contents are in the form of a null-terminated string of characters.
If the function succeeds, the return value is the number of chars stored
into the buffer pointed to by lpBuffer
If the function fails, the return value is zero

The GetEnvironmentStrings function returns a pointer to the environment
block of the calling process.
This should be treated as a read-only block; do not modify it directly.
Instead, use the SetEnvironmentVariable function to change an environment
variable.
When you are finished with the environment block obtained from
GetEnvironmentStrings, call the FreeEnvironmentStrings function to free the
block.

Calling SetEnvironmentVariable has no effect on the system environment
variables.
The user can add or modify system environment variables using the Control
Panel. 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. This allows applications, such as
the shell, to pick up your updates.

The GetEnvironmentVariable function determines whether a specified variable
is defined in the environment of the calling process, and, if so, what its
value is.

Regards, have a nice day.

Lumir Paul
paul@mikropost.cz


Return to “td.sourcecode”

Who is online

Users browsing this forum: [Ccbot] and 2 guests