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

问题描述 投票:0回答:2

在VSIX包中,我必须获得用于主动启动配置的调试器命令。换句话说,选择'sturt under 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);
        }

    }
vsix
2个回答
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
投票

在花了一些时间调试并在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);
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.