Using the Class Library from Managed Code

If a class library is being developed for a particular application, it's easy enough to have the application and the library in the same solution. A shared class library will be called by applications that are not in the same solution as the library. The difference between the two scenarios is less than you might think.

Using the Library from Code in the Same Solution

To test the managed class library, the simplest approach is to add another project to the solution, this one containing a test harness. Simply right-click the solution and choose Add, Add Project. Select a Console Application (.NET) and name it ManagedHarness . A source file is generated for the console application.

Before you can enter code that uses the library, you have to arrange for the compiler to know about the classes and methods inside it. Rather than including a header file as in unmanaged C++, you add a reference to the assembly. Right-click the ManagedHarness project in Solution Explorer and choose Add Reference. Because the assembly is in another project inside the same solution as the class library, you use the Projects tab on the Add Reference dialog box.

Once the reference is added, you can use the Arithmetic class like any other managed class. Because it's a garbage-collected class, you must create instances on the managed heap with new and work with them through pointers. Here's the test code in the console harness:

 
 // This is the main project file for VC++ application project // generated using an Application Wizard. #include "stdafx.h"  #using <mscorlib.dll> using namespace System; int _tmain() {    ManagedMathLibrary::Arithmetic* a = new ManagedMathLibrary::Arithmetic();    Console::Write("2.3 + 4.5 is ");    Console::WriteLine(a->Add(2.3, 4.5));    Console::ReadLine();    return 0; } 

Notice that there is no need to include ManagedArithmetic.h, or to add an additional linker dependency. Adding the reference takes care of everything. Also, because this version of Add returns a System::Double structure, you don't need to use __box around the answer before passing it to Console::WriteLine .

Creating a Release Version of the Library

The instructions so far in this chapter created a debug version (also called a debug build) of the library, and a debug version of the test harness to go with it. For distribution to other developers, it's better to create a release version. This has many advantages, including

  • Release builds are smaller, because they don't contain debugging symbols. Even in this tiny application, it makes a difference: The debug library is 7KB, whereas the release library is only 6KB.

  • Release builds are faster, because the optimizer is turned off in debug builds and some blocks of code are not included by the compiler in debug builds.

  • Release builds only use release builds of other libraries; and because license agreements often forbid redistributing debug versions of libraries, a release build might be the only build you can distribute anyway.

These differences are between the default debug build and the default release build. There are a few other differences too, but they are out of the scope of this chapter. What's more, you can change the definition of these buildseven enabling debugging symbols in a release build!but that too is out of scope.

To build a release version of MathLibrary.lib, just look on the toolbar next to the Start button (a blue right-pointing arrow) for a drop-down box with Debug selected. Click it and select Release. Build the application to create the Release version.

Using the Library from Code in a Different Solution

Working on the .NET runtime makes deploying libraries far simpler than it was with unmanaged code. To demonstrate this, consider a Windows application that uses the sample managed class library. To create a new solution with an application that uses the class library, follow these steps:

  1. In Visual Studio .NET 2003, create a new Windows Forms Application (.NET) called WinTest.

  2. Details on creating a managed Windows Forms application are in Chapter 4, "Building Simple User Interfaces with Windows Forms." If you need to understand the steps presented here, review that chapter as a reference.

  3. Drag two text box controls, two label controls, and a button from the toolbox onto the form.

  4. Use the Properties window to change the text property of the first label to +, the text of the second label to no text, the name of the second label to Answer, and the caption of the button to = (equals sign). Change the text property of the text boxes to no text.

  5. Rearrange the controls and resize the form so that it resembles Figure 6.1.

    Figure 6.1. Building a simple Windows Forms application that will use the managed library.

    graphics/06fig01.gif

  6. Double-click the = button to add a handler for the click event and open the source code for editing.

Before you can code the button handler, you need to add a reference to the managed library. To more accurately simulate a deployment, copy the release version (it's in the Release folder under the project) of ManagedMathLibrary.dll to another folder on your computer. In the instructions that follow, that folder is assumed to be C:\MathLibrary .

To add the reference, right-click the project in Solution Explorer and choose Add Reference. On the .NET tab, click the Browse button and browse to C:\MathLibrary , select ManagedMathLibrary.dll, and click Open. Click OK on the Add Reference dialog box to complete adding the reference.

At this point, the class inside the managed library is available to your code, using the full name of ManagedMathLibrary::Arithmetic . Here is the code for the button click handler:

 
 private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e) {    ManagedMathLibrary::Arithmetic* a = new ManagedMathLibrary::Arithmetic();    System::Double ans = a->Add(Double::Parse(textBox1->Text),                                Double::Parse(textBox2->Text));     Answer->Text = ans.ToString(); } 

Enter this code into the button click handler, and then build and run the Windows application. Enter numbers into the text boxes, and click the = button to see the sum of the two numbers .

This simple library accomplishes the same tasks as the unmanaged library shown in the previous chapter, and the two sample applications (a console application in the same solution and a Windows application in a different solution from the library) also accomplish the same tasks . Yet when you compare the effort to implement the managed solutions to the effort to implement the unmanaged solutions, it becomes very clear what the runtime has to offer. Deployment is much simpler in the managed case, and the Windows Forms application is developed far more rapidly than the MFC Dialog application. This simplicity is what motivates developers to move to managed code.



Microsoft Visual C++. NET 2003 Kick Start
Microsoft Visual C++ .NET 2003 Kick Start
ISBN: 0672326000
EAN: 2147483647
Year: 2002
Pages: 141
Authors: Kate Gregory

Similar book on Amazon

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