从活动项目配置中获取调试器命令 [英] Getting debugger command from active project configuration

查看:26
本文介绍了从活动项目配置中获取调试器命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VSIX 包中,我必须获取用于活动启动配置的调试器命令.In other words, the command that would be executed when 'sturt under debugger' is selected.使用下面的代码,我能够获得启动项目的活动配置,但我无法弄清楚如何从代表启动项目的 IVSHierarchy 获取调试器命令.这甚至可能不回到 DTE 吗?

In a VSIX package I have to get the debugger command for active startup configuration. In other words, the command that would be executed when 'sturt under debugger' is selected. Using the code below I was able to get active configuration for startup project, but I can't figure out how to get the debugger command from IVSHierarchy representing the startup project. Is this even possible without going back to DTE?

private void GetStartupProject()
    {
        ThreadHelper.ThrowIfNotOnUIThread();
        IVsSolutionBuildManager bm = Package.GetGlobalService(typeof(IVsSolutionBuildManager)) as IVsSolutionBuildManager;
        int hr;
        IVsHierarchy project;
        hr = bm.get_StartupProject(out project);
        if (hr == VSConstants.S_OK)
        {
            project.GetProperty((uint)VSConstants.VSITEMID.Root, (int)__VSHPROPID.VSHPROPID_Name, out object projectName);
            IVsProjectCfg[] activeCfgs = new IVsProjectCfg[1];
            bm.FindActiveProjectCfg(IntPtr.Zero, IntPtr.Zero, project, activeCfgs);
            activeCfgs[0].get_DisplayName(out string activeCfgName);
            textOut.Text += String.Format("{0} {1}\r\n",(string)projectName, activeCfgName);
        }

    }

推荐答案

在花了一些时间调试并在 Ed Dore 的帮助下,我能够将获得完整调试命令和本机 C++ 和托管代码的工作目录的代码放在一起项目:

After spending some time debugging and with help from Ed Dore, I was able to put together code that gets complete debugging command and working dir for native C++ and managed code projects:

    private void ListStartupProperties()
    {
        ThreadHelper.ThrowIfNotOnUIThread();
        IVsHierarchy vsHierarchy = null;
        int hresult = bm.get_StartupProject(out vsHierarchy);
        object objProject = null;
        if(vsHierarchy != null)
            hresult = vsHierarchy.GetProperty((uint)VSConstants.VSITEMID.Root, (int)__VSHPROPID.VSHPROPID_ExtObject, out objProject);
        Project startupProject = (Project)objProject;

        if (startupProject != null)
        {
            foreach (Property prop in startupProject.Properties)
            {
                try
                {
                    textOut.Text += string.Format("{0} = {1}\r\n", prop.Name, prop.Value);
                }
                catch (Exception e)
                {
                    textOut.Text += e.Message + "\r\n";
                }
            }
            string cmd = "";
            string args = "";
            string wd = "";
            VCProject vcp = startupProject.Object as VCProject;
            if (vcp != null)
            {   // This is VC project
                VCConfiguration vcc = vcp.ActiveConfiguration;
                VCDebugSettings dbg = vcc.DebugSettings;
                cmd = vcc.Evaluate(dbg.Command);
                args = vcc.Evaluate(dbg.CommandArguments);
                wd = vcc.Evaluate(dbg.WorkingDirectory);
            }
            else
            {   // Probably C# or VB
                Configuration cfg = startupProject.ConfigurationManager.ActiveConfiguration;
                ProjectConfigurationProperties cfgProperties = cfg.Object as ProjectConfigurationProperties;
                if (cfgProperties != null)
                {
                    string outPath = cfgProperties.OutputPath;
                    string localPath = startupProject.Properties.Item("FullPath").Value as string;
                    string outputName = startupProject.Properties.Item("OutputFileName").Value as string;
                    cmd = cfgProperties.StartProgram != "" ? 
                        cfgProperties.StartProgram :
                        localPath + outPath + outputName;
                    args = cfgProperties.StartArguments;
                    wd = cfgProperties.StartWorkingDirectory;
                }
            }
            textOut.Text += string.Format("StartProgram = {0}\r\n", cmd);
            textOut.Text += string.Format("StartArguments = {0}\r\n", args);
            textOut.Text += string.Format("WorkingDir = {0}\r\n", wd);
        }
    }

这篇关于从活动项目配置中获取调试器命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆