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);
}
}
/******************************************************* */