Solved

Making .NET SetEnvironmentVariable call "stick" across .NET / COM

Posted on 2003-11-18
5
651 Views
Last Modified: 2013-11-25
OK, I think the title sums up what I need. I am writing a C# application (.exe) which includes an old DLL written by another segment of my company. It is in VB and simplifies calls to a third party application. This application needs an environment variable set in order to run. I did this once
a couple of years ago in C++ / COM and had no problem. I set the env variable when my code started
and all subsequent calls to the third party application worked well with the env variable set.
Now on to C#. I have used the DllImportAttribute to bring in kernel32.dll and declared my SetEnvironmentVariable() method. All appears to be ok (no compile errors, sets the env variable as checked later by GetEnvironmentVariable()). But when I make my calls to the VB dll, the third party application fails (properly, cleanly, etc) because it doesn't see this environment variable. If I just set the environment variable through the Windows control panel, the code runs fine. I figure this has to do with the Application Domains / Process  model of .NET, but I can't figure out how to work around it.

Here is the DllImport, just so you can see some code, but I don't know what else could really help...

[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]      
public static extern bool SetEnvironmentVariable(string lpname, string lpvalue);

I have made direct calls to the above, and I have followed the MSDN method and called the following method:

public static bool SetEnvironmentVariableEx(string evar, string eval) {
   try {
      // Get the write permission to set the environment variable.
      EnvironmentPermission ep = new EnvironmentPermission(EnvironmentPermissionAccess.Write,evar);
      environmentPermission.Demand();
      return SetEnvironmentVariable(evar, eval);
   }
   catch( Exception e) {
      Console.WriteLine("Exception:" + e.Message);
   }
      return false;
   }

And finally, the project requires the inclusion of COMSVCSLIB.dll and does not produce a library named InterOp.XXXX.dll as many of old COM dll's do. I am still uncertain about the details of Interop, so I am not sure why this is.


Thanks for any help
--j


0
Comment
Question by:jmuldrum
  • 3
  • 2
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
Write required environment variable directlly to Registry:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
using Registry .NET class.
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
It should be something like this:

RegistryKey regkey;
String keyValue;

keyValue = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment";
regkey = Registry.LocalMachine.OpenSubKey(keyValue, True);
regkey.SetValue("Variable name", "Variable value");


0
 

Author Comment

by:jmuldrum
Comment Utility
Thanks for the quick response.... I will work on that in a few minutes, but how would this affect other applications running on the same machine which would use the same env. variable but with a different value?

--j
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
Comment Utility
Agree, this is not clean. Use this only if there is no othe ways.
I can think about following ways:
1) Use HKEY_CURRENT_USER instead of HKEY_LOCAL_MACHINE (user instead of system environment variable)
2) Delete it when you don't need it more
3) You can write small application which runs your own application using CreateProcess API. It allows to redefine environment variables for created process.
0
 

Author Comment

by:jmuldrum
Comment Utility
I had thought about usig CreateProcess(), but this little "experiment" will eventually make its way into an application which requires a lot of data be passed. I am connecting to a wrapper for MQSeries, so  moving this data across a process boundary is something I would rather not try to do unless no other way exist. Thanks for the info. I will try to test it this morning.
In the meantime, if anything else occurs to you, let me know

Thanks
--j
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now