How To define operating system environment variable ?

Post Reply
User avatar
vilian
Posts: 1011
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Has thanked: 3 times
Been thanked: 1 time
Contact:

How To define operating system environment variable ?

Post by vilian »

Hi,

I need to define through the program, an operating system environment variable to be seen by all the applications. He is possible?

Already I obtained to define with the following program. But the program that created only obtains to see the variable. It would like that all the programs in saw it to execution.

Code: Select all | Expand


SetEnvironmentVariable("VFAPORTAL","c:\teste ")

#pragma BEGINDUMP

#include <windows.h>

HB_FUNC( SETENVIRONMENTVARIABLE )
{
   hb_retl( SetEnvironmentVariableA( (LPCSTR) hb_parcx( 1 ),
                                     (LPCSTR) hb_parcx( 2 )
                                     ) ) ;
}

#pragma ENDDUMP

Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
Antonio Linares
Site Admin
Posts: 42777
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 116 times
Been thanked: 110 times
Contact:

Re: How To define operating system environment variable ?

Post by Antonio Linares »

Vilian,

SetEnvironmentVariable()
"This function has no effect on the system environment variables or the environment variables of other processes."
http://msdn.microsoft.com/en-us/library/ms686206(VS.85).aspx

But you can do it this way:
"Calling SetEnvironmentVariable has no effect on the system environment variables. 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."
http://msdn.microsoft.com/en-us/library/ms682653(VS.85).aspx
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
vilian
Posts: 1011
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Has thanked: 3 times
Been thanked: 1 time
Contact:

Re: How To define operating system environment variable ?

Post by vilian »

Antonio,

I created in the register of windows ( HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment ) the following entrance: VFAPORTAL.

Later I try to modify its content with the following program:

Code: Select all | Expand

#define WM_SETTINGCHANGE     26

SendMessage(-1,WM_SETTINGCHANGE,"VFAPORTAL","TESTE3")
 


But nothing it happens, what I am making made a mistake?
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
vilian
Posts: 1011
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Has thanked: 3 times
Been thanked: 1 time
Contact:

Re: How To define operating system environment variable ?

Post by vilian »

Up
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
Antonio Linares
Site Admin
Posts: 42777
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 116 times
Been thanked: 110 times
Contact:

Re: How To define operating system environment variable ?

Post by Antonio Linares »

Vilian,

Try to use 0xFFFF instead of -1
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 42777
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 116 times
Been thanked: 110 times
Contact:

Re: How To define operating system environment variable ?

Post by Antonio Linares »

Also use "Environment" instead of "TESTE3"
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 42777
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 116 times
Been thanked: 110 times
Contact:

Re: How To define operating system environment variable ?

Post by Antonio Linares »

Vilian,

Finally, use 0 (zero) instead of "VFAPORTAL" when calling SendMessage()
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
vilian
Posts: 1011
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Has thanked: 3 times
Been thanked: 1 time
Contact:

Re: How To define operating system environment variable ?

Post by vilian »

Antonio,

I did not understand. The command would be thus then?

SendMessage(0xFFFF,WM_SETTINGCHANGE,0,"Environment")

But as I define that changeable it goes to be modified and which the new value?
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
Antonio Linares
Site Admin
Posts: 42777
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 116 times
Been thanked: 110 times
Contact:

Re: How To define operating system environment variable ?

Post by Antonio Linares »

Vilian,

> SendMessage(0xFFFF,WM_SETTINGCHANGE,0,"Environment")

Yes, that is what I meant

> But as I define that changeable it goes to be modified and which the new value?

You set the new value in the registry, then notify all the windows about such change.

It should work that way
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
vilian
Posts: 1011
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Has thanked: 3 times
Been thanked: 1 time
Contact:

Re: How To define operating system environment variable ?

Post by vilian »

Antonio,

grateful for the help.

But as sending this value for the registry?
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
Antonio Linares
Site Admin
Posts: 42777
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 116 times
Been thanked: 110 times
Contact:

Re: How To define operating system environment variable ?

Post by Antonio Linares »

Vilian,

Please review FWH\samples\testrg32.prg
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
vilian
Posts: 1011
Joined: Wed Nov 09, 2005 2:17 am
Location: Brazil
Has thanked: 3 times
Been thanked: 1 time
Contact:

Re: How To define operating system environment variable ?

Post by vilian »

Antonio,

I made thus:

Code: Select all | Expand

#include "FiveWin.Ch"

#define  WM_SETTINGCHANGE        26
#define  HKEY_LOCAL_MACHINE      2147483650

function Main()

   LOCAL oReg, cName, uVar

   oReg := TReg32():New( HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Control\Session Manager\Environment",.t. )

   oReg:Set( "VFAPORTAL", "SIF212" )
   oReg:Close()

   SendMessage(0xFFFF,WM_SETTINGCHANGE,0,"Environment")
   SysRefresh()

   ? GetEnv("VFAPORTAL")

return nil


But the GetEnv continues returning the old value !
Seeing through the RegEdit I confirm the change in the value. but GetEnv continues returning me the old one. Why?
Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
User avatar
Antonio Linares
Site Admin
Posts: 42777
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 116 times
Been thanked: 110 times
Contact:

Re: How To define operating system environment variable ?

Post by Antonio Linares »

Vilian,

Have you tried to close and restart the application after such change ?
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply