473,799 Members | 3,025 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SetEnvironmentV ariable 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 SetEnvironmentV araible() 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 5518
From the Platform SDK
--------------------------

Calling SetEnvironmentV ariable has no effect on the system environment
variables. The user can add or modify system environment variables using the
Control Panel. To programmaticall y add or modify system environment
variables, add them to the
HKEY_LOCAL_MACH INE\System\Curr entControlSet\C ontrol\Session
Manager\Environ ment registry key, then broadcast a WM_SETTINGCHANG E 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 GetEnvironmentV ariable)
or are you trying to find it in the EnvironmentVari ables 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 SetEnvironmentV araible() 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 GetEnviromnentV ariable 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 GetEnvironmentV ariableW?

Maybe like this?

[DllImport("KERN EL32.DLL", EntryPoint="Get EnvironmentVari ableW",
SetLastError=tr ue, CharSet=CharSet .Unicode, ExactSpelling=t rue,
CallingConventi on=CallingConve ntion.Winapi)]
public static extern uint GetEnvironmentV ariable(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 GetEnviromnentV ariable 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("kern el32.dll",CharS et=CharSet.Auto , SetLastError=tr ue)]

[return:MarshalA s(UnmanagedType .Bool)]

// The declaration is similar to the SDK function.
public static extern bool SetEnvironmentV ariable(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 SetEnvironmentV ariableW, NOT
GetEnvironmentV ariableW, 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.Componen tModel;
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.Componen tModel.Containe r components = null;
public Form1() { InitializeCompo nent(); }

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

private void InitializeCompo nent()
{
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.SuspendLay out();
this.txName.Loc ation = new System.Drawing. Point(56, 16);
this.txName.Nam e = "txName";
this.txName.Tab Index = 0;
this.txName.Tex t = "";
this.txValue.Lo cation = new System.Drawing. Point(56, 40);
this.txValue.Na me = "txValue";
this.txValue.Ta bIndex = 1;
this.txValue.Te xt = "";
this.button1.Lo cation = new System.Drawing. Point(160, 40);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 2;
this.button1.Te xt = "set named";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
this.label1.Aut oSize = true;
this.label1.Loc ation = new System.Drawing. Point(14, 16);
this.label1.Nam e = "label1";
this.label1.Siz e = new System.Drawing. Size(34, 16);
this.label1.Tab Index = 3;
this.label1.Tex t = "Name";
this.label2.Aut oSize = true;
this.label2.Loc ation = new System.Drawing. Point(14, 40);
this.label2.Nam e = "label2";
this.label2.Siz e = new System.Drawing. Size(33, 16);
this.label2.Tab Index = 4;
this.label2.Tex t = "Value";
this.button2.Lo cation = new System.Drawing. Point(160, 16);
this.button2.Na me = "button2";
this.button2.Ta bIndex = 5;
this.button2.Te xt = "get named ";
this.button2.Cl ick += new System.EventHan dler(this.butto n2_Click);
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(240, 69);
this.Controls.A dd(this.button2 );
this.Controls.A dd(this.label2) ;
this.Controls.A dd(this.label1) ;
this.Controls.A dd(this.button1 );
this.Controls.A dd(this.txValue );
this.Controls.A dd(this.txName) ;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}

private void button1_Click(o bject sender, System.EventArg s e)
{
MessageBox.Show (string.Format( "SetEnvironment Variable returned :
{0}", LibWrap.SetEnvi ronmentVariable (txName.Text, txValue.Text))) ;
}

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

private void button2_Click(o bject sender, System.EventArg s e)
{
MessageBox.Show (string.Format( "GetEnvironment Variable returned :
{0}", Environment.Get EnvironmentVari able(txName.Tex t)));
}

}
internal class LibWrap
{
[DllImport("KERN EL32.DLL", EntryPoint="Set EnvironmentVari ableW",
SetLastError=tr ue, CharSet=CharSet .Unicode, ExactSpelling=t rue,
CallingConventi on=CallingConve ntion.StdCall)]
public static extern bool SetEnvironmentV ariable(String name, String
value);
[DllImport("KERN EL32.DLL", EntryPoint="Get EnvironmentVari ableW",
SetLastError=tr ue, CharSet=CharSet .Auto)]
public static extern uint GetEnvironmentV ariable(String name, ref
string value, uint length);
}
}
/*************** *************** *************** ********** */
Nov 17 '05 #6
ps. you can omit the DllImport for GetEnvironmentV ariableW, 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 SetEnvironmentV ariableW, NOT
GetEnvironmentV ariableW, 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.Componen tModel;
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.Componen tModel.Containe r components = null;
public Form1() { InitializeCompo nent(); }

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

private void InitializeCompo nent()
{
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.SuspendLay out();
this.txName.Loc ation = new System.Drawing. Point(56, 16);
this.txName.Nam e = "txName";
this.txName.Tab Index = 0;
this.txName.Tex t = "";
this.txValue.Lo cation = new System.Drawing. Point(56, 40);
this.txValue.Na me = "txValue";
this.txValue.Ta bIndex = 1;
this.txValue.Te xt = "";
this.button1.Lo cation = new System.Drawing. Point(160, 40);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 2;
this.button1.Te xt = "set named";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
this.label1.Aut oSize = true;
this.label1.Loc ation = new System.Drawing. Point(14, 16);
this.label1.Nam e = "label1";
this.label1.Siz e = new System.Drawing. Size(34, 16);
this.label1.Tab Index = 3;
this.label1.Tex t = "Name";
this.label2.Aut oSize = true;
this.label2.Loc ation = new System.Drawing. Point(14, 40);
this.label2.Nam e = "label2";
this.label2.Siz e = new System.Drawing. Size(33, 16);
this.label2.Tab Index = 4;
this.label2.Tex t = "Value";
this.button2.Lo cation = new System.Drawing. Point(160, 16);
this.button2.Na me = "button2";
this.button2.Ta bIndex = 5;
this.button2.Te xt = "get named ";
this.button2.Cl ick += new System.EventHan dler(this.butto n2_Click);
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(240, 69);
this.Controls.A dd(this.button2 );
this.Controls.A dd(this.label2) ;
this.Controls.A dd(this.label1) ;
this.Controls.A dd(this.button1 );
this.Controls.A dd(this.txValue );
this.Controls.A dd(this.txName) ;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}

private void button1_Click(o bject sender, System.EventArg s e)
{
MessageBox.Show (string.Format( "SetEnvironment Variable returned :
{0}", LibWrap.SetEnvi ronmentVariable (txName.Text, txValue.Text))) ;
}

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

private void button2_Click(o bject sender, System.EventArg s e)
{
MessageBox.Show (string.Format( "GetEnvironment Variable returned :
{0}", Environment.Get EnvironmentVari able(txName.Tex t)));
}

}
internal class LibWrap
{
[DllImport("KERN EL32.DLL", EntryPoint="Set EnvironmentVari ableW",
SetLastError=tr ue, CharSet=CharSet .Unicode, ExactSpelling=t rue,
CallingConventi on=CallingConve ntion.StdCall)]
public static extern bool SetEnvironmentV ariable(String name, String
value);
[DllImport("KERN EL32.DLL", EntryPoint="Get EnvironmentVari ableW",
SetLastError=tr ue, CharSet=CharSet .Auto)]
public static extern uint GetEnvironmentV ariable(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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2918
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 using the following code in Page_Load event handler. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim intcount As Integer intcount = 0 For intcount = 0 To 4
9
2001
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 file? Is the .pdb necessary for the release version? Thanks a bunch. Dara
3
2202
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 version, both having been dotfuscated by the standard dotfuscator of visual studio 2003 ? Tia
1
1994
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 referenced all the other class library projects (i.e. so that it used the dll created when those other projects were built). The debug solution was configured so that all the projects in it had debug
2
2377
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 application is developed using C++/Managed C++ and built using VS 2003 under .NET framework 1.1. In Debug, it uses of up to 600Mb of memory, whereas in Release it only gets
6
9143
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++ project. I then add a class library, and add a reference to this project in the first project. When I do a release build, I see the following in the output from the DLL compile: /OUT:"C:\Documents and Settings\Andrew\My Documents\Visual Studio
3
15520
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 line number of code that throws an exception. Specifically, I can have an error logging routine that, amongst other things parses the call stack and tells me the specific line of code that choked. It is also my understanding that if building in...
3
2000
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 to RELEASE? My current project uses about 8 dlls I've written in the past, but I don't want to keep re-compiling them for debug/release depending on whether I'm compiling the current project for debug/release.
8
2810
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 API like so: Environment.SetEnvironmentVariable("someKey", somevalue, EnvironmentVariableTarget.User); I have 2 such calls. Amazingly it takes nearly 10 seconds for these
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10268
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9079
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.