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 SolutionTo 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 LibraryThe 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
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 SolutionWorking 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:
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. |