Building the Project


When building an application with multiple languages, you need to build a new project for each language, because a different .NET language compiler is involved with each language. The Solution Explorer makes dealing with multiple projects of different languages easy and keeps them all within the same solution. An advantage of having multiple projects that use different compilers within a solution is that the build process can be performed on all the projects at one time with just a single step.

Creating the Application and Component Projects

The first step in building a .NET application that utilizes multiple programming languages is to decide what the primary language will be for the application. This is the language that will ultimately produce the executable file. For this project, it makes sense to pick either C# or Visual Basic because both of those languages have better Windows Form support than Visual C++, and this application is a Windows Form application.

Because it is a toss-up between VB and C#, this application will use Visual Basic as the primary language. Use the File, New, Project menu item to create a Visual Basic Windows application named IntegratedApp, as shown in Figure 6.1.

Figure 6.1. The New Project dialog for IntegratedApp in Visual Basic .NET.


You should now have an empty Visual Basic .NET Windows application with a blank Windows Form in the Form Designer. The next step is to create the two components in C# and C++. From the Solution Explorer, right-click the solution and select Add, New Project from the context menu. For the project type, select the Visual C# Projects item. Then select Class Library in the templates section and give it the name CSLibrary, as shown in Figure 6.2.

Figure 6.2. The Add New Project dialog for CSLibrary in Visual Basic .NET.


Finally, repeat the process of creating a class library for C++ by selecting to create a new managed C++ class library from the Add New Project dialog and name it CPPLibrary.

Defining the C++ Class

Open the file CPPLibrary.h, where you will find a namespace declaration with a generic class definition. Writing a class library DLL is very simple with the .NET Framework; any namespaces and public classes are automatically made available to any user of the component library. The project wizard already created a sample class for you, so all you have to do is simply change the given name to a more appropriate name for our application. Change the class from the name Class1 to CPPClass. The next step is to add a member function. In the Class View, find the CPPClass definition within the CPPLibrary project. Right click the class and select Add, Add Function. In the Add Function dialog displayed, set the return value to String* and the function name to GetMsg. Then click Finish. You should now be looking at the C++ header file once again, and you'll notice the new function within the class. Instead of returning NULL, however, it returns a string instead, as shown on line 14 of Listing 6.1.

Listing 6.1 The CPPClass Definition in CPPLibrary
 1: // CPPLibrary.h  2:  3: #pragma once  4:  5: using namespace System;  6:  7: namespace CPPLibrary  8: {  9:     public __gc class CPPClass 10:     { 11:     public: 12:        String* GetMsg() 13:        { 14:           return( "Hello from managed C++" ); 15:        } 16:     }; 17: } 

At this point, CPPClass has one method, GetMsg(), that returns a pointer to a String object containing a message. Adding an unmanaged class, CPPUnmanagedClass, to the namespace is done by adding the code shown in Listing 6.2.

Listing 6.2 The CPPUnmanagedClass Definition in CPPLibrary
 1: // CPPLibrary.h  2:  3: #pragma once  4:  5: using namespace System;  6:  7: namespace CPPLibrary  8: {  9: #pragma unmanaged 10:    class CPPUnmanagedClass 11:    { 12:    public: 13:       LPCTSTR GetMsg() 14:       { 15:          return( "Hello from unmanaged C++" ); 16:       } 17:    }; 18: 19: #pragma managed 20: 21:    public __gc class CPPClass 22:    { 23:     public: 24:        String* GetMsg() 25:        { 26:           return( "Hello from managed C++" ); 27:        } 28:    }; 29: } 

Because other .NET languages cannot call unmanaged code directly, you need to wrap the usage with managed code. Therefore, creating a new method in CPPClass that is managed and uses CPPUnmanagedClass would allow other .NET languages to effectively use the unmanaged code. Adding the method GetUnmanagedMsg(), as shown in Listing 6.3, provides the necessary wrapper.

Listing 6.3 The GetUnmanagedMsg() Declaration in CPPClass
 1: using namespace System;  2:  3: namespace CPPLibrary  4: {  5: #pragma unmanaged  6:    class CPPUnmanagedClass  7:    {  8:    public:  9:       LPCTSTR GetMsg() 10:       { 11:          return( "Hello from unmanaged C++" ); 12:       } 13:    }; 14: 15: #pragma managed 16: 17:    public __gc class CPPClass 18:    { 19:     public: 20:        String* GetMsg() 21:        { 22:           return( "Hello from managed C++" ); 23:        } 24: 25:        String* GetUnmanagedMsg() 26:        { 27:           CPPUnmanagedClass* pUMObj = new CPPUnmanagedClass; 28:           String* strMsg = pUMObj->GetMsg(); 29: 30:           return( strMsg ); 31:        } 32:    }; 33: } 

Finally, you need to include the windows.h file into the project so it can compile the unmanaged code. The include statement shown here is added to the stdafx.h file in the project:

 #include <windows.h> 

Defining the C# Component Class

The C# class is much more compact than the C++ class. Defining a similar method to return a message in the C# class is done by modifying the class1.cs file in the project you created earlier. First of all, as you did in the C++ project, change the class name to something more appropriate. Add the following class definition to the CSLibrary namespace, either manually or using the Add New Function dialog, and you're finished with the C# class library:

 public class CSharpClass {    public String GetMsg()    {       return( "Hello from C#" );    } } 

Defining the Visual Basic Windows Form

Using the Windows Form designer in Visual Basic makes it quick and easy to define a Windows Form, unlike doing it manually in Visual C++. The type of form you are creating for this application simply has a text area for the message to be displayed and buttons that will call the appropriate components when clicked.

Start by using the Form designer and dragging the appropriate controls to the form, as shown in Figure 6.3. Also use the property dialog for each of the controls and name the controls as shown. Name the buttons bntCS, btnCPP, btnUnmanagedCPP, and btnDone. Name the message text label stMessage.

Figure 6.3. A Windows Form in the Visual Basic .NET form designer.


Adding References to Components

Adding a reference to each of the component libraries is necessary in order to use the components within the Visual Basic application. Using the Solution Explorer, right-click each of the components and build them as shown in Figure 6.4.

Figure 6.4. Building components from the Solution Explorer.


Once you have built the components, use the right-click menu and select the references in the IntegratedApp project. Then select Add Reference from the menu to display the Add Reference dialog shown in Figure 6.5. Your components won't be shown in this dialog; therefore, use the Browse button to open the Select Component dialog, find the DLL files that were built for the components, and add them.

Figure 6.5. The Add Reference dialog.


Once you've added the references for both the C# and C++ components, you can use the classes in the Visual Basic project.

Using the C# and C++ Classes

When a user clicks the buttons on the Windows Form you designed earlier, you can use the classes in the appropriate components to get a message and display it in the stMessage control.

Adding the appropriate event handlers for each of the buttons is done by double-clicking each of the buttons on the form in the Windows Form editor. As you double-click these buttons, appropriate event handlers are added to handle each button's Click event. Add the code shown in Listing 6.4 to use the classes, display the messages, and close the application when the user clicks the Done button.

Listing 6.4 Event Handler Code in IntegratedApp Using Components Written in C# and C++
 1: Private Sub btnDone_Click(  2:         ByVal sender As System.Object, ByVal e As System.EventArgs) _  3:            Handles btnDone.Click  4:    Close()  5: End Sub  6:  7: Private Sub btnCPP_Click(  8:         ByVal sender As System.Object, ByVal e As System.EventArgs) _  9:            Handles btnCPP.Click 10:    Dim msgClass As CPPLibrary.CPPClass = New CPPLibrary.CPPClass() 11:    stMessage.Text = msgClass.GetMsg() 12: End Sub 13: 14: Private Sub btnUnmanagedCPP_Click ( 15:         ByVal sender As System.Object, ByVal e As System.EventArgs) _ 16:            Handles btnUnmanagedCPP.Click 17:    Dim msgClass As CPPLibrary.CPPClass = New CPPLibrary.CPPClass() 18:    stMessage.Text = msgClass.GetUnmanagedMsg() 19: End Sub 20: 21: Private Sub btnCS_Click( 22:         ByVal sender As System.Object, ByVal e As System.EventArgs) _ 23:            Handles btnCS.Click 24:    Dim msgClass As CSLibrary.CSharpClass = New CSLibrary.CSharpClass() 25:    stMessage.Text = msgClass.GetMsg() 26: End Sub 

Notice how even the unmanaged code is used in the same fashion as the managed code written in C++ and C#. This is because once the unmanaged code is wrapped by managed C++ code, VB only interfaces with the managed C++ code, and it doesn't matter how it is executed behind the scenes.

You can now build the application and run it by pressing the F5 key or selecting the Debug, Start menu item from the menu bar.

If you watch the output window within the debugger, you will notice that when the application starts, the components are not loaded. As you click each button on the dialog, the appropriate component is loaded when used for the first time. This feature speeds up the loading of applications if the code is broken into class libraries.


Sams Teach Yourself Visual C++. NET in 24 Hours
Sams Teach Yourself Visual C++.NET in 24 Hours
ISBN: 0672323230
EAN: 2147483647
Year: 2002
Pages: 237

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: