0

I use Addin in VS 2010 and VS Package (vsix) in VS 2012. Addin and VSPackage uses common libraries.

I need detect if the library code (in execution time) is executed by Addin OR VSPackage.

Now, I have this code, but always true for Addin AND VSPackage

 public static bool VSAddinVSPackageMode
        {
            get { return (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv"); }
        }

I would like

  public static bool VSAddinMode { get { ... } }
  public static bool VSPackageMode { get { ... } }

Any suggestions for do best way about it?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
  • I think you should tell the library in which type of evnvironment it is. AFAIK there is no special environment indicator on for which type of extension is a library being loaded/used, and in special circumstances a library could be used eigther a Package and an Add-In together in the same VS instance, in spite of this is not your case. – Ursegor Apr 11 '14 at 07:11
  • IMHO, I think always will be a way to do it programatically to detect VSAddin or VSPackage. – Kiquenet Apr 11 '14 at 14:01
  • Ok, maybe there is, but even if, why isn't it easyer to send your library which one calls it? I think you could do a MEF export-import, or just give it to a method. You won't be abel to get it from the user like this? Anyway, I hope someone helps you to find a way! – Ursegor Apr 12 '14 at 19:05

2 Answers2

0

Add-ins and packages are like different installers for the code. The actual code is the same and executed in the same VS process.

To detect the caller, if you know your host module names, you can walk the call stack until you find the add-in or package module.

Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66
0

You should probably not be using add-ins for Visual Studio 2010 or newer, but that's beside the point.

Add-Ins always use the same entry point. You could use a property like the following:

public bool VSAddinMode
{
    get;
    internal set;
}

Then, in your OnConnection method, set VSAddinMode = true; before calling other code.

VS Packages do not always have a single entry point, so the best way to implement VSPackageMode is the following:

public bool VSPackageMode
{
    get { return !VSAddinMode; }
}
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280