- Mixing Languages: Realizing the Promise of the CLR
- Building a Managed C++ Component with a C# Client
- Building a C# Component with a Managed C++ Client
- Building an Unmanaged C++ Client
- Summary
- Q&A
Building a C# Component with a Managed C++ Client
The next example reverses the situation from the previous example. For this example, you'll create the component using either VB.NET or C#, and then create a managed C++ client that accesses the component. Because of the macros that will be used in the managed C++ client, if you want to try building the component in the other language from the original one selected, you'll need to start a completely new solution.
Building the C# or VB.NET Component
To start this example project, follow these steps:
Select New and then Project from the File menu.
To create the component in C#, select Visual C# Projects on the left side of the new project dialog. To create the component in VB, select Visual Basic Projects on the left side of the dialog. For both component types, select Class Library on the right side of the dialog. If you are creating a C# component, name the project CSTaxCalc. If you are creating a VB component, name the project VBTaxCalc. If the previous example project is still open, be sure that the Close Solution radio button is selected. Click OK to create the new project.
If you created a C# project, open the Class1.cs file. If you created a VB project, open the Class1.vb file. Replace all occurrences of Class1 with CTaxCalculations.
If you want to rename the file in which the component is located, select the Class1.cs or Class1.vb file in the Solution Explorer pane. Press F2, and rename the file TaxCalculations, being sure to keep the file extension unchanged (for example, if it's a C# project, the new filename should be TaxCalculations.cs; if it's a VB project, the new filename should be TaxCalculations.vb).
If you are creating a Visual Basic component, edit the source code file, adding the function in Listing 4 (you don't have the capability to use the Add Function dialog with the Visual Basic component). You can skip the rest of the steps in this section, as the VB component is complete at this point.
Listing 4The VB.NET CalculateTaxes Function
Public Class CTaxCalculations Public Function CalculateTaxes(ByVal fPurchaseAmt As Double, _ ByVal strCategory As String) As Double ' Declare the array of tax categories Dim strCategories() As String = {"food", "clothing", "music"} ' Declare the array of tax rates Dim fRates() As Double = {0.0725, 0.0835, 0.081} Dim i As Int32 ' Preset the default return value CalculateTaxes = 0 ' Loop through the categories For i = 0 To 2 ' Is this the category purchased? If (strCategory = strCategories(i)) Then ' Yes, calculate the taxes due CalculateTaxes = fPurchaseAmt * fRates(i) End If Next End Function End Class
If you are creating a C# component, select and right-click the CTaxCalculations class in the Class View pane. Select Add, Add Method from the context menu.
In the C# Method Wizard, specify the method access as public, the return type as double, the name as CalculateTaxes, and add two parameters. For the first parameter, specify the modifier as None, the parameter type as double, and the parameter name as dbPurchaseAmt. For the second parameter, specify the modifier as None, the parameter type as string, and the parameter name as strCategory. Click Finish to add this method.
Edit the method created, adding the code in Listing 5.
Listing 5The C# CalculateTaxes Function
public double CalculateTaxes(double dbPurchaseAmt, string strCategory) { // Declare the array of tax categories String[] strCategories = {"food", "clothing", "music"}; // Declare the array of tax rates double[] dbRates = {0.0725, 0.0835, 0.081}; // Loop through the categories for (int i=0; i < 3; i++) { // Is this the category purchased? if (strCategory == strCategories[i]) { // Yes, calculate the taxes due return dbPurchaseAmt * dbRates[i]; } } // Couldn't find the category purchased, return 0 return 0; }
Building the Managed C++ Client
The next step in this example creates the managed C++ client application that accesses the component that you created using VB or C#. To create the client, follow these steps:
In the Solution Explorer pane, select and right-click the solution node at the top of the tree. Select Add, New Project if you are creating this example for the first time. Select Add, Existing Project if you are creating this example for the second time, using the other language to create the component, and then navigate to the project directory of the managed C++ client and select the project file.
If you are adding a new project, select Visual C++ Projects on the left side of the dialog, and select Managed C++ Application on the right side. Name the project MgdClient. Click OK to create the project.
If you created the component using C#, add the boldfaced code in Listing 6. If you created the component using VB, add the boldfaced code, substituting VBTaxCalc for CSTaxCalc every occurrence in Listing 6.
Listing 6The Managed C++ Client Main Function
This is the main project file for VC++ application project // generated using an Application Wizard. #include "stdafx.h" #using <mscorlib.dll> [ic:Input]#using "CSTaxCalc.dll" using namespace System; // This is the entry point for this application #ifdef _UNICODE int wmain(void) #else int main(void) #endif { // TODO: Please replace the sample code below with your own. [ic:Input]CSTaxCalc::CTaxCalculations* pGetTax = new CSTaxCalc::CTaxCalculations(); double dbPurchaseAmt, dbTaxAmt, dbTotalDue; // Create the string for the category being purchased String* pstrCategory = S"clothing"; // Specify the purchase amount dbPurchaseAmt = 45.0; // Get the amount of taxes to collect dbTaxAmt = pGetTax->CalculateTaxes(dbPurchaseAmt, pstrCategory); if (dbTaxAmt > 0.0) { // Calculate the total due dbTotalDue = dbPurchaseAmt + dbTaxAmt; // Display the category being purchased Console::WriteLine(pstrCategory); // Display the purchase amount Console::Write("Purchase Amount = "); Console::WriteLine(dbPurchaseAmt.ToString("c")); // Display the tax due Console::Write(" Tax = "); Console::WriteLine(dbTaxAmt.ToString("c")); // Display the total amount Console::Write(" Total Due = "); Console::WriteLine(dbTotalDue.ToString("c")); } return 0; }
In the Solution Explorer pane, select and right-click the project node for the MgdClient project. Select Properties from the context menu.
On the Configuration combo box, select All Configurations.
On the left side of the Properties dialog, select Build Events, Pre-Build Events. On the right side of the dialog, in the Command Line entry, if you created the component using C#, enter:
copy "$(SolutionDir)\bin\$(ConfigurationName)\CSTaxCalc.dll" $(ConfigurationName)
If you created the component using VB, enter:
copy"$(SolutionDir)\bin\VBTaxCalc.dll" $(ConfigurationName)
On the left side of the dialog, select C/C++, General. On the right side of the dialog, in the Resolve #using References section, enter
$(outdir)
Click OK to accept the configuration changes and close the Properties dialog.
In the Solution Explorer pane, select and right-click the project node for the MgdClient project. Select Set as Startup Project from the context menu.
You are now ready to compile and run this example application. You should see the same output window that you saw for the previous example with the language roles reversed back in Figure 2.