如何以编程方式创建空的 Visual C++ 项目? [英] How can an Empty Visual C++ project be created programmatically?

查看:35
本文介绍了如何以编程方式创建空的 Visual C++ 项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用 Empty Visual C++ 项目的模板来以编程方式为这个空项目创建一个新的解决方案.我现在拥有的代码(C#)是:

I wanted to know how to use the template for an Empty Visual C++ project to programmatically create a new solution with this empty project. The code (C#) I have right now is:

string VSProgID = "VisualStudio.Solution.10.0";
Type solnObjType = System.Type.GetTypeFromProgID(VSProgID, true);
Solution4 soln = (Solution4) System.Activator.CreateInstance(solnObjType, true);
soln.Create(@"C:\NewSoln", "New");
soln.SaveAs(@"C:\NewSoln\New.sln");
string emptyVCProjPath = soln.GetProjectTemplate("General/Empty Project", "VC++"); // Here's where I need help
soln.AddFromTemplate(emptyVCProjPath, @"C:\NewSoln\NewProj", "New.vcxproj", false);

我无法在所有其他模板 (C#/F#/VB) 所在的位置找到 VC++ 空项目模板.当我通过 IDE 手动创建新项目时,VC++ 空项目模板确实出现在已安装的模板中.soln.GetProjectTemplate() 的参数应该是什么?它似乎没有将 "VC++" 识别为一种语言.我知道 "CSharp""VisualBasic" 是有效的选项.但我想要一个 VC++ 空项目.任何帮助将不胜感激.

I wasn't able to find the VC++ Empty Project template in the location where all the other templates (C#/F#/VB) are located. The VC++ Empty Project Template does appear in the installed templates when I create a new project manually through the IDE. What should the parameters be for soln.GetProjectTemplate()? It doesn't seem to recognize "VC++" as a language. I know that "CSharp" and "VisualBasic" are valid options. But I wanted a VC++ Empty Project. Any help will be greatly appreciated.

推荐答案

我发布这个答案是希望它可以帮助那些和我面临同样问题的人.

显然,Visual Studio 扩展性框架 API 不提供以编程方式创建空 Visual C++ 项目的选项.我必须生成解决方案和项目文件才能实现这一点.具体来说,必须生成 [solution_name].sln[project_name].vcxproj[project_name].vcxproj.filters 文件.必须为新项目生成 GUID 并将其插入适当的位置.如果需要向这个空项目添加文件,则需要为头文件生成ClInclude 标签,为源文件生成ClCompile 标签.这些标签被添加到 [project_name].vcxproj[project_name].vcxproj.filters 文件中.参考现有的 Visual C++ 项目的文件,以帮助您找出内容的来源.非常简单.

Apparently the Visual Studio Extensibility framework API doesn't provide an option to create an Empty Visual C++ project programmatically. I had to generate the solution and project files to achieve this. Specifically, the [solution_name].sln, [project_name].vcxproj, and [project_name].vcxproj.filters files had to be generated. A GUID had to be generated for the new project and inserted at the appropriate places. If one needs to add files to this empty project, ClInclude tags need to be generated for header files, and ClCompile tags for source files. These tags are added to the [project_name].vcxproj, and [project_name].vcxproj.filters files. Refer to an existing Visual C++ project's files to help you figure out what goes where. It is pretty straightforward.

更新

由于某种原因,这种方式生成的 VC++ 解决方案没有直接打开(通过在 Windows 资源管理器中双击解决方案文件).我必须启动 Visual Studio 并从那里打开解决方案.

For some reason, the VC++ solution generated this way does not open directly (by double-clicking the solution file in Windows Explorer). I have to launch Visual Studio and open the solution from there.

这篇关于如何以编程方式创建空的 Visual C++ 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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