{"id":44,"date":"2012-01-16T23:40:59","date_gmt":"2012-01-16T21:40:59","guid":{"rendered":"http:\/\/tom.paschenda.org\/blog\/?p=44"},"modified":"2012-01-16T23:40:59","modified_gmt":"2012-01-16T21:40:59","slug":"set-multiple-startup-projects-in-visual-studio-2010-via-macro","status":"publish","type":"post","link":"https:\/\/tom.paschenda.org\/blog\/?p=44","title":{"rendered":"Set multiple startup projects in Visual Studio 2010 via macro"},"content":{"rendered":"<p>\u2014\u2014RANT MODE: ON\u2014\u2013<\/p>\n<p>In one C++ project I am currently working on (yes, C++ is not dead \ud83d\ude09 , I frequently have to switch between different startup projects in Visual Studio. Also, I sometimes want to start several executables at once. For this, Visual Studio supports setting of multiple startup projects.<\/p>\n<p>However, settings those by hand can be a bit tedious, so I tried to make a visual basic macro for it. Ok, to be honest I did not try to make one &#8211; I tried to steal one from the internet.<\/p>\n<p>A bit of googling revealed the following post which gives an approach for a single startup project and a &#8220;theoretical&#8221; solution for multiple startup projects:<br \/>\n<a href=\"http:\/\/matthiaskraaz.blogspot.com\/2011\/08\/visual-studio-setting-multiple-startup.html\">http:\/\/matthiaskraaz.blogspot.com\/2011\/08\/visual-studio-setting-multiple-startup.html<\/a><\/p>\n<p>Turns out, setting multiple startup projects programmatically is not possible in Visual Studio 2005.<\/p>\n<p>With Visual Studio 2010 however, it is possible. Here is how it works:<\/p>\n<p>\u2014\u2014RANT MODE: OFF\u2014\u2013<\/p>\n<pre name=\"code\" class=\"vb\">\r\nPublic Sub SetMyStartupProjects()  \r\n\r\n  'Create an array and fill it with the startup projects you would like to set\r\n  Dim StartupProjectsArray As Object = System.Array.CreateInstance(GetType(Object), 3)\r\n  StartupProjectsArray(0) = \"MyProj1.vcxproj\"\r\n  StartupProjectsArray(1) = \"MyProj2.vcxproj\"\r\n  StartupProjectsArray(2) = \"MyProj3.vcxproj\"\r\n\r\n  'Set the startup projects of the current solution\r\n  Dim CurrentSolutionBuild As SolutionBuild = DTE.Solution.SolutionBuild\r\n  CurrentSolutionBuild.StartupProjects = StartupProjectsArray\r\n\r\nEnd sub<\/pre>\n<p>The code for creating the array might look a bit clunky, but a collection or any array defined via literals doesn&#8217;t work.<br \/>\nThe type you use on the right hand side of <code>CurrentSolutionBuild.StartupProjects =<\/code> really has to be of the type <code>System.Array<\/code> and the things inside the array really need to have the type <code>Object<\/code>. The rest is straightforward.<\/p>\n<p>As a bonus, here is a little helper method that takes a usual VB collection:<\/p>\n<pre name=\"code\" class=\"vb\">\r\nPrivate Function SetStartupProjects(ByVal StartupProjects As Collection)\r\n  Dim StartupProjectsArray As Object = System.Array.CreateInstance(GetType(Object), StartupProjects.Count)\r\n  Dim i As Integer\r\n  For i = 0 To StartupProjects.Count - 1\r\n    StartupProjectsArray(i) = StartupProjects(i + 1)\r\n  Next\r\n\r\n  Dim CurrentSolutionBuild As SolutionBuild = DTE.Solution.SolutionBuild\r\n  CurrentSolutionBuild.StartupProjects = StartupProjectsArray\r\nEnd Function<\/pre>\n<p>You can call it like this:<\/p>\n<pre name=\"code\" class=\"vb\">\r\nPublic Sub SetMyStartupProjects()\r\n  'Create a collection that you would like to set\r\n  Dim StartupProjects As New Collection\r\n  StartupProjects.Add(\"MyProj1.vcxproj\")\r\n  StartupProjects.Add(\"MyProj2.vcxproj\")\r\n  StartupProjects.Add(\"MyProj3.vcxproj\")\r\n\r\n  'Set the startup projects of the current solution\r\n  SetStartupProjects(StartupProjects)\r\nEnd sub<\/pre>\n<p>I wonder why Microsoft doesn&#8217;t allow collections or straightforward String-arrays in the first place. Well, whatever, if you can encapsulate the ugly stuff, it is not ugly anymore.<\/p>\n<p>Happy coding!\n<link href=\"css\/SyntaxHighlighter.css\" type=\"text\/css\" rel=\"stylesheet\" \/> <script src=\"js\/shCore.js\" language=\"javascript\"><\/script><br \/>\n<script src=\"js\/shBrushCSharp.js\" language=\"javascript\"><\/script><br \/>\n<script src=\"js\/shBrushXml.js\" language=\"javascript\"><\/script><br \/>\n<script language=\"javascript\"> dp.SyntaxHighlighter.ClipboardSwf = \\'\/flash\/clipboard.swf\\'; dp.SyntaxHighlighter.HighlightAll(\\'code\\'); <\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u2014\u2014RANT MODE: ON\u2014\u2013 In one C++ project I am currently working on (yes, C++ is not dead \ud83d\ude09 , I frequently have to switch between different startup projects in Visual Studio. Also, I sometimes want to start several executables at once. For this, Visual Studio supports setting of multiple startup projects. However, settings those by [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,9],"tags":[],"class_list":["post-44","post","type-post","status-publish","format-standard","hentry","category-software-development","category-vba"],"_links":{"self":[{"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/44","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=44"}],"version-history":[{"count":0,"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/44\/revisions"}],"wp:attachment":[{"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}