6

I am opening VS2010 solutions using C# and VS2010 automation. I open the solutions like this:

Type type = Type.GetTypeFromProgID("VisualStudio.DTE.10.0", true);
Object comObject = Activator.CreateInstance(type);
...
sol.Open(solution_full_path);

The problem I am having is that when I create the instance of the VisualStudio.DTE.10.0 object, it starts the devenv.exe process from winlogon.exe which sees completely different environment than my application. Some of the environment variables are important for resolving some paths set in projects.

Is there any how I can influence the environment variables of the devenv.exe process? Is there any way how I could inject environment/properties using the VS2010 automation interfaces?

1
  • This question probably requires more context; what's your end goal in all this? Regression testing? Bulk modification of solution files? Different techniques are applicable to different situations and types of environment influence. That said, you may want to look at Machine.config, which can provide Visual Studio settings that apply to all instances created on a particular machine. Nov 28, 2011 at 4:27

2 Answers 2

2

Is it possible to start devenv by yourself inside your environment. Then get your hands on the running Visual Studio Instance via the running object table (ROT).

// Get an instance of the currently running Visual Studio IDE.
EnvDTE80.DTE2 dte2;
dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.
GetActiveObject("VisualStudio.DTE.10.0");

You will get problems if you have more than one running VS instance but this also an easy one. You can get your hands on a specific VS instance where you only need to know the process id of your self started VS instance.

Visual Studio also registers a ProgID as an item moniker in the ROT. The ProgID is comprised of the name and process ID of the DTE process. So, for example, the object's ROT entry might be "!VisualStudio.DTE.10.0:1234," where 1234 is the process ID.

1
  • This is very interesting! I will have to check this fist. Thanks!
    – wilx
    Nov 27, 2011 at 21:24
0

Not entirely sure if this is what you are looking for, but windows environment variables can be changed from (assuming Windows 7): Control Panel -> System and Security -> System -> Advanced System Settings -> Environment Variables (button).

In this screen, you can set user variables as well as system variables. Perhaps the settings you want your app to find are stored under the user, rather than the system, and then opening the app under a different user causes those variables to not be available?

Would creating the variables you need as system variables solve your problem?

2
  • No, changing global environment variables is not an option. I need the variables to be inherited from the process that is creating the COM objects.
    – wilx
    Nov 25, 2011 at 10:27
  • @wilx Sorry, I can't seem to find any way of accessing a (not current) process' environment. Curiously though, I can't find any (reliable) source that it isn't possible either. Although, it may be impossible simply for security reasons.
    – Miika L.
    Nov 25, 2011 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.