Handling Multiple Languages in an Add-in

 < Free Open Study > 



Using Multiple Languages in the Add-in

Having created an add-in in C#, you are going to create a simple example of using multiple languages within the add-in. You are going to add a new project to the add-in. It will be a Visual Basic DLL and you will call it from the C# add-in's Exec method. It will also pass the application object (IDE instance) to the Visual Basic code so that it can reference the extensibility objects.

Adding a Visual Basic DLL to the Project

In order to add a new project to the add-in solution, right-click the solution in the Solution Explorer. In the pop-up menu, select Add New Project. Visual Studio will display the New Projects dialog box, which you have seen numerous times before. Select the Visual Basic Projects folder and single-click the Class Library icon.

Note 

The class library is the type of project that you need to select to create a dynamic link library (DLL). One of the great things about Visual Studio .NET is its capability to debug any number of DLLs in series within the single solution.

Single-click the icon to select the project type, because you want to enter a name for the project rather than let Visual Studio use a default project name. Then click the OK button to create the new DLL project and add it to the solution. You are adding it to the solution instead of creating a new one so that you can debug the DLL by calling it from the add-in. Once the project is created, the Solution Explorer will appear as shown in Figure 10-5. You add references to the EnvDTE and Extensibility namespaces by right-clicking the References folder in the new project. You then select Add New Reference in the pop-up menu. When the References dialog box appears, select the respective items.

click to expand
Figure 10-5: DLL project added to the add-in solution

In order for the C# project to call the Visual Basic DLL, you add a reference in the C# project to the Visual Basic project. Again, right-click the References folder in the C# project and select Add New Reference in the pop-up menu. This time, you need to choose the Projects tab in the References dialog box. Figure 10-6 shows the Projects tab and the Visual Basic project. You must reference the project because the DLL has not yet been built and will therefore not appear in the .NET tab of the dialog box. Click the Visual Basic project, and click the Select and OK buttons to add a reference to the DLL.

click to expand
Figure 10-6: Adding a reference to the DLL project

Listing 10-4 shows the code for the Visual Basic class and associated method that will be called by the C# add-in. Having added the references to the project, you also must import the two namespaces for EnvDTE and Extensibility. First, change the name of the generated Class1 to VBClass. Next, add a Public method named HelloWorld. This is the method that the C# add-in will call. You will notice that the method expects a parameter of type EnvDTE._DTE. This is the application object (IDE instance) that will be passed from the add-in. Although you are only going to access the name of the IDE instance, the DLL can now use the appObject to reference any object in the automation model, just as if it were the original Connect class in the add-in.

Listing 10-4: Visual Basic DLL Code

start example
 Imports Extensibility Imports EnvDTE Public Class VBClass    Public Sub HelloWorld(ByVal appObject As EnvDTE._DTE)       MsgBox("Hello World from VB DLL called by C# Add-in." & _                Chr(10) & "IDE Instance: " & appObject.Name)    End Sub End Class 
end example

Calling the Visual Basic DLL from the Add-in

Listing 10-5 shows the modifications that you make to the C# Exec method to call the Visual Basic DLL. The lines in boldface are for calling the DLL within an error-protected Try/Catch construct. The DLL is first instantiated by the following line of code:

 VBClass ovb = new VBClass(); 

Listing 10-5: Calling the Visual Basic DLL HelloWorld Method

start example
 public void Exec(string commandName,        EnvDTE.vsCommandExecOption executeOption,        ref object varIn,        ref object varOut,        ref bool handled) {    handled = false;    if(executeOption ==         EnvDTE.vsCommandExecOption.vsCommandExecOptionDoDefault)    {       if(commandName == "Chap10AddinCS.Connect.Chap10AddinCS")       {       MessageBox.Show ("You Rang?");          try          {             VBClass ovb = new VBClass();                ovb.HelloWorld(applicationObject);          }          catch(System.Exception e)          {             MessageBox.Show("Error calling VBDLL " +                e.Message);          }          handled = true;          return;       {    }    } 
end example

Next, the HelloWorld method of the DLL is called, passing the applicationObject variable that was set in the OnConnection method of the add- in. The following line of code calls the method:

 ovb.HelloWorld(applicationObject); 

Running the Add-in to Call the DLL

You have now completed the code for the new DLL written in Visual Basic. Although it is about as simple as a DLL can be, it is nevertheless a complete DLL that is capable of referencing any object in the IDE automation model. You have also added the code to the C# add-in that will call the DLL. This completes the coding for using multiple languages in an add-in or for any other type of Visual Studio .NET application.

To run the add-in, click the Debug Start button in the IDE for the add-in. This will automatically start a second instance of Visual Studio .NET. When the second instance of the IDE opens, open the Add-in Manager dialog box from the Tools menu. Check both boxes for the CH10AddinCS add-in, and then close the dialog box. At this time, the add-in will be connected and its menu item will be added to the Tools menu. If you select Tools CH10AddinCS, the C# add-in will display two message boxes. The first is from the add-in itself. The second comes from the Visual Basic DLL and will appear as shown in Figure 10-7.

click to expand
Figure 10-7: Message displayed from the Visual Basic DLL



 < Free Open Study > 



Writing Add-Ins for Visual Studio  .NET
Writing Add-Ins for Visual Studio .NET
ISBN: 1590590260
EAN: 2147483647
Year: 2002
Pages: 172
Authors: Les Smith

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net