Use Addin project in another application

岁酱吖の 提交于 2020-01-05 17:32:14

问题


I have created a addin application with which i'm creating a solution with some files programtically. I have to integrate this in another winforms application. So once the user clicks the button a new project is created with some inputs dynamically.

In the addin project the form contains a method(OnConnection) which gets called automatically. This creates the project at run-time. The application works fine.

    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
            {
                _applicationObject = (DTE2)application;
                _addInInstance = (AddIn)addInInst;
                createProjectsFromTemplates(_applicationObject);
            }

public void createProjectsFromTemplates(DTE2 dte)
        {
            try
            {
                // Create a solution with two projects in it, based on project 
                // templates.
                Solution2 soln = (Solution2)dte.Solution;
                string csTemplatePath;
                //string vbTemplatePath;
                string csPrjPath = "\\\\#####.com\\###\\My Documents\\TestCreateProject";
                //string vbPrjPath = "C:\\UserFiles\\user1\\addins\\MyVBProject";

                // Get the project template path for a C# console project.
                // Console Application is the template name that appears in 
                // the right pane. "CSharp" is the Language(vstemplate) as seen 
                // in the registry.
                csTemplatePath = soln.GetProjectTemplate("WpfApplication.zip", "CSharp");
                System.Windows.Forms.MessageBox.Show("C# template path: " + csTemplatePath);

                // Get the project template path for a Visual Basic console
                // project.
                // "vbproj: is the DefaultProjectExtension as seen in the 
                // registry.
                //vbTemplatePath = soln.GetProjectTemplate("ConsoleApplication.zip", "vbproj");
                //System.Windows.Forms.MessageBox.Show("Visual Basic template path: " + vbTemplatePath);

                // Create a new C# console project using the template obtained 
                // above.
                soln.AddFromTemplate(csTemplatePath, csPrjPath, "NewWCFCSharpAutoGeneratorProject", false);

                // Create a new Visual Basic console project using the template 
                // obtained above.
                //soln.AddFromTemplate(vbTemplatePath, vbPrjPath, "New VB Console Project", false);


                Project prj;
                ProjectItem prjItem;

                String itemPath;
                // Point to the first project (the Visual Basic project).
                prj = soln.Projects.Item(1);

                prjItem = prj.ProjectItems.AddFromFileCopy("\\\\####.com\\###\\My Documents\\SampelCSharp.cs");
                // Retrieve the path to the class template.
                //itemPath = soln.GetProjectItemTemplate("MyClass.zip", "CSharp");
                //Create a new project item based on the template, in this
                // case, a Class.
                //prjItem = prj.ProjectItems.AddFromTemplate(itemPath, "MyNewClass.cs");
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("ERROR: " + ex.Message);
            }
        }

Now i have to use the add in project from another application(Winforms) in order to create the project at run-time. I have build the addin project and imported the .dll file to that application. I have created an instance for the class and got the "OnConnection" method.But i'm not sure about what to be passed as the parameters. Because when debugging the method it shows the "application" parameter carries some "COM" objects within it.

Also if the application runs once, the new project is created when executed again it says file already exists in the path. I have to overwrite the older one.

What is the solution for this?

Note: Using Visual studio 2012 and Framework 3.5


回答1:


Try this:

EnvDTE80.DTE2 dte2;
dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.11.0");

Connect objConnect = new Connect();
Array objArray = null;
objConnect.OnConnection(dte2, ext_ConnectMode.ext_cm_UISetup, null, ref objArray);


来源:https://stackoverflow.com/questions/21986644/use-addin-project-in-another-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!