473,218 Members | 1,675 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

SetEnvironmentVariable in Debug and Release cofiguratin

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
10 5435
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Srinivasa Rao | last post by:
I have read in one article that when we compile the application in release mode, all the debug classes and properties will be automatically removed from the code. I tried to implement this thing by...
9
by: dee | last post by:
Hi I'm about to upload my site and I have switched to release version. Is that enough or do I still need to disable <compilation defaultLanguage="vb" debug="true" /> the debug="true" in the .pdb...
3
by: | last post by:
Since I need to dotfuscate my exe file anyway, does it make any difference if I use Debug or Release versions. Would a Debug version be easier to decompile/study/reverse engineer than a Release...
1
by: Epetruk | last post by:
Hello, In VS2003, I used to have two solutions - a debug and release solution. Each solution had a webservice project and several other class library projects. The webservice project...
2
by: Epetruk | last post by:
Hello, I have a problem where an application I am working on throws an OutOfMemoryException when I run it in Release mode, whereas it doesn't do this when I am running in Debug. The...
6
by: Andrew Rowley | last post by:
I am having trouble getting debug and release builds to work properly with project references using C++ .NET and Visual Studio 2003. I created a test solution, with a basic Windows form C++...
3
by: Bob Johnson | last post by:
It is my understanding - and please correct me if I'm wrong - that when building a project in debug mode, I can deploy the .pdb file along with the ..exe and thereby have access to the specific...
3
by: TBass | last post by:
Hello, Is there a way to get Visual Studio 2003 look to one directory for debug version dlls when set to DEBUG and then to another directory where I store the release version of a dll when set...
8
by: Dilip | last post by:
I am running into a weird problem in my ultra-simple Winforms application written in C#. In the Form.Shown event I set a couple of environment variables using the standard SetEnvironmentVariable...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.