MyAppVB6_VSTA2_Integration


posted by Melody
04-06-2009

Downloads: 634
File size: 384.6kB
Views: 2,816
MyAppVB6_VSTA2_Integration
Filed under: ,

This sample demonstrates a VB6 VSTA v 2.0 integration using a VB.Net integration library.  Below is from the included READ ME.doc

 

     There are many ways to integrate VSTA v 2.0 with a VB6 application.  This sample demonstrates an advanced integration option (with seamless non-destructive debugging but no macro management) which closely follows the integration code from the SDK samples. 

     A COM visible library, VSTA2_Integration, is referenced by the VB6 host application, MyAppVB6.  The VSTA2_Integration library exposes the classes VSTARunTimeIntegration and VstaDesignTimeIntegration.  The VB6 host first creates and connects to its VSTARunTimeIntegration instance passing in the object to be used as an entry point (see definition below) for add-ins through the VSTARunTimeIntegration.Connect method.  Then the VB6 host creates and connects to its VSTADesignTimeIntegration instance passing in the VSTARunTimeIntegration instance through the VSTADesignTimeIntegration.Connect method. 

     This host object (passed in through the VSTARunTimeIntegration.Connect method) is stored in the internal VSTA2_Integration.HostItemProvider (see definition below) which is passed into the add-ins through the VSTA pipeline.  Once the host object is passed through the pipeline, it is stored in the add-in in a hidden code file and accessed through “Me” or “Me.Application” (see section on templates below). 

     The VSTARunTimeIntegration.Connect method makes calls to load add-ins and the VSTARunTimeIntegration.Disconnect method makes calls to unload add-ins.  The VB6 host calls the VSTARunTimeIntegration.Connect method on form load, and VSTARunTimeIntegration.Disconnect on form unload.  This can easily be changed so that add-ins can be loaded and unloaded by selecting a button on the host. 

     The DesignTimeIntegration.Connect method takes in and stores the VSTARunTimeIntegration instance and prepares for IDE management.  The DesignTimeIntegration.ShowIDE method displays the IDE with a new or existing VSTA project open.  The DesignTimeIntegration.Disconnect method disposes of the IDE.  The VB6 host calls the DesignTimeIntegration.Connect on form load and the DesignTimeIntegration.Disconnect on form unload.  It is not advisable to change this.  The ShowIDE method is called through a button on the VB6 host.

     This sample does not use a proxy or HostTypeMapProvider, instead a “proxy shim" is included in the add-in to provide a MyAppVB6 entry point.  This class is defined in the hidden AppAddIn.designer2.xx file included with the project templates. 

     Two sets of templates are included with this sample.  The “MyAppVB6_VSTA2_Template_Robust.zip” templates allow the add-in to access the host object through “Me” or “this”.  The “MyAppVB6_VSTA2_Template.zip” templates allow the add-in to access the host object through “Me.Application” or “this.Application”.  For the robust templates, the hidden file AppAddIn.designer2.xx contains code which re-implements/exposes some or all of the entry point type from the VB6 host.  The only benefit to this additional code is it allows the host object to be accessed more directly through “Me” or “this” instead less directly through “Me.Application” or “this.Application”.  Which type of template to use is a design decision.

To use this sample:
   1)   Extract the sample to C:\VSTA.  If a different location is used the GeneratePIA files will need to be altered.
   2)   Run the included setup file Setup_MyAppVB6_VSTA2.js. This registers the host with VSTA and extracts the project templates.
   3)   Open and build the VSTA2_Integration solution.  Building this will register the library in the GAC.
   4)   Open the MyAppVB6 solution and build the MyAppVB6.exe.
   5)   Run the GeneratePIA.bat file to generate a PIA for the MyAppVB6.exe created above. 
   6)   Open and build the sample add-ins included.
   7)   Run the host from the exe, a sample add-in, or the VSTA2_Integration solution.
   8)   Select ShowIDE to launch the IDE.
   9)   Add code to the start-up method of the macro project and set a break point.
   10)  Begin debugging to hit the break point set above.

To use this sample to integrate VSTA v 2 into your VB6 host, you will need to:
   1) Update your host to reference and use the VSTA2_Integration library.
   2) Generate a PIA for your host.  This must be regenerated every time the exe is recreated because VB6 reassigns GUID’s with each build.
   3) Register your host with VSTA by adding registry values (see SDK)
   4) Update the project templates.  The minimum update required includes:
          a. HostID- in the xxproj files update the HostID
          b. Debug location- in the xxproj files update the DebugInfoExeName with the location of your host’s exe.
          c. Reference- in the xxproj files update the reference to MyAppVB6.dll with your host’s PIA.
          d. Entry point- 
                    i. the types in AppAddIn.designer2.xx files
                    ii. basetype node in the AppAddIn.designer.xml file with the EntryPoint type used in the AppAddIn.designer2.xx file.
          *once all project template updates are complete re-zip the template using no subfolders.
   5) Move the updated project templates to the ProjectTemplatesLocation used to register your host with VSTA.
   6) Run vsta setup for the host.

Definitions:

Entry Point- 
     An entry point is a type that is instantiated when the host application loads an add-in. The entry point also contains methods that can be called by the host application to initialize the add-in. Typically, the entry point is a host type that lives at the top of the object model hierarchy, such as an Application class that represents the host application, or a Document class that represents a document in the application.
An entry point is also the base class for a host item in the add-in developer's project. A host item is a class that provides a starting point for add-in developers to write their code, and is the class that add-in developers use to obtain all other objects that they use to automate the host application.

HostItemProvider- 
     A host item provider is a class that exposes host objects to add-ins. This class implements the IHostItemProvider interface in the host application. This interface defines a single method, GetHostObject. This method returns an instance of a type in the object model of the host application that corresponds to an entry point in the add-in.
When the host application loads an add-in, each entry point in the add-in calls the GetHostObject method to obtain an instance of its corresponding type in the host application. Each time the add-in calls a member of the entry point, the member uses this object to call into the corresponding member of the object in the host application.



*KNOWN ISSUES
1) VB6 event hook-ups can be lost through garbage collection as well as creating or showing a System.Windows.Forms object when the event is triggered.  We are working with Microsoft to resolve this issue.  For more information see the thread “VB6 event hookups dissappearing after X iterations, then throwing RCW error on event disconnect” on the MSDN Visual Basic Interop and Upgrade forum.

This issue was resolved by keeping a local instance of the source of the event. For details refer to the thread mentioned above or the blog Saving DTE and other COM Event Hook-ups.

 

2) After performing actions in the IDE such as building the host application’s exe will stay up after the host is closed.  We are working to resolve this issue and will post an update on www.Summsoft.com when a workaround is available.

Copyright Summit Software Company, 2008. All rights reserved.