For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > Setting User Environment Variables









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Setting User Environment Variables
chaf2701

2005-11-16, 7:05 pm

Hi,

I know that setting environment variables throw code was discussed a
lot but I still have problems.

I would like to modify user environment variables by editing the
registry key: HKEY_CURRENT_USER \ Environment. I tried the following:

private System.Int32 wResult = 0;
private System.IntPtr HWND_BROADCAST = new System.IntPtr(0xffff);
private System.Int32 WM_SETTINGCHANGE = 0x001A;
private System.Int32 SMTO_NORMAL = 0x0000;

[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
private static extern int SendMessageTimeout(
IntPtr hWnd,
[MarshalAs(UnmanagedType.U4)] int Msg,
IntPtr wParam,
IntPtr lParam,
[MarshalAs(UnmanagedType.U4)] int fuFlags,
[MarshalAs(UnmanagedType.U4)] int uTimeout,
[MarshalAs(UnmanagedType.U4)] ref int lpdwResult);


void SetEnvironmentVariable(System.String iName, System.String iValue)
{
//
// Access the user environment variables in the registry
//
using (RegistryKey wKey =
Registry.CurrentUser.CreateSubKey("Environment"))
{
//
// Set the environment variable value
//
wKey.SetValue(iName, iValue);

//
// Broadcast a WM_SETTINGCHANGE message
//
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE,
IntPtr.Zero, IntPtr.Zero, SMTO_NORMAL, 5000, ref wResult);

//
// Close the key and flush it to disk
//
wKey.Close();
}
}


It seems to work partially: the registry is modifyied but when I open a
new CMD shell and I print the content of my environment variable, it
still contained the old value.

Any idea ?
Thanks
Frank

Brian Muth

2005-11-16, 7:05 pm

Child processes by default will inherit the environment variable settings
from the client. They don't go to the registry. Your registry changes will
take affect the next time the current user logs on.

On the other hand, the application can set its own environment variable
settings, and all will be well....!

Brian


Vincent Fatica

2005-11-16, 7:05 pm

See:

http://support.microsoft.com/defaul...kb;en-us;104011

"How to propagate environment variables to the system"

As another poster pointed out, apps get their environment from their parent.

You cannot force an app (a possible parent) to update its environment (so
that children will get an updated environment).

Though you may find it disappointing, only Explorer and very few other apps
are programmed to respond to the method of Q104011 by updating its
environment.

The upshot is ... if you apply the method of Q104011, then apps newly
started **by Explorer** (and their descendants) will see the changes.

- Vince

On 16 Nov 2005 12:13:10 -0800, "chaf2701" <francois.chartrand@gmail.com>
wrote:

>Hi,
>
>I know that setting environment variables throw code was discussed a
>lot but I still have problems.
>
>I would like to modify user environment variables by editing the
>registry key: HKEY_CURRENT_USER \ Environment. I tried the following:
>
>private System.Int32 wResult = 0;
>private System.IntPtr HWND_BROADCAST = new System.IntPtr(0xffff);
>private System.Int32 WM_SETTINGCHANGE = 0x001A;
>private System.Int32 SMTO_NORMAL = 0x0000;
>
>[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
>private static extern int SendMessageTimeout(
> IntPtr hWnd,
> [MarshalAs(UnmanagedType.U4)] int Msg,
> IntPtr wParam,
> IntPtr lParam,
> [MarshalAs(UnmanagedType.U4)] int fuFlags,
> [MarshalAs(UnmanagedType.U4)] int uTimeout,
> [MarshalAs(UnmanagedType.U4)] ref int lpdwResult);
>
>
>void SetEnvironmentVariable(System.String iName, System.String iValue)
>{
> //
> // Access the user environment variables in the registry
> //
> using (RegistryKey wKey =
>Registry.CurrentUser.CreateSubKey("Environment"))
> {
> //
> // Set the environment variable value
> //
> wKey.SetValue(iName, iValue);
>
> //
> // Broadcast a WM_SETTINGCHANGE message
> //
> SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE,
>IntPtr.Zero, IntPtr.Zero, SMTO_NORMAL, 5000, ref wResult);
>
> //
> // Close the key and flush it to disk
> //
> wKey.Close();
> }
>}
>
>
>It seems to work partially: the registry is modifyied but when I open a
>new CMD shell and I print the content of my environment variable, it
>still contained the old value.
>
>Any idea ?
>Thanks
>Frank

chaf2701

2005-11-16, 7:05 pm

> Child processes by default will inherit the environment variable settings
> from the client. They don't go to the registry. Your registry changes will
> take affect the next time the current user logs on.


I was told to effect these changes without having to log off, broadcast
a WM_SETTINGCHANGE message to all windows in the system, so that any
interested applications (such as Windows Explorer, Program Manager,
Task Manager, Control Panel, and so forth) can perform an update.

Refer to this article: http://support.microsoft.com/?id=104011

> On the other hand, the application can set its own environment variable
> settings, and all will be well....!


It doesn't fit my application needs.

> Brian


chaf2701

2005-11-16, 7:05 pm

The code I posted is conform to article Q104011. Isn't it ?

My point is, even apps newly started **by Explorer** don't see the
changes. What did I do wrong?

Brian Muth

2005-11-16, 7:05 pm

>
>
> It doesn't fit my application needs.
>


I don't understand. Why can't your application set the environment variables
directly? What "doesn't fit"?

Brian


Vincent Fatica

2005-11-16, 7:05 pm

On 16 Nov 2005 13:16:02 -0800, "chaf2701" <francois.chartrand@gmail.com>
wrote:

>The code I posted is conform to article Q104011. Isn't it ?
>
>My point is, even apps newly started **by Explorer** don't see the
>changes. What did I do wrong?


You said:

> SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE,
> IntPtr.Zero, IntPtr.Zero, SMTO_NORMAL, 5000, ref wResult);


That code doesn't seem to be from Q104011.

It should be the equivalent of the following. Of utmost inmportance is the
(pointer to the) string "Environment".

SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
(LPARAM) "Environment", SMTO_ABORTIFHUNG, 5000, &RetCode);

--
- Vince
chaf2701

2005-11-21, 7:04 pm

My application needs to set environment variables for other
applications...

chaf2701

2005-11-21, 7:04 pm

You're rigth! I made a stupid mistake...

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com