Automating Visual Studio instance from separate process - c#

Is there a way to write an application that can connect to a running instance of Visual Studio and issue commands to it? For example, could I write a WPF app with a button that, when clicked, issues a "Build.BuildSolution" command to an already-open instance of Visual Studio, causing it to start a build?
I'm sure I could use SendKeys to send Ctrl+Shift+B, but I want to know if there's a way to write to an actual API to automate Visual Studio, and invoke commands by name.

Here's a C# program that connects to a running Visual Studio and issues a Build command. The DTE.9 part means "Visual Studio 2008" - use DTE.8 for VS 2005, or DTE.10 for VS 2010.
using System;
using System.Runtime.InteropServices;
using EnvDTE80;
namespace SORemoteBuild
{
class Program
{
[STAThread]
static void Main(string[] args)
{
// Get an instance of the currently running Visual Studio IDE.
EnvDTE80.DTE2 dte2;
dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.
GetActiveObject("VisualStudio.DTE.9.0");
dte2.Solution.SolutionBuild.Build(true);
}
}
public class MessageFilter : IOleMessageFilter
{
// ... Continues at http://msdn.microsoft.com/en-us/library/ms228772.aspx
(The nonsense with STAThread and MessageFilter is "due to threading contention issues between external multi-threaded applications and Visual Studio", whatever that means. Pasting in the code from http://msdn.microsoft.com/en-us/library/ms228772.aspx makes it work.)

If you are just trying to automate builds/packaging of your apps, you should look into MSBuild -- it is Microsoft's build engine that lets you script automation of most of the functions of Visual Studio.

Related

Visual Studio and Post-mortem debugging

I would like to know how vsjitdebugger.exe can cause a particular running instance of Visual Studio to be used to debug a process.
Background: currently vsjitdebugger.exe is set as the executable run whenever there is a "debug break" - i.e. in C# System.Diagnostics.Debugger.Launch();. Whenever this happens I see a dialog with a list of available debuggers - this includes all running instances of Visual Studio.
My question is how vsjitdebugger.exe activates a particular running instance of Visual Studio. Please note that I am particularly interested in existing running instances of VS and not in starting a new instance.
Maybe you can attach the process to vs debugger through VS sdk.
public static void Attach(DTE dte)
{
EnvDTE.Processes processes = dte.Debugger.LocalProcesses;
foreach(EnvDTE.Process proc in processes)
if(proc.Name.IndexOf("Target.exe") != -1)
proc.Attach();
}
The document: Process Interface

How to register my visual studio extension to run events?

I have a visual studio extension and I want it to run another executable before the program itself is running without debugger (when the program runs with a certain configuration).
how can I do that?
I managed to do that when the program is being debugged - I used DTE.Events.DebuggerEvents interface. is there a similar event for running the program without debugger?
private static void EventsRegistering()
{
DTE2 visualStudioInstance;
debuggerEvents = visualStudioInstance.Events.DebuggerEvents;
debuggerEvents.OnEnterRunMode += DebuggerEvents_OnEnterRunMode;
}
Just to be clear - I don't want it to run with every build of the program, just for every run.
You can subscribe to DTE.Events.CommandEvents.OnBeforeExecute and detect when a command name is Debug.StartWithoutDebugging. See for example Monitor commands execution.

Using Visual Studio Code and using defined symbols

EDIT: I have edited the whole question, since this is not only for Unity3D, but at all .sln projects.
I have a installation of Visual Studio Code(Not Visual Studio, but this:https://code.visualstudio.com/) on my Macbook at work. VSCode is otherwise working just fine with normal and Unity3D projects. I get Intellisense on all classes, including Unity3D specific ones, like GameObject. So I think my installation and startup sequence is correct.
Only problem I have, is that VSCode does not seem to recognize constants defined in the .csproj files. First I noticed this with some Unity3D plugins, but it is persistent on normal Visual Studio projects too.
My sample project is a dummy application downloaded from internet, but it is fully working on MonoDevelop. This is my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DummyConsoleApplication
{
class Program
{
static void Main(string[] args)
{
tester();
}
#if DEBUG
static void tester(){
}
#endif
}
}
The function call in Main causes a not found exception on the editor, but it compiles fine, since the .csproj file has this line:
<DefineConstants>DEBUG;TRACE</DefineConstants>
Any verification on if this is normal behaviour for VSCode would be greately appreciated. Also, if anyone is aware of any solution, even hacky ones, to get past this bug and force Intellisense to autocomplete would help out too.
The error I get is:
The name 'tester' does not exist in the current context [DummyConsoleApplication]
My hardware is a Macbook with Yosemite and my compiler is dnx-mono.1.0.0-beta4.
This is a known limitation with OmniSharp, the C# engine that Visual Studio Code is built around. There is an open enhancement request for adding <DefineConstants> support, but it is tied to a larger issue with regards to MSBuild Support.
Currently, this isn't a supported configuration under Visual Studio Code. You can try to define your constants through the launch.json instead, but support is minimal at best.
It should work...
As a sanity check, have you:
"Sync MonoDevelop Project" recently?
Make sure Visual Studio Code has the -csharp solution (.sln) selected? (Click the flame in the status bar to change)

Is it possible to create Stress Test using Microsoft.VisualStudio.TestTools.LoadTesting.LoadTest Class?

I have a WebService in ASP.NET and need to write Stress Test now.
I have loadtest in my project now. I added them using wizard in VS2013 Ultimate http://msdn.microsoft.com/en-us/library/ms182594.aspx
But now I want to move from VS Ultimate to VS Professional which has not this feature.
My Question: Can I create Load Test programly using Microsoft.VisualStudio.TestTools.LoadTesting.LoadTest Class?
If yes, can you provide me examples of creating and using?
If no, Is any alternatives inside VS?
loadtest is working OK now and using this method:
[TestMethod()]
[DeploymentItem("template.jpg")]
public void MultithreadedProcessorUsingTest()
{
WebService1SoapClient _service = new WebService1SoapClient();
string testImagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "template.jpg");
byte[] image = File.ReadAllBytes(testImagePath);
var data = _service.UploadImage(image);
int currentProcNumber = data.processInfo.processNumber;
Assert.Fail(currentProcNumber.ToString());
}
Visual Studio Web and Load tests are only supported in the Ultimate editions.
By using the controller and agent software, download it from Microsoft, you can run load tests on computers that do not have Visual Studio Ultimate.

Visual Studio Extensibility Package not looking at correct project

I have created a new VS 2010 extensibility package. So far, all I want to do is have the user press a button and fill a listview with the entire contents of the solution. I have the following code:
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.
GetActiveObject("VisualStudio.DTE.10.0");
foreach (Project project in dte.Solution.Projects)
{
foreach(ProjectItem pi in project.ProjectItems)
{
listView1.Items.Add(pi.Name.ToString());
}
}
This does seem to work, however, it populates the list with the contents of the solution with the package in it and not the experimental instance that is launched when this is run. Am I instantiating the reference wrongly?
GetActiveObject method returns first process instance of DTE, not
caller DTE. (in Visual Studio SDK 2010 project on Visual Studio 2010,
type F5 to execure experimental hive may fail)
Look at here and here for more details...
No - you need to use ProjectItem.SubProject to get to what you want... depending on the solution structure some recursion could be needed... for some sample code doing nicely all this see http://www.wwwlicious.com/2011/03/envdte-getting-all-projects.html

Categories

Resources