Home

Extended Static Libraries Using .NET

 

Introduction

A static library is a library that you can create independent of an application and use it in your program. It can be used to extend the library of functions of a library you use in your programming language. For example, Visual C++ (whether MFC or .Net) is significantly limited as compared to Visual Basic in the areas of mathematics, finance, data validation functions. Creating your own library can help reduce your work when facing such assignments.

Creating a static is extremely easy. The problem you would have is deciding what to put in the library.

Creating the Project

  1. Start Microsoft Visual C++. On the main menu, click File -> new Project...
  2. In the New Project dialog box, select Visual C++ Projects in the Project Types list
  3. In the Templates list, click Empty Project (.Net)
  4. In the Project Name, type a name such as ExtLib and specify your desired folder in the Location box
  5. Click OK
  6. To add a new form, on the main menu, click Project -> Add New Item...
  7. In the Templates list of the Add New Item dialog box, click Windows Form (.Net)
  8. In the Name edit box, type inputbox and press Enter
  9. Design the dialog box as follows:
     
    Control Name Text Additional Properties
    Form inputbox Input Box FormBorderStyle: FixedDialog
    MaximizeBox: False
    MinimizeBox: False
    Label lblMessage Message  
    Text Box txtResult    
  10. Save All

Coding the Library

  1. Right-click the form and click View Code
  2. Include a public Get method that can return the contents of the text box of the form and, at the end of the header file, declare a function as follows:
     
    #pragma once
    
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    
    
    namespace ExtLib
    {
    	/// <summary> 
    	/// Summary for inputbox
    	///
    	/// WARNING: If you change the name of this class, you will need to change the 
    	///          'Resource File Name' property for the managed resource compiler tool 
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public __gc class frmInputBox : public System::Windows::Forms::Form
    	{
    	public: 
    		frmInputBox(void)
    		{
    			InitializeComponent();
    		}
            
    	protected: 
    		void Dispose(Boolean disposing)
    		{
    			if (disposing && components)
    			{
    				components->Dispose();
    			}
    			__super::Dispose(disposing);
    		}
    	private: System::Windows::Forms::Label *  lblMessage;
    	private: System::Windows::Forms::TextBox *  txtResult;
    	private: System::Windows::Forms::Button *  btnCancel;
    	private: System::Windows::Forms::Button *  btnOK;
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container* components;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			this->lblMessage = new System::Windows::Forms::Label();
    			this->txtResult = new System::Windows::Forms::TextBox();
    			this->btnCancel = new System::Windows::Forms::Button();
    			this->btnOK = new System::Windows::Forms::Button();
    			this->SuspendLayout();
    			// 
    			// lblMessage
    			// 
    			this->lblMessage->Location = System::Drawing::Point(16, 16);
    			this->lblMessage->Name = S"lblMessage";
    			this->lblMessage->Size = System::Drawing::Size(288, 23);
    			this->lblMessage->TabIndex = 0;
    			this->lblMessage->Text = S"Message";
    			// 
    			// txtResult
    			// 
    			this->txtResult->Location = System::Drawing::Point(16, 56);
    			this->txtResult->Name = S"txtResult";
    			this->txtResult->Size = System::Drawing::Size(288, 20);
    			this->txtResult->TabIndex = 1;
    			this->txtResult->Text = S"";
    			// 
    			// btnCancel
    			// 
    	this->btnCancel->DialogResult = System::Windows::Forms::DialogResult::Cancel;
    			this->btnCancel->Location = System::Drawing::Point(232, 96);
    			this->btnCancel->Name = S"btnCancel";
    			this->btnCancel->TabIndex = 2;
    			this->btnCancel->Text = S"Cancel";
    			// 
    			// btnOK
    			// 
    	this->btnOK->DialogResult = System::Windows::Forms::DialogResult::OK;
    			this->btnOK->Location = System::Drawing::Point(144, 96);
    			this->btnOK->Name = S"btnOK";
    			this->btnOK->TabIndex = 3;
    			this->btnOK->Text = S"OK";
    			// 
    			// inputbox
    			// 
    			this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
    			this->ClientSize = System::Drawing::Size(322, 136);
    			this->Controls->Add(this->btnOK);
    			this->Controls->Add(this->btnCancel);
    			this->Controls->Add(this->txtResult);
    			this->Controls->Add(this->lblMessage);
    	this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedDialog;
    			this->MaximizeBox = false;
    			this->MinimizeBox = false;
    			this->Name = S"inputbox";
    			this->Text = S"Input Box";
    			this->ResumeLayout(false);
    
    		}		
    	public:
    
    		String* GetResult(void)
    		{
    			return this->txtResult->Text;
    		}
    	};
    
    	String *InputBox(String *Msg, String *Example=0, String *Caption=0);
    }
  3. Access the source file and change it as follows (if your namespace has a different name, make sure you change it accordingly):
     
    #include "inputbox.h"
    
    namespace ExtLib
    {
    	String *InputBox(String *Msg, String *Example, String *Caption)
    	{
    		frmInputBox *FIB = new frmInputBox;
    
    		if( FIB->ShowDialog() == DialogResult::OK )
    			return FIB->GetResult();
    		return 0;
    	}
    }
  4. To save the project, on the main menu, click File -> Save All.
  5. To indicate that you are creating a library instead of an executable, on the main menu, click Project -> ExtLib Properties...
  6. In the Property Pages, click the arrow of the Configuration Type and click Static Library (.lib)
     
  7. Click OK
  8. To build the library, on the main menu, click Build -> Build ExtLib. At the end, you should receive Build: 1 succeeded, 0 failed, 0 skipped

Using the Static Library

  1. To create a new project, on the main menu of MSVC, click File -> New Project...
  2. From the New Projects dialog box, in the Project Types list, click Visual C++ Projects
  3. In the Templates list, click Windows Forms Application (.Net)
  4. In the Name text box, type TestStaticLib
  5. Specify the Location and click OK
  6. Open Windows explorer or My Computer. Locate the folder where the ExtLib project was created. From the Debug folder, copy the ExtLib.lib file
  7. Paste it in the TestStaticLib folder of the current project 
  8. In the same way, copy the input.h file from the ExtLib folder and paste it in the TestStaticLib folder (if you are using MSVC .Net 2003, you should paste both files in the TestStaticLib folder inside of the primary TestStaticLib folder)
  9. Return to MSVC. To add the library to your project, on the main menu, click Project -> Add Existing Item...
  10. Change the Files Of Types to All Files
  11. In the list of files, click the ExtLib.lib file and click Open
  12. Add a Button control and a Text Box to the dialog box
  13. Change the Caption of the button to Input Box
  14. Change the Name of the text box to txtResult
  15. On the form, double-click the button to create its OnClick event
  16. Change the file as follows as follows:
     
    #pragma once
    
    #include "inputbox.h"
    
    namespace TestStaticLib
    {
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	/// <summary> 
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the 
    	///          'Resource File Name' property for the managed resource compiler tool 
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public __gc class Form1 : public System::Windows::Forms::Form
    	{	
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    		}
      
    	protected:
    		void Dispose(Boolean disposing)
    		{
    			if (disposing && components)
    			{
    				components->Dispose();
    			}
    			__super::Dispose(disposing);
    		}
    	private: System::Windows::Forms::Button *  button1;
    	private: System::Windows::Forms::TextBox *  txtResult;
    
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container * components;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			this->button1 = new System::Windows::Forms::Button();
    			this->txtResult = new System::Windows::Forms::TextBox();
    			this->SuspendLayout();
    			// 
    			// button1
    			// 
    			this->button1->Location = System::Drawing::Point(16, 16);
    			this->button1->Name = S"button1";
    			this->button1->TabIndex = 0;
    			this->button1->Text = S"Input Box";
    			this->button1->Click += new System::EventHandler(this, button1_Click);
    			// 
    			// txtResult
    			// 
    			this->txtResult->Location = System::Drawing::Point(104, 16);
    			this->txtResult->Name = S"txtResult";
    			this->txtResult->Size = System::Drawing::Size(176, 20);
    			this->txtResult->TabIndex = 1;
    			this->txtResult->Text = S"";
    			// 
    			// Form1
    			// 
    			this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
    			this->ClientSize = System::Drawing::Size(292, 266);
    			this->Controls->Add(this->txtResult);
    			this->Controls->Add(this->button1);
    			this->Name = S"Form1";
    			this->Text = S"Input Box Test";
    			this->ResumeLayout(false);
    
    		}	
    	private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
    			 {
    			    String *InputResult;
    
    			     InputResult = ExtLib::InputBox("Enter Employee's Full Name:");
    			     txtResult->Text = InputResult;
    			 }
    
    	};
    }
  17. To test your program, press Ctrl + F5. VOILA.
 

Home Copyright © 2002 FunctionX FunctionX