Home > Microsoft .Net Development Tips > C# Development > Making C++ interact with Visual Basic and C# components
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

C# DEVELOPMENT

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


Davis Chapman
10.28.2002
Rating: --- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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;
      else
        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.

    Rate this Tip
    To rate tips, you must be a member of SearchWinDevelopment.com.
    Register now to start rating these tips. Log in if you are already a member.




    Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



    RELATED CONTENT
    C# Development
    Let Microsoft StyleCop tame your wild C#
    Picking a .NET smart client communications technology
    LINQ beyond queries: Strong-typed refection
    Book excerpt: An introduction to DSL tools
    Assembly versioning in the .NET Framework 2.0
    Book excerpt: Creating graphical output using the .NET Compact Framework
    On Extension Methods in C# and .NET Framework 3.5
    Generate RSA public and private keys, export to XML
    Book excerpt: Upgrading to Visual Studio 2005
    Learning .NET: Tips for getting started with .NET development

    Visual Basic and Visual Basic .NET
    Compose XML more quickly using Visual Basic 9
    Virtualization keeps Legacy Apps alive
    VB 9 Anonymous Types help create flexible objects
    Visual Studio Team System Add-ins: Conchango Scrum for Team System and Scrum Dashboard
    Book Excerpt: Sams Teach Yourself Visual Basic 2008 in 24 Hours -- Complete Starter Kit
    Check out CodePlex for a ton of interesting .NET projects
    Book excerpt: Murach's VB 2008
    Book excerpt: Printing in Visual Basic 2005
    Visual Basic 2008 and closures
    WinForms development using SQL Server 2005 and Visual Basic 2005

    C# programming language
    Inside Visual Studio 2010
    Mono 2.0 moves .NET apps to Linux - includes migration analyzer
    LINQ beyond queries: Strong-typed refection
    Assembly versioning in the .NET Framework 2.0
    Book excerpt: Creating graphical output using the .NET Compact Framework
    Visual Studio 2008 Learning Guide: C# 3.0
    Simonyi firm to address divide between domain experts and developers
    On Extension Methods in C# and .NET Framework 3.5
    Generate RSA public and private keys, export to XML
    Book excerpt: Upgrading to Visual Studio 2005

    RELATED GLOSSARY TERMS
    Terms from Whatis.com − the technology online dictionary
    C#  (SearchWinDevelopment.com)
    GLib  (SearchWinDevelopment.com)

    RELATED RESOURCES
    2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
    Search Bitpipe.com for the latest white papers and business webcasts
    Whatis.com, the online computer dictionary

    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



  • Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts