Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 1 9.2 Working with Unmanaged Code.

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Introduction to Visual Basic.NET Uploaded By: M.Sheraz anjum.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Developing a Simple Graphical User Interface (GUI)
Abstract Data Type Fraction Example
CA 121 Intro to Programming Tariq Aziz and Kevin Jones GUI Programming in Visual Studio.NET Chapter 1 Tariq Aziz and Kevin Jones.
Compunet Corporation Programming with Visual Studio.NET GUI Week 13 Tariq Aziz and Kevin Jones.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
1 Session-I & II CSIT-121 Spring 2006 Session Targets Introducing the VC++.NET Solving problems on computer Programming in C++ Writing and Running Programs.
1 Writing a Good Program 5. Objects and Classes in C++
Advanced Object-Oriented Programming Features
Compunet Corporation Programming with Visual Basic.NET GUI Week # 11 Tariq Ibn Aziz.
Using Visual C++ and Pelles C
1 ENG236: ENG236: C++ Programming Environment (2) Rocky K. C. Chang THE HONG KONG POLYTECHNIC UNIVERSITY.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
McGraw-Hill© 2007 The McGraw-Hill Companies, Inc. All rights reserved. 1-1.
Multi-function Scientific Calculator An Introduction to Graphical User Interfaces (GUI)
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
1 INF160 IS Development Environments AUBG, COS dept Lecture 06 Title: Dev Env: Code::Blocks (Extract from Syllabus) Reference:
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Computing IV Visual C Introduction with OpenCV Example Xinwen Fu.
CS Tutorial 1 Getting Started with Visual Studio 2012 (Visual Studio 2010 are no longer available on MSDNAA, please choose Visual Studio 2012 which.
Multiple Forms, Container Controls, AddHandler This presentation is based on the Forms and ContainerControls VB Projects 1.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN AK - IT Softwarekonstruktion.
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
Lecture 6: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept.
Computer Science I How to Configure Visual Studio.NET 2003 for C++ Colin Goble.
Active-HDL Interfaces Building VHPI Applications C Compilation Course 9.
Sample Application Multi Layered Architecture (n-tier): –Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
1 Writing a Good Program 8. Elementary Data Structure.
Using Microsoft Visual Studio 2005 Original by Suma Rao Revised by John G. McMahon ( 9/6/2008 )
Programming with Visual C++: Concepts and Projects Chapter 2B: Reading, Processing and Displaying Data (Tutorial)
Visual Basic.NET BASICS Lesson 1 A First Look at Microsoft Visual Basic.NET.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Information and Communication Technology Sayed Mahbub Hasan Amiri Dhaka Residential Model College Higher Secondary.
Microsoft Visual Basic 2005 BASICS Lesson 1 A First Look at Microsoft Visual Basic.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Creating a Multiple-Form Interface.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
Open project in Microsoft Visual Studio → build program in “Release” mode.
1 Introduction to Object Oriented Programming Chapter 10.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
Tutorial 1 Writing Your First C++ Program CSC1110C Introduction to Computer Programming By Paul Pun Acknowledgement: Special thanks to Dr. Michael Fung.
Visual Basic Fundamental Concepts
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Developing a Simple Graphical User Interface (GUI)
Lab 1 Introduction to C++.
VISUAL BASIC.
Visual Basic..
Hands-on Introduction to Visual Basic .NET
Advanced Programming Lecture 02: Introduction to C# Apps
CIS16 Application Development Programming with Visual Basic
1. Open Visual Studio 2008.
Lab 1 Introduction to C++.
Chapter 3 – Introduction to C# Programming
Double click Microsoft Visual Studio 2010 on the Computer Desktop
Programs written in C and C++ can run on many different computers
Computer Terms Review from what language did C++ originate?
Class rational part2.
GUI Programming in Visual Studio .NET
Presentation transcript:

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Working with Unmanaged Code

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 2 Working with Unmanaged Code Thanks to the IJW (It Just Works) technology, managed C++ application can directly work with the legacy C++/C codes It means that all C++ programs that we developed previously can be used directly with managed programs It is a unique feature of Visual C++, not the case for other components of Visual Studio, such as Visual Basic or Visual C# The simplest way for a managed C++ application to call legacy C++/C functions is by means of using static library. VB and C# cannot use unmanaged C++ libraries

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 3 { public: int DrawShape(); int PlaySound(); DogClass.h : class CAT }; : Main Program #include "DogClass.h" void main() { DOG Bobby; CAT Frisky; Bobby.DrawShape(); Frisky.DrawShape(); } MainProject.cpp : : Frisky.PlaySound(); : #include "CatClass.h" { public: int DrawShape(); int PlaySound(); CatClass.h : class CAT d 35 5f … … 22 6f … dd 23 : }; : d 35 5f … … 22 6f … dd 23 : : General Approach Library for DOG Library for CAT

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 4 { public: int DrawShape(); int PlaySound(); DogClass.h : class CAT }; : { public: int DrawShape(); int PlaySound(); CatClass.h : class CAT d 35 5f … … 22 6f … dd 23 : }; : d 35 5f … … 22 6f … dd 23 : : Managed C++ Application CAT c; DOG d; c.DrawShape(); d.DrawShape(); : : Library for DOG Library for CAT

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 5 Step 1: Create the class required class Arithmetic// declare the class { public: void SetNum (int in1, int in2); int GetNum1(); int GetNum2(); int Add();// return num1 + num2 private: int num1; int num2; }; Example: Adding two numbers Arithmetic.h

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 6 Step 2: Implement the class void Arithmetic::SetNum(int in1, int in2) {num1 = in1; num2 = in2; } int Arithmetic::GetNum1() {return num1; } int Arithmetic::GetNum2() {return num2; } int Arithmetic::Add() {return num1 + num2; } For each member function, develop the required codes

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 7 Step 3: Test the class class Arithmetic// declare the class { public: void SetNum (int in1, int in2); int GetNum1(); int GetNum2(); int Add();// return num1 + num2 private: int num1; int num2; }; Create the header file and a main() for testing the functionality of the class This main() is only for testing purpose, not supposed to be used in real application Create the header file and a main() for testing the functionality of the class This main() is only for testing purpose, not supposed to be used in real application Arithmetic.h

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 8 #include using namespace std; #include "Arithmetic.h" // Implement the functions and test them void Arithmetic::SetNum(int in1, int in2) {num1 = in1; num2 = in2; } int Arithmetic::GetNum1() {return num1; } int Arithmetic::GetNum2() {return num2; } int Arithmetic::Add() {return num1 + num2; } MainTest.cpp int main() { Arithmetic Op; Op.SetNum(3,4); int Num1 = Op.GetNum1(); int Num2 = Op.GetNum2(); cout << Num1 << " + " << Num2 << " = " << Op.Add() << endl; return 0; } Just a testing console application, not supposed to be used in real application

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 9 Step 4: Create the library with Visual C++ Assume your class is fully tested. You can create your library using Visual C++ Start your Visual C++.NET In Visual C++.NET, start a new Win32 Console Application as usual Set the location to store your library project to, e.g. E:\temp\ENG236\Ch9 Set the project name, e.g. LibArith However, on choosing the options in the Application Settings window, check Static library and uncheck the option Precompiled header.

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 10

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 11 1.In the Visual Studio environment, click Project  Properties 2.Go to Configuration Properties  General  Project Defaults  Common Language Runtime support 3. Choose Pure MSIL Common Language Runtime Support (/clr:pure ) 4. Click Apply and OK

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 12 In the Visual Studio environment, click Project  Add New Item... and choose Header File (.h) Set the Header File name to, e.g. Arithmetic Click OK and the header file Arithmetic.h will be added to the project LibArith Type the class declaration.

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 13 Type your class declaration here Remember to comment your codes Type your class declaration here Remember to comment your codes

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 14 In the Visual Studio environment, click Project  Add New Item... and choose C++ File (.cpp) Set the C++ source file name, e.g. ArithCodes Click OK and the C++ source file ArithCodes.cpp will be added to the project LibArith You may verify it by checking the Solution Explorer Type the codes for the implementation of the member functions Remember to include your header file and note its location Build the library by clicking Build Solution.

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 15 You need to enter the correct path of the header file It shows that the Arithmetic.h is in the same folder as that of ArithCodes.cpp You need to enter the correct path of the header file It shows that the Arithmetic.h is in the same folder as that of ArithCodes.cpp

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 16 Step 5: Locate the library and header files Locate the files Arithmetic.h and LibArith.lib Arithmetic.h shows the class definition From its contents, we know how to use the class LibArith.lib contains the codes for the implementation of the member functions Note that it is in object code form. No one can see or modify the source codes.

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 17 E:\Temp\ENG236 \Ch9\LibArith\ LibArith E:\Temp\ENG236\C h9\LibArith\debu g

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 18 Step 6: Integrating into the application Open a project of Windows Forms Application, e.g. ArithForm in the folder e.g. E:\temp\ENG236\ Ch9\ Design the form as shown:

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 19 Copy Arithmetic.h and LibArith.lib to the current folder E:\temp\ENG236\Ch9\ArithForm\ArithForm In the Visual Studio environment, click Project  Add Existing Item under ArithForm. Select All Files to show all the files in the folder Add Arithmetic.h and LibArith.lib to the project.

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 20 #include "Arithmetic.h" #include // For atoi() // Must be added at the beginning of Form1.h using namespace System::Runtime::InteropServices; private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { int num1, num2, res; // variables used by unmanaged code String ^ tbstr1 = textBox1->Text; String ^ tbstr2 = textBox2->Text; char *strNum1 = (char*)Marshal::StringToHGlobalAnsi(tbstr1).ToPointer(); char *strNum2 = (char*)Marshal::StringToHGlobalAnsi(tbstr2).ToPointer(); Double-click the Add button and add the following codes to button1_Click()

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 21 num1 = atoi(strNum1); num2 = atoi(strNum2); Arithmetic Op; // Class from the static libray Op.SetNum(num1,num2); // Using the unmanaged codes res = Op.Add(); // Using the unmanaged codes MessageBox::Show(""+res,"Result",MessageBoxButtons::OK); Marshal::FreeHGlobal((IntPtr)strNum1); Marshal::FreeHGlobal((IntPtr)strNum2); }

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 22 See the added Arithmetic.h and LibArith.lib here After building the application, the result is shown

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 23 You may double-click LibArith.lib. You can only see some hex codes. No source codes can be found

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 24 To see how to use the class Arithmetic, double-click Arithmetic.h

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 25 Step 7: Execute the application The addition is done by the library function

Computer Programming and Basic Software Engineering 9 Building Graphical User Interface 26 Exercise 9.2 Modify your static library such that it will provide the other 3 arithmetic functions: subtraction, multiplication and division; all of them in integer arithmetic. For division, detect if the divisor is 0. Your GUI should include 3 more buttons namely “ Subtract ”, “ Multiply ”, and “ Divide ” such that when any one is clicked, the corresponding function of the static library will be called and the result will be shown in a message box.