How can an Empty Visual C++ project be created programmatically?
Asked Answered
O

1

2

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

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.

Outgo answered 17/2, 2012 at 18:16 Comment(5)
Wouldn't it be enough to just create an empty VC++ project and use that as your template?Iain
I was considering that, but from what I gather so far, templates are supposed to be in ZIP format. With the empty project open, I try to use the "Export Template" feature but it is disabled. I'm not sure how to obtain the ZIP containing the necessary files to be used as a template. What I also don't understand is: what happens with the all GUIDs in the project files? Are they regenerated for the new project?Outgo
I also couldn't find a ready VC++ template. But my suggestion was to do it yourself, manually. Namely: Create an empty VC++ project and set it up as you like; Save it; Open in a text editor; Put placeholders for any data that you'd like to generate (e.g. #GUID#, #CPP_FILES#, etc). Now THIS is YOUR own VC++ template. When you want to create a new project, open you template file from your C# code, replace placeholders with appropriate data and you're done.Iain
Thanks for bringing this up. I also happened to consider this at one point. What distracted me again with this approach is having to generate GUIDs, and changing whatever other parameters that need to be changed. And if this is the approach I have to end up taking, I wonder why MS doesn't allow the use of the VS extensibility APIs to work with VC++ project templates like it does for C#, F#, and VB. This would suck. I still hope there is a way to use the framework as opposed to having to write code to do all the project file generation. I'll wait a little to see if someone can share some magic.Outgo
I guess MS is giving way less attention to good old C++. Still, maybe there's a VC++ template to download from somewhere. In case it doesn't work - then coding it manually shouldn't be too complicated. BTW, you could also traverse the template project file as an XML (e.g. with XDocument). Replacing GUIDs could become annoying, so you might eventually combine both techniques. Good luck!Iain
O
3

I am posting this answer in the hope that it may help someone facing the same problem that I was facing.

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.


UPDATE

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.

Outgo answered 1/3, 2012 at 20:1 Comment(3)
Your solution is very informative. I will start from there. Many thanks!Russon
Here is my solution: #32786264Russon
@Russon I didn't manipulate the XML directly. I used templates just like you, and Regex replace (because string replace didn't meet my needs). I left out the details because the finer details might be different for different project needs. But, it's great that you've included more detail in your solution. Thanks.Outgo

© 2022 - 2024 — McMap. All rights reserved.