public VisualStudioProcess(Process2 process)
        {
            if (process == null)
                throw new ArgumentNullException("process");

            r_process = process;
        }
        // Define an extension method for type System.Process that returns the command
        // line via WMI.
        private static bool GetCommandLine(Process2 process, out string executablePath, out string args)
        {
            executablePath = "";
            args           = "";
            if (process == null)
            {
                return(false);
            }
            string cmdLine = null;

            using (var searcher = new ManagementObjectSearcher(
                       $"SELECT CommandLine,ExecutablePath FROM Win32_Process WHERE ProcessId = {process.ProcessID}")) {
                // By definition, the query returns at most 1 match, because the process
                // is looked up by ID (which is unique by definition).
                var matchEnum = searcher.Get().GetEnumerator();
                if (matchEnum.MoveNext()) // Move to the 1st item.
                {
                    cmdLine        = matchEnum.Current["CommandLine"]?.ToString();
                    executablePath = matchEnum.Current["ExecutablePath"]?.ToString();
                }
            }
            if (cmdLine == null || executablePath == null)
            {
                // Not having found a command line implies 1 of 2 exceptions, which the
                // WMI query masked:
                // An "Access denied" exception due to lack of privileges.
                // A "Cannot process request because the process (<pid>) has exited."
                // exception due to the process having terminated.
                // We provoke the same exception again simply by accessing process.MainModule.
                //var dummy = process.MainModule; // Provoke exception.
                return(false);
            }
            args = cmdLine.Replace("\"" + executablePath + "\" ", "");
            return(true);
        }
Beispiel #3
0
        private AttachResult Attach(AttachType attachType)
        {
            string engine = _attachTypesMap[attachType];

            if (IsBeingDebugged())
            {
                return(AttachResult.BeingDebugged);
            }

            var dbg   = _dte.Debugger as Debugger2;
            var trans = dbg.Transports.Item("Default");

            var eng = trans.Engines.Item(engine);

            Process2 proc = null;

            try
            {
                proc = dbg.GetProcesses(trans, "").Item(_processName) as Process2;
            }
            catch (Exception)
            {
                return(AttachResult.NotRunning);
            }

            proc.Attach2(eng);

            return(AttachResult.Attached);
        }
Beispiel #4
0
        public void TestAttachThreadsBreakAllAndSetExitFlag()
        {
            string debugSolution = TestData.GetPath(@"TestData\DebugAttach\DebugAttach.sln");
            string startFile     = "fg.py";

            using (var app = new VisualStudioApp()) {
                var        dbg2            = (Debugger2)app.Dte.Debugger;
                SD.Process processToAttach = OpenSolutionAndLaunchFile(app, debugSolution, startFile, "", "");

                try {
                    Process2 proc = AttachAndWaitForMode(app, processToAttach, AD7Engine.DebugEngineName, dbgDebugMode.dbgRunMode);
                    dbg2.Break(WaitForBreakMode: false);
                    DebugProject.WaitForMode(app, dbgDebugMode.dbgBreakMode);

                    var x = proc.Threads.Cast <Thread2>()
                            .SelectMany <Thread2, StackFrame>(t => t.StackFrames.Cast <StackFrame>())
                            .SelectMany <StackFrame, Expression>(f => f.Locals.Cast <Expression>())
                            .Where(e => e.Name == "exit_flag")
                            .First();

                    Assert.IsNotNull(x, "Couldn't find a frame with 'exit_flag' defined!");

                    x.Value = "True";

                    dbg2.Go(WaitForBreakOrEnd: false);
                    DebugProject.WaitForMode(app, dbgDebugMode.dbgDesignMode);
                } finally {
                    if (!processToAttach.HasExited)
                    {
                        processToAttach.Kill();
                    }
                }
            }
        }
Beispiel #5
0
 public void OpenFolderSelectFilePath(FileInfo info)
 {
     if (info.DirectoryName == null)
     {
         return;
     }
     Process2.StartPath(xdg, info.DirectoryName);
 }
        public VisualStudioProcess(Process2 process)
        {
            if (process == null)
            {
                throw new ArgumentNullException("process");
            }

            r_process = process;
        }
Beispiel #7
0
        /// <inheritdoc />
        public AttachDebuggerResult AttachToProcess(Process process)
        {
            if (process == null)
            {
                throw new ArgumentNullException("process");
            }

            AttachDebuggerResult result = AttachDebuggerResult.CouldNotAttach;

            try
            {
                IVisualStudio visualStudio = GetVisualStudio(true, logger);
                if (visualStudio != null)
                {
                    visualStudio.Call(dte =>
                    {
                        EnvDTE.Process dteProcess = FindProcess(dte.Debugger.DebuggedProcesses, process);
                        if (dteProcess != null)
                        {
                            result = AttachDebuggerResult.AlreadyAttached;
                        }
                        else
                        {
                            dteProcess = FindProcess(dte.Debugger.LocalProcesses, process);
                            if (dteProcess != null)
                            {
                                Process2 dteProcess2   = dteProcess as Process2;
                                Debugger2 dteDebugger2 = dte.Debugger as Debugger2;
                                if (dteProcess2 != null && dteDebugger2 != null)
                                {
                                    IList <Guid> engineGuids = GetEngineGuids();
                                    Engine[] engines         = GetEngines(dteDebugger2, engineGuids);
                                    dteProcess2.Attach2(engines);
                                }
                                else
                                {
                                    dteProcess.Attach();
                                }

                                result = AttachDebuggerResult.Attached;
                            }
                            else
                            {
                                logger.Log(LogSeverity.Debug, "Failed to attach Visual Studio debugger to process because it was not found in the LocalProcesses list.");
                            }
                        }
                    });
                }
            }
            catch (VisualStudioException ex)
            {
                logger.Log(LogSeverity.Debug, "Failed to attach Visual Studio debugger to process.", ex);
            }

            return(result);
        }
        public void JobWithMissingDependency()
        {
            m_Manager.CreateEntity(typeof(EcsTestData), typeof(EcsTestData2));

            var job = new Process2().Schedule(EmptySystem);

            Assert.Throws <InvalidOperationException>(() => { new Process2().Schedule(EmptySystem); });

            job.Complete();
        }
Beispiel #9
0
        public void DoSomething()
        {
            var process1 = new Process1();
            var process2 = new Process2();
            var process3 = new Process3();

            process1.SetProcess(process2);
            process2.SetProcess(process3);

            process1.RunProcess();
        }
        static void Main(string[] args)
        {
            Process1 proc1 = new Process1();
            Process2 proc2 = new Process2();
            Process3 proc3 = new Process3();

            proc1.SetProcess(proc2);
            proc2.SetProcess(proc3);

            proc1.RunProcess();
        }
Beispiel #11
0
        //----------------------------------------------------------------------
        private static void KillBtExplorerExe()
        {
            var list = Process2.GetProcessesByName("BTExplorer");

            Debug.Assert(list.Length == 0 || list.Length == 1);
            foreach (var cur in list)
            {
                cur.Kill();
                Debug.WriteLine("Kill BTExplorer.exe");
            }
        }
 internal void Refresh(ThreadStackManager manager, Process2 process, Dictionary <int, String> threadNames)
 {
     try
     {
         r_nodeview.Refresh(manager, process, threadNames);
     }
     catch (Exception e)
     {
         Refresh(e);
     }
 }
        public void JobWithMissingDependency()
        {
            Assert.IsTrue(Unity.Jobs.LowLevel.Unsafe.JobsUtility.JobDebuggerEnabled, "JobDebugger must be enabled for these tests");

            m_Manager.CreateEntity(typeof(EcsTestData), typeof(EcsTestData2));

            var job = new Process2().Schedule(EmptySystem);

            Assert.Throws <InvalidOperationException>(() => { new Process2().Schedule(EmptySystem); });

            job.Complete();
        }
Beispiel #14
0
		public virtual IList<Engine> GetEngines(Process2 process)
		{
			if (ProcessData.CodeTypes.Count == 0)
				return null;

			var engines = ProcessData.CodeTypes
				.Select(type => process.Transport.Engines.Item(type))
				.Where(engine => engine != null)
				.ToList();

			if (engines.Count > 0)
				return engines;

			return null;
		}
        public void JobCalculateEntityCount()
        {
            m_Manager.CreateEntity(typeof(EcsTestData));
            m_Manager.CreateEntity(typeof(EcsTestData), typeof(EcsTestData2));
            m_Manager.CreateEntity(typeof(EcsTestData), typeof(EcsTestData2));
            m_Manager.CreateEntity(typeof(EcsTestData), typeof(EcsTestData2), typeof(EcsTestData3));
            m_Manager.CreateEntity(typeof(EcsTestData), typeof(EcsTestData2), typeof(EcsTestData3));

            var job = new Process1();

            Assert.AreEqual(5, job.CalculateEntityCount(EmptySystem));
            job.Schedule(EmptySystem).Complete();

            var job2 = new Process2();

            Assert.AreEqual(4, job2.CalculateEntityCount(EmptySystem));
            job2.Schedule(EmptySystem).Complete();
        }
Beispiel #16
0
        public void AttachLotsOfThreads(VisualStudioApp app)
        {
            string debugSolution = app.CopyProjectForTest(@"TestData\DebugAttach\DebugAttach.sln");
            string startFile     = "LotsOfThreads.py";

            var dbg2 = (Debugger2)app.Dte.Debugger;

            SD.Process processToAttach = OpenSolutionAndLaunchFile(app, debugSolution, startFile, "", "");
            System.Threading.Thread.Sleep(2000);

            try {
                Process2 proc = AttachAndWaitForMode(app, processToAttach, AD7Engine.DebugEngineName, dbgDebugMode.dbgRunMode);
            } finally {
                if (!processToAttach.HasExited)
                {
                    processToAttach.Kill();
                }
            }
        }
Beispiel #17
0
        public void CheckInput(String s)
        {
            Match m = reHostId.Match(s);

            if (!m.Success || dte == null)
            {
                return;
            }

            int      processId = Int32.Parse(m.Groups[1].Value);
            var      processes = dte.Debugger.LocalProcesses.Cast <Process2>().ToArray();
            Process2 process   = processes.Where(x => x.ProcessID == processId).Cast <Process2>().FirstOrDefault();

            if (process == null)
            {
                return;
            }

            process.Attach();
        }
Beispiel #18
0
        private static Process2 AttachAndWaitForMode(VisualStudioApp app, SD.Process processToAttach, object debugEngines, dbgDebugMode expectedMode)
        {
            Debugger2 dbg2 = (Debugger2)app.Dte.Debugger;

            System.Threading.Thread.Sleep(1000);
            Process2  result  = null;
            Transport t       = dbg2.Transports.Item("Default");
            bool      foundit = false;

            foreach (Process2 p in dbg2.LocalProcesses)
            {
                if (p.ProcessID == processToAttach.Id)
                {
                    foundit = true;
                    p.Attach2(debugEngines);
                    result = p;
                    break;
                }
            }
            Assert.IsTrue(foundit, "The process to attach [{0}] could not be found in LocalProcesses (did it exit immediately?)", processToAttach.Id);
            DebugProject.WaitForMode(app, expectedMode);
            return(result);
        }
        private void ProcessBreakMode()
        {
            try
            {
                Process2 process = (Process2)r_dte.Debugger.DebuggedProcesses.Item(1);

                Dictionary <int, String> threadNames = new Dictionary <int, string>();
                Threads threads = process.Threads;
                int     count   = threads.Count;
                for (int i = 1; i <= count; ++i)
                {
                    Thread thread = threads.Item(i);
                    int    id     = thread.ID;
                    String name   = thread.Name;
                    threadNames.Add(id, name);
                }

                int procid = process.ProcessID;
                System.Diagnostics.Process proc = System.Diagnostics.Process.GetProcessById(procid);

                using (Dac dac = new Dac(proc))
                {
                    ThreadStackManager  man    = dac.ConstructThreadStacks();
                    VisualStudioProcess vsProc = new VisualStudioProcess(process);
                    r_myControl.Refresh(man, vsProc, threadNames);
                }
            }
            catch (ExpiredException ex)
            {
                r_myControl.Refresh(ex);
            }
            catch (Exception ex)
            {
                r_myControl.Refresh(ex);
            }
        }
 internal void Refresh(ThreadStackManager manager, Process2 process, Dictionary<int, String> threadNames)
 {
     try
     {
         r_nodeview.Refresh(manager, process, threadNames);
     }
     catch (Exception e)
     {
         Refresh(e);
     }
 }
 public ProcessNodeFactory(Process2 process)
 {
     _process = process;
 }
        /// <summary>
        /// 锁定
        /// </summary>
        /// <returns></returns>
        public async void SystemLock(int waitSecond = 30)
        {
            await Task.Delay(waitSecond * 1000);

            Process2.Start("rundll32.exe", "user32.dll,LockWorkStation", true);
        }
        /// <summary>
        /// 关闭系统
        /// </summary>
        /// <param name="waitSecond">等待秒数</param>
        public async void SystemShutdown(int waitSecond = 30)
        {
            await Task.Delay(waitSecond * 1000);

            Process2.Start("shutdown", "/s /t 0", true);
        }
Beispiel #24
0
 public ProcessNodeFactory(Process2 process)
 {
     _process = process;
 }
Beispiel #25
0
 public void OpenFolderByDirectoryPath(DirectoryInfo info)
 {
     Process2.StartPath(xdg, info.FullName);
 }
        public static void Commands()
        {
            while (true)
            {
                string command = Console.ReadLine();
                switch (command)
                {
                case "exit":
                {
                    Process Process;
                    Process = Process.GetCurrentProcess();
                    Process.CloseMainWindow();
                    return;
                }

                case "restart":
                {
                    Process Process;
                    Process Process2;
                    Process2 = Process.GetCurrentProcess();
                    Log.logwriter.Close();
                    Process.Start("!PiCaSsO!.exe");
                    Process2.Kill();
                    return;
                }

                case "help":
                {
                    ColoredConsole.ConsoleWriteBlueWithOut("!PiCaSsO!-Help:\nhelp -displays this help-\nrestart -restarts the server-\nexit -shuts the server down-\ncls -clears the console window-");
                    break;
                }

                case "cls":
                {
                    Console.Clear();
                    break;
                }

                case "unregister":
                {
                    RegistryManager.RegistryManager.UnRegisterKey();
                    break;
                }

                default:
                {
                    if (command.StartsWith("register"))
                    {
                        if (command.Length >= 8)
                        {
                            RegistryManager.RegistryManager.RegisterKey(command.Substring(command.LastIndexOf(" ") + 1, command.Length - 9));
                        }
                        else
                        {
                            ColoredConsole.ConsoleWriteErrorWithOut("Invalid key.");
                        }
                    }
                    else
                    {
                        ColoredConsole.ConsoleWriteErrorWithOut("Unknown command: " + command);
                    }
                    break;
                }
                }
            }
        }
 public ProcessItem(Process2 process)
 {
     Process = process;
 }
            static OpenResultCode OpenCoreByProcess(string url)
            {
                var r = Process2.OpenCoreByProcess(url, s => Toast.Show(s));

                return(r ? OpenResultCode.StartedByProcess2 : OpenResultCode.Exception);
            }
Beispiel #29
0
 protected override JobHandle OnUpdate(JobHandle inputDeps)
 {
     inputDeps = new Process1().Schedule(this, inputDeps);
     inputDeps = new Process2().Schedule(this, inputDeps);
     return(inputDeps);
 }
Beispiel #30
0
    static void Main(string[] args)
    {
        String                prjTemplDir    = Path.Combine(vsInstallPath, @"Common7\IDE\ProjectTemplates");
        List <String>         vsTemplates    = Directory.GetFiles(prjTemplDir, "*.vstemplate", SearchOption.AllDirectories).Select(x => x.Substring(prjTemplDir.Length + 1)).ToList();
        Regex                 re             = new Regex("^(.*?)" + Regex.Escape(@"\"));
        Func <String, String> untilBackSlash = (s) => { return(re.Match(s).Groups[1].ToString()); };
        List <String>         languages      = vsTemplates.Select(x => untilBackSlash(x)).ToList();
        List <String>         vsNames        = new List <string>();

        for (int i = 0; i < vsTemplates.Count; i++)
        {
            bool   keep = true;
            String lang = languages[i];
            if (lang != "CSharp" && lang != "VC" && lang != "Javascript")
            {
                keep = false;
            }

            if (
                vsTemplates[i].Contains("WebTemplate")  // has wizard
                ||
                vsTemplates[i].Contains("WapProj")      // hangs
                )
            {
                keep = false;
            }

            if (!keep)
            {
                vsTemplates.RemoveAt(i);
                languages.RemoveAt(i);
                i--;
                continue;
            }
            vsTemplates[i] = vsTemplates[i].Substring(languages[i].Length + 1);
            vsNames.Add(vsTemplates[i].Replace("\\", "_").Replace("1033_", "").Replace(".vstemplate", "").Replace(".", ""));
        }

        //var procs = System.Diagnostics.Process.GetProcesses().Where(x => x.ProcessName == "devenv").ToArray();
        //String devenvPath = @"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\devenv.exe";
        //Assembly asm = Assembly.LoadFile(devenvPath);

        DTE2 dte = null;

        try
        {
            //String[] allPaths = {
            //    Path.Combine(vsInstallPath, @"Common7\IDE\PublicAssemblies"),
            //    // VCProjectShim is here.
            //    Path.Combine(vsInstallPath, @"Common7\IDE\CommonExtensions\Microsoft\VC\Project")
            //};

            //AppDomain.CurrentDomain.AssemblyResolve += (s, asmArgs) =>
            //{
            //    String dllName = new AssemblyName(asmArgs.Name).Name + ".dll";

            //    foreach (String dir in allPaths)
            //    {
            //        string path = Path.Combine(dir, dllName);

            //        if (!File.Exists(path))
            //            continue;

            //        return Assembly.LoadFrom(path);
            //    }

            //    Console.WriteLine("Warning: Required assembly not found: " + dllName);
            //    return null;
            //};

            //dte = (DTE)Activator.CreateInstance(Type.GetTypeFromProgID("VisualStudio.DTE.10.0"), true);
            //dte = (DTE)Activator.CreateInstance(Type.GetTypeFromProgID("VisualStudio.DTE.12.0"), true);
            //dte = (DTE)Activator.CreateInstance(Type.GetTypeFromProgID("VisualStudio.DTE.16.0"), true);

            bool bNormalStart = false;

            MessageFilter.Register();
            if (bNormalStart)
            {
                dte = (DTE2)Activator.CreateInstance(Type.GetTypeFromProgID(vsId), true);
            }
            else
            {
                //Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider.GetService()
                bool     bAttached         = false;
                int      processId         = 0;
                Process2 processToAttachTo = null;

                for (int iTry = 0; iTry < 2; iTry++)
                {
                    dte = (DTE2)Marshal.GetActiveObject(debuggerVsId);

                    var       processes = dte.Debugger.LocalProcesses.Cast <EnvDTE.Process>().ToArray();
                    Debugger2 debugger2 = (Debugger2)dte.Debugger;

                    //
                    // https://varionet.wordpress.com/tag/debug/
                    // Attach2 sometimes triggers error: 8971001E, need to detach from all processes.
                    // Something to do debugging multiple processes?
                    //
                    debugger2.DetachAll();

                    int       c         = debugger2.Transports.Count;
                    Transport transport = debugger2.Transports.Item(1 /* Default transport */);
                    //foreach (var ix in transport.Engines)
                    //{
                    //    Engine e = ix as Engine;

                    //    String name = e.Name;
                    //    Console.WriteLine("'" + name + "'");
                    //}

                    // Otherwise will get timeout while trying to evaluate any com object in visual studio watch window
                    String debuggerType = "Managed (v3.5, v3.0, v2.0)";
                    //String debuggerType = "Managed (v4.6, v4.5, v4.0)";
                    //String debuggerType = "Managed";
                    Engine[] engines = new Engine[] { transport.Engines.Item(debuggerType) };

                    foreach (var process in processes)
                    {
                        String name = Path.GetFileNameWithoutExtension(Utils.call(() => (process.Name))).ToLower();
                        if (name != "devenv")
                        {
                            continue;
                        }

                        processId = Utils.call(() => (process.ProcessID));
                        String cmdArgs = GetProcessCommandLine(processId);

                        if (cmdArgs == null || !cmdArgs.Contains("-Embedding"))
                        {
                            continue;
                        }

                        processToAttachTo = process as Process2;
                        //Console.ReadKey();
                        Console.WriteLine("Attaching to: " + processId);
                        Utils.callVoidFunction(() => { processToAttachTo.Attach2(engines); });
                        // Apparently it takes some time for debugger to attach to process, otherwise missing breakpoints.
                        // Not sure if some sort of wait could be triggerred.
                        //System.Threading.Thread.Sleep(2000);
                        bAttached = true;
                        break;
                    }

                    if (bAttached)
                    {
                        break;
                    }

                    {
                        if (iTry == 1)
                        {
                            Console.WriteLine("Error: Failed to launch vs2017/2019.");
                            return;
                        }

                        // Analogue of
                        // Activator.CreateInstance(Type.GetTypeFromProgID(vsId), true);
                        // only with  experimental visual studio version.
                        Start_devenv_Embedded();
                    }
                }

                dte = (DTE2)GetDTE(processId, 120);
                //dte = null;
            }

            String edition = dte.Edition;
            Console.WriteLine("Edition: " + edition);
            Console.WriteLine("-----------------------------------------------");

            // Make it visible.
            if (!dte.MainWindow.Visible)
            {
                dte.MainWindow.Visible = true;
                dte.UserControl        = true;
            }

            Solution2 sln2 = (Solution2)dte.Solution;
            //sln2.Open(@"C:\PrototypingQuick\ProjectGen\Solution2.sln");
            //sln2.Open(@"C:\PrototypingQuick\ProjectGen\Solution.sln");
            //sln2.Open(@"C:\PrototypingQuick\ProjectGen\Solution1.sln");
            //sln2.Close();

            // Add existing project to solution
            //String projPath = @"D:\PrototypingQuick\ProjectGen\Dll1\Dll1.vcxproj";
            //String projPath = @"D:\PrototypingQuick\ProjectGen\Dll1\Dll2.vcxproj";
            //sln2.AddFromFile(projPath);
            //sln2.SolutionBuild.Build();

            //Microsoft.VisualStudio.OLE.Interop.IServiceProvider serv = (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)dte;
            //dte.ExecuteCommand("File.InvokeOpenSyncProjectFile", "args");
            //dte.ExecuteCommand("File.InvokeOpenSyncProjectFile");
            //dte.ExecuteCommand("File.InvokeOpenSyncProjectFile", "thisIsArg1");
            //dte.ExecuteCommand("Tools.InvokeExecuteScript", @"D:\Prototyping\cppscriptcore\Tools\vsDev\bin\Debug\vsDev.dll");
            //int cmdCount = Utils.call(() => (dte.Commands.Count));


            //for( int i = 1; i <= cmdCount; i++)
            //{
            //    Command c = Utils.call(() => dte.Commands.Item(i));
            //    Console.WriteLine(Utils.call(() => c.Name));
            //    Console.WriteLine( "    " + Utils.call(() => c.ID));
            //    Console.WriteLine( "    " + Utils.call(() => c.Guid.ToString()) );
            //    Console.WriteLine();
            //}

            //Guid service = new Guid("89BE061A-1103-4D1D-8293-A51F8480E202");
            //Guid serviceApi = new Guid("89BE061A-1103-4D1D-8293-A51F8480E201");
            //IntPtr obj = IntPtr.Zero;
            //int r = serv.QueryService(ref service, ref serviceApi, out obj);

            //Console.WriteLine("[ Press any key to close ... ]");
            //Console.ReadKey();

            /*
             * // Generate huge amount of temporary projects.
             * Solution2 sln2 = (Solution2)dte.Solution;
             * String mainDir = @"d:\Prototyping\testsln";
             * String slnTempDir = Path.Combine(mainDir, "slnTemp");
             *
             *
             * for (int i = 0; i < vsTemplates.Count; i++)
             * {
             *  String name = vsNames[i];
             *  String dir = Path.Combine(mainDir, name);
             *  bool bKeepProject = true;
             *
             *  if (Directory.Exists(dir))
             *      Directory.Delete(dir, true);
             *
             *  Console.Write("Project '" + name + "... " );
             *  Directory.CreateDirectory(dir);
             *  sln2.Create(slnTempDir, name);
             *
             *  try
             *  {
             *      string csTemplatePath = sln2.GetProjectTemplate(vsTemplates[i], languages[i]);
             *      sln2.AddFromTemplate(csTemplatePath, dir, name, false);
             *      Console.WriteLine("ok." );
             *  }
             *  catch (Exception ex)
             *  {
             *      Console.WriteLine("Project '" + name + ": " + ex.Message);
             *      bKeepProject = false;
             *  }
             *  if(bKeepProject)
             *      sln2.SaveAs(Path.Combine(dir, name + ".sln"));
             *  sln2.Close(bKeepProject);
             *
             *  if (!bKeepProject)
             *      Directory.Delete(dir, true);
             * }
             */


            Solution sln = dte.Solution;

            /*
             * String slnPath = @"c:\Prototyping\testsln";
             * if( !sln.IsOpen )
             *  sln.Create(slnPath, "test");
             * Solution2 sln2 = (Solution2) sln;
             * Solution3 sln3 = (Solution3) sln;
             *
             * int nCount = sln.Projects.Count;
             *
             * dte.Events.SolutionEvents.ProjectAdded += SolutionEvents_ProjectAdded;
             * dte.Events.SolutionItemsEvents.ItemAdded += SolutionItemsEvents_ItemAdded;
             * dynamic events = dte.Events.GetObject("CSharpProjectItemsEvents");
             * //Events2 events2 = dte.Events as Events2;
             * //events2.ProjectItemsEvents.ItemAdded += Pievents_ItemAdded;
             * pievents = events as ProjectItemsEvents;
             * pievents.ItemAdded += Pievents_ItemAdded;
             *
             *
             * if (nCount <= 0)
             * {
             *  string csTemplatePath = sln2.GetProjectTemplate(@"Windows\1033\ConsoleApplication\csConsoleApplication.vstemplate", "CSharp");
             *  sln.AddFromTemplate(csTemplatePath, slnPath + "\\prj", "Foo", false);
             *  dte.ExecuteCommand("File.SaveAll");
             * }
             */

            //sln.Open(@"D:\PrototypingQuick\ConsoleApplication2\ConsoleApplication2.sln");
            while (sln.Projects.Count == 0)
            {
                Console.WriteLine("Please open solution in newly opened visual studio and then press enter to continue...");
                Console.ReadLine();
            }

            // Enumerate available configurations within a project
            Project p = sln2.Projects.Item(1);

            //
            // Get / set .NET framework version, https://blogs.msdn.microsoft.com/visualstudio/2010/02/25/how-to-retarget-a-project-using-dte/
            //
            // ".NETFramework,Version=v4.7.2"
            //
            String DotNetVersion = p.Properties.Item("TargetFrameworkMoniker").Value;


            var oConfs = p.ConfigurationManager.Cast <Configuration>().ToArray();
            var confs  = oConfs.Select(x => x.ConfigurationName + "|" + x.PlatformName).ToArray();

            foreach (String c in confs)
            {
                Console.WriteLine("- " + c);
            }

            // C#/C++ project properties scanning
            //Project p = sln.Projects.Item(1);
            //Dictionary<String, Object> d = new Dictionary<string, object>();
            //foreach (Property prop in p.Properties)
            //{
            //    try
            //    {
            //        d[prop.Name] = prop.Value;
            //    }
            //    catch (Exception)
            //    {
            //    }
            //}

            // C++ project model scanning
            //VCProject vcProject = p.Object as VCProject;
            //VCProject vcProject = (VCProject)p.Object;

            //if (vcProject == null)
            //{
            //    MessageFilter.Revoke();
            //    Console.WriteLine("Not a C++ project or vs2017 or later (registry moved to file problem).");
            //    Console.WriteLine("[Press any key to close]");
            //    Console.ReadLine();
            //    return;
            //}

            //VCProjectEngine peng2 = vcProject.VCProjectEngine as VCProjectEngine;
            //VCProjectEngineShim peng = peng2 as VCProjectEngineShim;
            //var sp = peng.VsServiceProvider;

            // Scan for all subprojects.
            List <Project> projects = GetProjects(sln);
            foreach (Project genProj in projects)
            {
#if !OLD_VS
                VSProject2 sharpProj = genProj.Object as VSProject2;
#else
                VSProject2 sharpProj = genProj.Object as VSProject2;
                //VSProject3 sharpProj = genProj.Object as VSProject3;
                //VSProject4 sharpProj = genProj.Object as VSProject4;
#endif
                VCProject cppProj = genProj.Object as VCProject;

                String name = genProj.Name;
                Console.Write("Project: " + name + " - language( ~" + genProj.Kind + "): ");
                if (sharpProj == null && cppProj == null)
                {
                    Console.WriteLine("Unknown");
                    continue;
                }

                if (sharpProj != null)
                {
                    Console.WriteLine("C#");
                }

                if (cppProj != null)
                {
                    Console.WriteLine("C++ ");
                }

                //foreach (Reference r in ((References)vsp2.References).Cast<Reference>())
                //{
                //    Console.WriteLine(r.Path);
                //    Console.WriteLine("CopyLocal = " + r.CopyLocal);
                //}
            }

            Console.WriteLine("[Press any key to close]");
            Console.ReadLine();

            // Get project GUID
            //IVsSolution service = GetService( dte, typeof(IVsSolution)) as IVsSolution;

            //String uname = p.UniqueName;
            //IVsHierarchy hierarchy;
            //service.GetProjectOfUniqueName(uname, out hierarchy);
            //Guid projectGuid;
            //hierarchy.GetGuidProperty(Microsoft.VisualStudio.VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ProjectIDGuid, out projectGuid);

            //Console.WriteLine("Project guid: " + projectGuid.ToString());

            // Add file in programming language independent manner.
            //p.ProjectItems.AddFromFile(@"D:\Prototyping\cppscriptcore\cppscript\cppscript.cpp");

            //if (vcProject != null)
            //{
            //foreach (object oFile in (IVCCollection)vcProject.Files)
            //{
            //    VCFile file = oFile as VCFile;
            //    Console.WriteLine(file.Name);
            //    Console.WriteLine(" " + file.RelativePath);

            //    foreach (var _conf in (IVCCollection)file.FileConfigurations)
            //    {
            //        VCFileConfiguration conf = _conf as VCFileConfiguration;
            //        Console.WriteLine(conf.Name);

            //        VCCLCompilerTool compilerTool = conf.Tool as VCCLCompilerTool;
            //        if (compilerTool == null)
            //            continue;

            //        Console.WriteLine("Defines: " + compilerTool.PreprocessorDefinitions);
            //    }
            //}


            //VCFilter f = null;
            //foreach (object oItem in (IVCCollection)vcProject.Items)
            //{
            //    VCFile file = oItem as VCFile;
            //    VCFilter fitem = oItem as VCFilter;

            //    if (fitem != null && fitem.Name == "Test1")
            //        f = fitem;
            //}

            //if( f == null )
            //    f = vcProject.AddFilter("Test1") as VCFilter;
            //f.AddFile(@"D:\Prototyping\cppscriptcore\cppscript\cppscript.cpp");


            // Recursive files / folder adding / C++ project
            //String fromDir = @"C:\Prototyping\vlc-3.0.2";

            //List<String> files = Directory.GetFiles(fromDir, "*.c", SearchOption.AllDirectories).ToList();
            //files.AddRange(Directory.GetFiles(fromDir, "*.h", SearchOption.AllDirectories));
            //files = files.Select(x => x.Substring(fromDir.Length + 1)).ToList();

            //Stopwatch sw = Stopwatch.StartNew();

            //foreach (String file in files)
            //{
            //    String[] pp = file.Split('\\');
            //    IVCCollection items = (IVCCollection)vcProject.Items;
            //    VCFilter parent = null;
            //    VCFilter filter = null;

            //    for (int i = 0; i < pp.Length - 1; i++)
            //    {
            //        filter = items.OfType<VCFilter>().Where(x => x.Name == pp[i]).FirstOrDefault();
            //        if (filter == null)
            //            if (i == 0)
            //                filter = (VCFilter)vcProject.AddFilter(pp[i]);
            //            else
            //                filter = (VCFilter)parent.AddFilter(pp[i]);

            //        parent = filter;
            //        items = (IVCCollection)parent.Items;
            //    }

            //    String fullpath = Path.Combine(fromDir, file);
            //    if (filter == null)
            //        vcProject.AddFile(fullpath);
            //    else
            //        filter.AddFile(fullpath);
            //}

            //sw.Stop();
            //Console.WriteLine(sw.ElapsedMilliseconds);
            //}

            MessageFilter.Revoke();
            //Console.WriteLine();
            //Console.ReadKey();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            Console.WriteLine("Stack trace: " + ex.StackTrace);
            Console.ReadKey();
        }
        finally
        {
            // Need to close solution, so devenv.exe would not remain hanging in memory.
            //if (dte != null)
            //    dte.Solution.Close();
        }
    }