从活动的项目配置中获取调试器命令

时间:2019-03-28 21:42:23

标签: vsix

在VSIX软件包中,我必须获取debugger命令以进行主动启动配置。换句话说,选择了“在调试器下启动”时将要执行的命令。使用下面的代码,我可以为启动项目获得有效的配置,但是我无法弄清楚如何从代表启动项目的IVSHierarchy中获取调试器命令。甚至不用返回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);
        }

    }

2 个答案:

答案 0 :(得分:1)

IVsProjectCfg interface不允许枚举各种配置属性,或包含允许您检索它们的方法。您可能已经怀疑,各种项目类型会通过自动化公开其设置,对于C#和VB.NET项目,这些设置将与使用EnvDTE / VSLangProj接口来检索给定配置的特定调试器属性相关。对于C#/ VB.NET项目,您将要检索/使用ProjectConfigurationProperties3 interface。例如:

private void OnGetDebuggerSettings(object sender, EventArgs e)
{
    ThreadHelper.ThrowIfNotOnUIThread();

    IVsHierarchy vsHierarchy = null;
    IVsSolutionBuildManager slnBuildMgr = (IVsSolutionBuildManager)GetService(typeof(SVsSolutionBuildManager));
    int hresult = slnBuildMgr.get_StartupProject(out vsHierarchy);
    object objProject = null;
    hresult = vsHierarchy.GetProperty((uint)VSConstants.VSITEMID.Root, (int)__VSHPROPID.VSHPROPID_ExtObject, out objProject);
    Project startupProject = (Project)objProject;

    // Note, cannot enumerate the ProjectConfigurationProperties, as it's not a collection interface
    // Refer to the documentation for ProjetConfigurationProperties3, or set a BP on the WriteLine below
    // and view the Dynamic View of the cfgProperties in the debugger's locals or watch window.
    Configuration cfg = startupProject.ConfigurationManager.ActiveConfiguration;
    ProjectConfigurationProperties3 cfgProperties = cfg.Object as ProjectConfigurationProperties3;
    if (cfgProperties!=null)
    {
        System.Diagnostics.Debug.WriteLine(cfgProperties.StartArguments);
    }
}

希望这可以帮助您启动并运行。

答案 1 :(得分:1)

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

    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);
        }
    }