/// <summary>
        ///     Get the DTE object for the instance of Visual Studio IDE that has
        ///     the specified solution open.
        /// </summary>
        /// <param name="solutionFile">The absolute filename of the solution</param>
        /// <returns>Corresponding DTE object or null if no such IDE is running</returns>
        public static object GetIDEInstance(string solutionFile)
        {
            var runningInstances = GetIDEInstances(true);
            var enumerator       = runningInstances.GetEnumerator();

            while (enumerator.MoveNext())
            {
                try
                {
                    var ide = enumerator.Value;
                    if (ide != null)
                    {
                        var sol = ViaCOM.GetProperty(ide, "Solution");
                        if (0 == string.Compare((string)ViaCOM.GetProperty(sol, "FullName"), solutionFile, ignore_case))
                        {
                            return(ide);
                        }
                    }
                }
                catch
                {
                    // ignored
                }
            }

            return(null);
        }
        public static bool SetProjectWorkingDir(object sol, string project_name, string working_dir)
        {
            bool made_change = false;

            Console.WriteLine("Looking for project {0}...", project_name);
            try
            {
                object prjs  = ViaCOM.GetProperty(sol, "Projects");
                object count = ViaCOM.GetProperty(prjs, "Count");
                for (int i = 1; i <= (int)count; ++i)
                {
                    object[] prjItemArgs = { (object)i };
                    object   prj         = ViaCOM.CallMethod(prjs, "Item", prjItemArgs);
                    string   name        = (string)ViaCOM.GetProperty(prj, "Name");
                    if (0 == string.Compare(name, project_name, ignore_case))
                    {
                        Console.WriteLine("Found project: {0}", project_name);
                        Console.WriteLine("Setting working directory");

                        string full_project_name = (string)ViaCOM.GetProperty(prj, "FullName");
                        Console.WriteLine(full_project_name);

                        // *NOTE:Mani Thanks to incompatibilities between different versions of the
                        // VCProjectEngine.dll assembly, we can't cast the objects recevied from the DTE to
                        // the VCProjectEngine types from a different version than the one built
                        // with. ie, VisualStudio.DTE.7.1 objects can't be converted in a project built
                        // in VS 8.0. To avoid this problem, we can use the com object interfaces directly,
                        // without the type casting. Its tedious code, but it seems to work.

                        // oCfgs should be assigned to a 'Project.Configurations' collection.
                        object oCfgs = ViaCOM.GetProperty(ViaCOM.GetProperty(prj, "Object"), "Configurations");

                        // oCount will be assigned to the number of configs present in oCfgs.
                        object oCount = ViaCOM.GetProperty(oCfgs, "Count");

                        for (int cfgIndex = 1; cfgIndex <= (int)oCount; ++cfgIndex)
                        {
                            object[] itemArgs       = { (object)cfgIndex };
                            object   oCfg           = ViaCOM.CallMethod(oCfgs, "Item", itemArgs);
                            object   oDebugSettings = ViaCOM.GetProperty(oCfg, "DebugSettings");
                            ViaCOM.SetProperty(oDebugSettings, "WorkingDirectory", (object)working_dir);
                        }

                        break;
                    }
                }
                made_change = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Failed to set working dir for project, {0}.", project_name);
            }

            return(made_change);
        }
        public static bool GetDTEAndSolution()
        {
            bool found_open_solution = true;

            Console.WriteLine("Looking for existing VisualStudio instance...");

            // Get an instance of the currently running Visual Studio .NET IDE.
            // dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.7.1");
            string full_solution_name = System.IO.Path.GetFullPath(solution_name);

            if (false == use_new_vs)
            {
                dte = GetIDEInstance(full_solution_name);
            }

            if (dte == null)
            {
                try
                {
                    Console.WriteLine("  Didn't find open solution, starting new background VisualStudio instance...");
                    Console.WriteLine("  Reading .sln file version...");
                    string version = GetSolutionVersion(full_solution_name);

                    Console.WriteLine("  Using version: {0}...", version);
                    string progid = GetVSProgID(version);

                    Type objType = Type.GetTypeFromProgID(progid);
                    dte = System.Activator.CreateInstance(objType);
                    Console.WriteLine("  Reading solution: \"{0}\"", full_solution_name);

                    solution = ViaCOM.GetProperty(dte, "Solution");
                    object[] openArgs = { full_solution_name };
                    ViaCOM.CallMethod(solution, "Open", openArgs);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine("Quitting do to error opening: {0}", full_solution_name);
                    solution = null;
                    dte      = null;
                    return(found_open_solution);
                }
                found_open_solution = false;
            }

            if (solution == null)
            {
                solution = ViaCOM.GetProperty(dte, "Solution");
            }

            return(found_open_solution);
        }
        /// <summary>
        ///     Get a table of the currently running instances of the Visual Studio .NET IDE.
        /// </summary>
        /// <param name="openSolutionsOnly">Only return instances that have opened a solution</param>
        /// <returns>A hashtable mapping the name of the IDE in the running object table to the corresponding DTE object</returns>
        public static Hashtable GetIDEInstances(bool openSolutionsOnly)
        {
            var runningIDEInstances = new Hashtable();
            var runningObjects      = GetRunningObjectTable();

            var rotEnumerator = runningObjects.GetEnumerator();

            while (rotEnumerator.MoveNext())
            {
                var candidateName = (string)rotEnumerator.Key;
                if (!candidateName.StartsWith("!VisualStudio.DTE"))
                {
                    continue;
                }

                var ide = rotEnumerator.Value;
                if (ide == null)
                {
                    continue;
                }

                if (openSolutionsOnly)
                {
                    try
                    {
                        var sol          = ViaCOM.GetProperty(ide, "Solution");
                        var solutionFile = (string)ViaCOM.GetProperty(sol, "FullName");
                        if (solutionFile != string.Empty)
                        {
                            runningIDEInstances[candidateName] = ide;
                        }
                    }
                    catch
                    {
                        // ignored
                    }
                }
                else
                {
                    runningIDEInstances[candidateName] = ide;
                }
            }
            return(runningIDEInstances);
        }
Beispiel #5
0
 public static bool SetActiveConfig(string config)
 {
     bool result = false;
     try
     {
         Console.WriteLine("Trying to set active config to \"{0}\"", config);
         object solBuild = ViaCOM.GetProperty(solution, "SolutionBuild");
         object solCfgs = ViaCOM.GetProperty(solBuild, "SolutionConfigurations");
         object[] itemArgs = { (object)config };
         object solCfg = ViaCOM.CallMethod(solCfgs, "Item", itemArgs);
         ViaCOM.CallMethod(solCfg, "Activate", null);
         Console.WriteLine("  Success!");
         result = true;
     }
     catch (Exception e)
     {
         Console.WriteLine("  Failed to set \"{0}\" as the active config.", config);
         Console.WriteLine(e.Message);
     }
     return result;
 }
        public static bool SetStartupProject(string startup_project)
        {
            bool result = false;

            try
            {
                // You need the 'unique name of the project to set StartupProjects.
                // find the project by generic name.
                Console.WriteLine("Trying to set \"{0}\" to the startup project", startup_project);
                object prjs  = ViaCOM.GetProperty(solution, "Projects");
                object count = ViaCOM.GetProperty(prjs, "Count");
                for (int i = 1; i <= (int)count; ++i)
                {
                    object[] itemArgs = { (object)i };
                    object   prj      = ViaCOM.CallMethod(prjs, "Item", itemArgs);
                    object   prjName  = ViaCOM.GetProperty(prj, "Name");
                    if (0 == string.Compare((string)prjName, startup_project, ignore_case))
                    {
                        object solBuild = ViaCOM.GetProperty(solution, "SolutionBuild");
                        ViaCOM.SetProperty(solBuild, "StartupProjects", ViaCOM.GetProperty(prj, "UniqueName"));
                        Console.WriteLine("  Success!");
                        result = true;
                        break;
                    }
                }

                if (result == false)
                {
                    Console.WriteLine("  Could not find project \"{0}\" in the solution.", startup_project);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("  Failed to set the startup project!");
                Console.WriteLine(e.Message);
            }
            return(result);
        }
Beispiel #7
0
        /// <summary>
        /// Get a table of the currently running instances of the Visual Studio .NET IDE.
        /// </summary>
        /// <param name="openSolutionsOnly">Only return instances that have opened a solution</param>
        /// <returns>A hashtable mapping the name of the IDE in the running object table to the corresponding DTE object</returns>
        public static Hashtable GetIDEInstances( bool openSolutionsOnly )
        {
            Hashtable runningIDEInstances = new Hashtable();
            Hashtable runningObjects = GetRunningObjectTable();

            IDictionaryEnumerator rotEnumerator = runningObjects.GetEnumerator();
            while ( rotEnumerator.MoveNext() )
            {
                string candidateName = (string) rotEnumerator.Key;
                if (!candidateName.StartsWith("!VisualStudio.DTE"))
                    continue;

                object ide = rotEnumerator.Value;
                if (ide == null)
                    continue;

                if (openSolutionsOnly)
                {
                    try
                    {
                        object sol = ViaCOM.GetProperty(ide, "Solution");
                        string solutionFile = (string)ViaCOM.GetProperty(sol, "FullName");
                        if (solutionFile != String.Empty)
                        {
                            runningIDEInstances[ candidateName ] = ide;
                        }
                    } 
                    catch {}
                }
                else
                {
                    runningIDEInstances[ candidateName ] = ide;
                }                       
            }
            return runningIDEInstances;
        }
Beispiel #8
0
        /// <summary>
        /// Get the DTE object for the instance of Visual Studio IDE that has 
        /// the specified solution open.
        /// </summary>
        /// <param name="solutionFile">The absolute filename of the solution</param>
        /// <returns>Corresponding DTE object or null if no such IDE is running</returns>
        public static object GetIDEInstance( string solutionFile )
        {
            Hashtable runningInstances = GetIDEInstances( true );
            IDictionaryEnumerator enumerator = runningInstances.GetEnumerator();

            while ( enumerator.MoveNext() )
            {
                try
                {
                    object ide = enumerator.Value;
                    if (ide != null)
                    {
                        object sol = ViaCOM.GetProperty(ide, "Solution");
                        if (0 == string.Compare((string)ViaCOM.GetProperty(sol, "FullName"), solutionFile, ignore_case))
                        {
                            return ide;
                        }
                    }
                } 
                catch{}
            }

            return null;
        }
        static int Main(string[] args)
        {
            int  retVal    = 0;
            bool need_save = false;

            try
            {
                parse_command_line(args);

                Console.WriteLine("Editing solution: {0}", solution_name);

                bool found_open_solution = GetDTEAndSolution();

                if (dte == null || solution == null)
                {
                    retVal = 1;
                }
                else
                {
                    MessageFilter.Register();

                    // Walk through all of the projects in the solution
                    // and list the type of each project.
                    foreach (DictionaryEntry p in projectDict)
                    {
                        string project_name = (string)p.Key;
                        string working_dir  = (string)p.Value;
                        if (SetProjectWorkingDir(solution, project_name, working_dir))
                        {
                            need_save = true;
                        }
                    }

                    if (config != null)
                    {
                        need_save = SetActiveConfig(config);
                    }

                    if (startup_project != null)
                    {
                        need_save = SetStartupProject(startup_project);
                    }

                    if (need_save)
                    {
                        if (found_open_solution == false)
                        {
                            ViaCOM.CallMethod(solution, "Close", null);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                retVal = 1;
            }
            finally
            {
                if (solution != null)
                {
                    Marshal.ReleaseComObject(solution);
                    solution = null;
                }

                if (dte != null)
                {
                    Marshal.ReleaseComObject(dte);
                    dte = null;
                }

                MessageFilter.Revoke();
            }
            return(retVal);
        }