3

I am using Visual Studio 2012

I have a T4-Template within a project to generate Code from existing Code. For that, i use the Visual Studio Env.DTE-API. The same error as discribed will be thrown in a simple console application, so it cannot be a problem within my project.

For a couple of weeks, all runs fine.

Today a am confronted only sometimes with a exception. The same project runs on a coworkers machine like a charm, no exceptions ever.

That leads me to the assumption that something with my Visual Studio or or my machine in general is provoking this exception, not the project itself.

The exception reads as follow (i cutted it a little bit):

Error 7 Running transformation: System.Runtime.InteropServices.COMException (0x800401E3): Vorgang nicht verfügbar. (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE)) at System.Runtime.InteropServices.Marshal.GetActiveObject(Guid& rclsid, IntPtr reserved, Object& ppunk) at System.Runtime.InteropServices.Marshal.GetActiveObject(String progID)

The code on which the exception will be throwed reads as follow:

DTE dte = (DTE) Marshal.GetActiveObject("VisualStudio.DTE");

My questions are: Is there generally something wrong or critical with the code i use? And, maybe someone has an idea what could be wrong with Visual Studio or the machine running it.

I have already ran the code sample which can be found on (Marshal.GetActiveObject() throws MK_E_UNAVAILABLE exception in C#), my Visual Studio instances are visible and there in ROT.

Best Regards

Community
  • 1
  • 1
Peter Bucher
  • 295
  • 2
  • 13
  • 1
    With the call "DTE2 dte2 = (DTE2)Marshal.GetActiveObject("VisualStudio.DTE.11.0");" it seems to work. DTE2 instead of DTE. I got the idea from: http://msdn.microsoft.com/en-us/library/vstudio/yf86a8ts.aspx – Peter Bucher Mar 13 '13 at 12:56
  • It works event better with a T4 Template and the this.Host property as here described: http://stackoverflow.com/questions/3548026/get-referenced-projects-path-in-t4-template – Peter Bucher Mar 13 '13 at 13:49
  • I got this error when attempting to get the instance of VS with two versions installed. Looked in registry for biggest CLSID matching "VisualStudio.DTE*" and found this one that worked: `DTE dte = (DTE) Marshal.GetActiveObject("VisualStudio.DTE.14.0");` – Rich Moss May 03 '19 at 22:55

1 Answers1

3

It has been observed that you will consistently get this error if a process running with elevated privileges tries to obtain an interface to a process running without elevated privileges, and vice versa.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • 1
    So, for example: One Visual Studio runas administrator and another Visual Studio does run as a normal user. If the first calling the interface of the latter or vice versa, it can come to the problem i discribed? Because with my solutions there is only a access from one instance to itsef, so it`s clear why it works with your describtion. – Peter Bucher Nov 04 '13 at 13:39