FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour How To define operating system environment variable ?
Posts: 1067
Joined: Wed Nov 09, 2005 02:17 AM

How To define operating system environment variable ?

Posted: Wed Jan 14, 2009 06:03 PM
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.

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
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Re: How To define operating system environment variable ?

Posted: Thu Jan 15, 2009 01:16 AM

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
Posts: 1067
Joined: Wed Nov 09, 2005 02:17 AM

Re: How To define operating system environment variable ?

Posted: Thu Jan 15, 2009 01:21 PM
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:

#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
Posts: 1067
Joined: Wed Nov 09, 2005 02:17 AM

Re: How To define operating system environment variable ?

Posted: Sat Jan 17, 2009 01:01 PM

Up

Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Re: How To define operating system environment variable ?

Posted: Sat Jan 17, 2009 04:10 PM

Vilian,

Try to use 0xFFFF instead of -1

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Re: How To define operating system environment variable ?

Posted: Sat Jan 17, 2009 04:12 PM

Also use "Environment" instead of "TESTE3"

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Re: How To define operating system environment variable ?

Posted: Sat Jan 17, 2009 04:23 PM

Vilian,

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

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1067
Joined: Wed Nov 09, 2005 02:17 AM

Re: How To define operating system environment variable ?

Posted: Tue Jan 20, 2009 01:47 PM

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
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Re: How To define operating system environment variable ?

Posted: Tue Jan 20, 2009 06:31 PM

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
Posts: 1067
Joined: Wed Nov 09, 2005 02:17 AM

Re: How To define operating system environment variable ?

Posted: Tue Jan 20, 2009 08:01 PM

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
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Re: How To define operating system environment variable ?

Posted: Tue Jan 20, 2009 10:50 PM

Vilian,

Please review FWH\samples\testrg32.prg

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1067
Joined: Wed Nov 09, 2005 02:17 AM

Re: How To define operating system environment variable ?

Posted: Wed Jan 21, 2009 01:30 PM
Antonio,

I made thus:

#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
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

Re: How To define operating system environment variable ?

Posted: Mon Jan 26, 2009 09:20 AM

Vilian,

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

regards, saludos

Antonio Linares
www.fivetechsoft.com

Continue the discussion