By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,212 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,212 IT Pros & Developers. It's quick & easy.

SetEnvironmentVariable in Debug and Release cofiguratin

P: n/a
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 ?!

Nov 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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\Contro l\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))

--
--

Of all words of tongue and pen, the saddest are: "It might have been"
"de************@gmail.com" wrote:
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 ?!

Nov 17 '05 #2

P: n/a
Thanks for the response ;)
I do the checks via GetEnviromnentVariable and also via Process
Explorer, which is a very nice freeware utility.
--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #3

P: n/a
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);
--
--

Of all words of tongue and pen, the saddest are: "It might have been"
"DenG" wrote:
Thanks for the response ;)
I do the checks via GetEnviromnentVariable and also via Process
Explorer, which is a very nice freeware utility.
--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com

Nov 17 '05 #4

P: n/a
Actually, it was:

// Import the Kernel32 dll file.
[DllImport("kernel32.dll",CharSet=CharSet.Auto, SetLastError=true)]

[return:MarshalAs(UnmanagedType.Bool)]

// The declaration is similar to the SDK function.
public static extern bool SetEnvironmentVariable(string lpName, string
lpValue);

This is the Set one.
BTW, it is taken from
http://support.microsoft.com/default...b;en-us;829145

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #5

P: n/a
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("SetEnvironmentVaria ble 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("GetEnvironmentVaria ble 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);
}
}
/************************************************** ***** */
Nov 17 '05 #6

P: n/a
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" wrote:
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("SetEnvironmentVaria ble 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("GetEnvironmentVaria ble 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);
}
}
/************************************************** ***** */

Nov 17 '05 #7

P: n/a
Do you have VS.NET 2003?
'Cause I have 2002.

Anyway, it didn't work at Release mode. Sorry :(

The solution was found:
http://www.dotnetnewsgroups.com/news...d.asp?ID=77768


--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #8

P: n/a
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(

--
--

Of all words of tongue and pen, the saddest are: "It might have been"
"DenG" wrote:
Do you have VS.NET 2003?
'Cause I have 2002.

Anyway, it didn't work at Release mode. Sorry :(

The solution was found:
http://www.dotnetnewsgroups.com/news...d.asp?ID=77768


--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com

Nov 17 '05 #9

P: n/a
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.
--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #10

P: n/a
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!!

--
--

Of all words of tongue and pen, the saddest are: "It might have been"
"DenG" wrote:
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.
--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com

Nov 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.