Problem solve Get help with specific problems with your technologies, process and projects.

Making C++ interact with Visual Basic and C# components

How to make C++ .NET work with components that have been developed in other languages.

This tip excerpted from InformIT, explains how to make C++ .NET work with components that have been developed in other languages.

You can create a COM component through ATL, and then wrap that component in a managed C++ wrapper to make it available to other CLR applications or objects. That provides you a path to making unmanaged C++ objects available for use in applications created with other CLR applications, but how does it really work? And can you go the other way, using .NET objects created in other languages in unmanaged C++ applications?

The quick answer is yes, you can use other .NET objects in unmanaged C++ applications, and you can create managed objects that can be used in other .NET languages. Although using objects created in managed C++ (or wrapped with managed C++) is much easier in other .NET languages, it's still possible to take it the other way, and use objects created in other .NET languages in both managed and unmanaged C++ applications.

Accessing objects created in other languages in C# and VB.NET projects is really very simple. If you've used previous versions of Visual Basic, you probably know how to add references to COM objects so that you can use those objects in your project. With VB.NET and C#, it's basically the same process that enables you to use other CLR objects, regardless of what language was used to create the objects, including managed C++.

When you create a C# (pronounced C-sharp, taken from musical notation) or VB.NET project in the Solution Explorer pane under the project node, you'll find a node labeled References. If you select and right-click this node, on the context menu you'll find the option Add Reference, which will open the Add Reference dialog.

On the .NET page, you'll find the various registered .NET objects available for use in your application. On the COM page, you'll find all the COM objects registered on your computer, including the component you created yesterday (assuming that you did create the example application yesterday). On the Projects page, you'll find a list of projects in the current solution, plus you can browse to locate other objects in other projects.

After you select an object to reference, click the Select button to add it to the list of referenced objects in the bottom portion of the dialog. If you need to remove an object reference, select it in the bottom portion and click the Remove button. After you add references to all the objects you need, click OK to add all those objects as nodes below the References folder on the Solution Explorer pane.

After you add references to the objects, you can freely include them in your VB.NET or C# code. For instance, if you added a reference to the component that you created yesterday into a C# project, you would use it as follows:

MgdGetTaxForPurchase pGetTax = new MgdGetTaxForPurchase();

if (pGetTax.GetPurchaseTaxes(fPurchaseAmt, pstrCategory, ref fTaxAmt))
If you're using the component in a VB.NET application, you'd use it as follows:

Dim pGetTax as MgdGetTaxForPurchase 

pGetTax = New MgdGetTaxForPurchase()
if (pGetTax.GetPurchaseTaxes(fPurchaseAmt, pstrCategory, byref fTaxAmt))

Now, if you go back to yesterday's example and try to create C# and VB.NET clients for the component, you'll find that these don't quite work correctly. A few changes in yesterday's managed C++ wrapper need to be made before you can use it in C# or VB.NET applications:

  • The amount of taxes to be calculated should be returned as a result, not passed in as a parameter.

  • With the float variables, you need to use Single variable types in VB.NET. Another option is to use double variables instead of float.

    After making these changes, your C# code would look like this:

    MgdGetTaxForPurchase pGetTax = new MgdGetTaxForPurchase();
    fTaxAmt = pGetTax.GetPurchaseTaxes(fPurchaseAmt, pstrCategory);

    And the VB.NET code would look like this:

    Dim pGetTax as MgdGetTaxForPurchase 
    pGetTax = New MgdGetTaxForPurchase()
    sgTaxAmt = pGetTax.GetPurchaseTaxes(sgPurchaseAmt, pstrCategory)

    The changes you need to make to your managed C++ wrapper look like the following:

    float MgdGetTaxForPurchase::GetPurchaseTaxes(float fPurchaseAmt,
          System::String* pstrCategory)
      wchar_t *pCat;
      int iLen;
      float fTax;
      // Convert the category from a BSTR to a wchar_t
      __wchar_t pCategory __gc[] = pstrCategory->ToCharArray();
      iLen = pstrCategory->get_Length();
      pCat = new wchar_t[iLen + 1];
      for (int i = 0; i < iLen; i++)
        pCat[i] = pCategory[i];
      pCat[i] = NULL;
      // Calculate the taxes due
      if (m_pTaxCalc->CalculateTaxes(fPurchaseAmt, pCat, &fTax))
        return fTax;
        return 0.0;

    To read the entire tip from which this tip is excerpted, you have to click over to InformIT. Then you'll have to register, if you haven't already done so, but registration is free.

  • Dig Deeper on C# programming language

    Start the conversation

    Send me notifications when other members comment.

    Please create a username to comment.