SetEnvironmentVariable in Debug and Release cofiguratin

  • Thread starter Thread starter denis.greatest
  • Start date Start date
D

denis.greatest

Hello folks,

I was trying to set some environment variable for a process I create.
(The variables are my own, not system).

I use SetEnvironmentVaraible() from C# code.
In Debug configuration this works fine, but in Release configuration
the action fails.
The return value is TRUE, and no Exception is thrown.

Any ideas ?!
 
From the Platform SDK
--------------------------

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.

So, my next question is this ... how are you checking for your environment
variable, are you doing it programatically (calling GetEnvironmentVariable)
or are you trying to find it in the EnvironmentVariables tab of the control
panel, etc.

You will not be able to see the variable in your system environment
variables (via the control panel) every PROCESS has an environment block that
contains a set of environment variables and their values, you will have to
inspect these environment variables programatically.

I hope that helps.

:o))
 
Thanks for the response ;)
I do the checks via GetEnviromnentVariable and also via Process
Explorer, which is a very nice freeware utility.
 
How are you declaring your access to GetEnvironmentVariableW?

Maybe like this?

[DllImport("KERNEL32.DLL", EntryPoint="GetEnvironmentVariableW",
SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.Winapi)]
public static extern uint GetEnvironmentVariable(String name, ref string
value, uint length);
 
Whoops, my bad, I meant to ask about SetEnvironmentVariableW, NOT
GetEnvironmentVariableW, but you seem to have figured that mistake out anyhow!

The following is a simple form app that tests this functionality. On my
machine it works fine in both debug and release builds .... try it on your
machine and see what happens.

/******************************************************* */
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CSTest
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txName;
private System.Windows.Forms.TextBox txValue;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button2;

private System.ComponentModel.Container components = null;
public Form1() { InitializeComponent(); }

protected override void Dispose( bool disposing )
{
if( disposing ) { if (components != null) { components.Dispose(); } }
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.txName = new System.Windows.Forms.TextBox();
this.txValue = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.txName.Location = new System.Drawing.Point(56, 16);
this.txName.Name = "txName";
this.txName.TabIndex = 0;
this.txName.Text = "";
this.txValue.Location = new System.Drawing.Point(56, 40);
this.txValue.Name = "txValue";
this.txValue.TabIndex = 1;
this.txValue.Text = "";
this.button1.Location = new System.Drawing.Point(160, 40);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "set named";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(14, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(34, 16);
this.label1.TabIndex = 3;
this.label1.Text = "Name";
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(14, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(33, 16);
this.label2.TabIndex = 4;
this.label2.Text = "Value";
this.button2.Location = new System.Drawing.Point(160, 16);
this.button2.Name = "button2";
this.button2.TabIndex = 5;
this.button2.Text = "get named ";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(240, 69);
this.Controls.Add(this.button2);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.txValue);
this.Controls.Add(this.txName);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}

private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(string.Format("SetEnvironmentVariable returned :
{0}", LibWrap.SetEnvironmentVariable(txName.Text, txValue.Text)));
}

[STAThread]
static void Main() { Application.Run(new Form1()); }

private void button2_Click(object sender, System.EventArgs e)
{
MessageBox.Show(string.Format("GetEnvironmentVariable returned :
{0}", Environment.GetEnvironmentVariable(txName.Text)));
}

}
internal class LibWrap
{
[DllImport("KERNEL32.DLL", EntryPoint="SetEnvironmentVariableW",
SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool SetEnvironmentVariable(String name, String
value);
[DllImport("KERNEL32.DLL", EntryPoint="GetEnvironmentVariableW",
SetLastError=true, CharSet=CharSet.Auto)]
public static extern uint GetEnvironmentVariable(String name, ref
string value, uint length);
}
}


/******************************************************* */
 
ps. you can omit the DllImport for GetEnvironmentVariableW, it isn't used :o)
--
--

Of all words of tongue and pen, the saddest are: "It might have been"


billr said:
Whoops, my bad, I meant to ask about SetEnvironmentVariableW, NOT
GetEnvironmentVariableW, but you seem to have figured that mistake out anyhow!

The following is a simple form app that tests this functionality. On my
machine it works fine in both debug and release builds .... try it on your
machine and see what happens.

/******************************************************* */
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CSTest
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txName;
private System.Windows.Forms.TextBox txValue;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button2;

private System.ComponentModel.Container components = null;
public Form1() { InitializeComponent(); }

protected override void Dispose( bool disposing )
{
if( disposing ) { if (components != null) { components.Dispose(); } }
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.txName = new System.Windows.Forms.TextBox();
this.txValue = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.txName.Location = new System.Drawing.Point(56, 16);
this.txName.Name = "txName";
this.txName.TabIndex = 0;
this.txName.Text = "";
this.txValue.Location = new System.Drawing.Point(56, 40);
this.txValue.Name = "txValue";
this.txValue.TabIndex = 1;
this.txValue.Text = "";
this.button1.Location = new System.Drawing.Point(160, 40);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "set named";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(14, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(34, 16);
this.label1.TabIndex = 3;
this.label1.Text = "Name";
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(14, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(33, 16);
this.label2.TabIndex = 4;
this.label2.Text = "Value";
this.button2.Location = new System.Drawing.Point(160, 16);
this.button2.Name = "button2";
this.button2.TabIndex = 5;
this.button2.Text = "get named ";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(240, 69);
this.Controls.Add(this.button2);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.txValue);
this.Controls.Add(this.txName);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}

private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(string.Format("SetEnvironmentVariable returned :
{0}", LibWrap.SetEnvironmentVariable(txName.Text, txValue.Text)));
}

[STAThread]
static void Main() { Application.Run(new Form1()); }

private void button2_Click(object sender, System.EventArgs e)
{
MessageBox.Show(string.Format("GetEnvironmentVariable returned :
{0}", Environment.GetEnvironmentVariable(txName.Text)));
}

}
internal class LibWrap
{
[DllImport("KERNEL32.DLL", EntryPoint="SetEnvironmentVariableW",
SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool SetEnvironmentVariable(String name, String
value);
[DllImport("KERNEL32.DLL", EntryPoint="GetEnvironmentVariableW",
SetLastError=true, CharSet=CharSet.Auto)]
public static extern uint GetEnvironmentVariable(String name, ref
string value, uint length);
}
}


/******************************************************* */
 
I am using VS 2003, which could be the cause of the issue, however, all you
need to do is download the latest .net SDK and compile the code from the
command line and you should be able to test if this is the case or not.


Sorry I cannot be of any more help :o(
 
Thanks for your concern though ;) !
BTW, I think eventually the problem is related to the different version
of the API in WIN32: the *A (ASCII) and *W (UNICODE) version.
 
You could be right on that assumption, however, as you can tell, I tried to
implement my own wrapper for GetEnvironmentVariableW ... but I cannot get
that to work!!
 
Back
Top