Page 1 of 1

How To define operating system environment variable ?

PostPosted: Wed Jan 14, 2009 6:03 pm
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 view

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


Re: How To define operating system environment variable ?

PostPosted: Thu Jan 15, 2009 1:16 am
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

Re: How To define operating system environment variable ?

PostPosted: Thu Jan 15, 2009 1:21 pm
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 view
#define WM_SETTINGCHANGE     26

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


But nothing it happens, what I am making made a mistake?

Re: How To define operating system environment variable ?

PostPosted: Sat Jan 17, 2009 1:01 pm
by vilian
Up

Re: How To define operating system environment variable ?

PostPosted: Sat Jan 17, 2009 4:10 pm
by Antonio Linares
Vilian,

Try to use 0xFFFF instead of -1

Re: How To define operating system environment variable ?

PostPosted: Sat Jan 17, 2009 4:12 pm
by Antonio Linares
Also use "Environment" instead of "TESTE3"

Re: How To define operating system environment variable ?

PostPosted: Sat Jan 17, 2009 4:23 pm
by Antonio Linares
Vilian,

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

Re: How To define operating system environment variable ?

PostPosted: Tue Jan 20, 2009 1:47 pm
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?

Re: How To define operating system environment variable ?

PostPosted: Tue Jan 20, 2009 6:31 pm
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

Re: How To define operating system environment variable ?

PostPosted: Tue Jan 20, 2009 8:01 pm
by vilian
Antonio,

grateful for the help.

But as sending this value for the registry?

Re: How To define operating system environment variable ?

PostPosted: Tue Jan 20, 2009 10:50 pm
by Antonio Linares
Vilian,

Please review FWH\samples\testrg32.prg

Re: How To define operating system environment variable ?

PostPosted: Wed Jan 21, 2009 1:30 pm
by vilian
Antonio,

I made thus:

Code: Select all  Expand view
#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?

Re: How To define operating system environment variable ?

PostPosted: Mon Jan 26, 2009 9:20 am
by Antonio Linares
Vilian,

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