Chapter 1: Creating ActiveX Clients By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual Basic.

Slides:



Advertisements
Similar presentations
COM vs. CORBA.
Advertisements

© Paradigm Publishing, Inc Excel 2013 Level 2 Unit 2Managing and Integrating Data and the Excel Environment Chapter 7Automating Repetitive Tasks.
Programming with Microsoft Visual Basic th Edition
Tutorial 12: Enhancing Excel with Visual Basic for Applications
The Web Warrior Guide to Web Design Technologies
1 Chapter 12 Working With Access 2000 on the Internet.
VBA Modules, Functions, Variables, and Constants
Using Visual Basic 6.0 to Create Web-Based Database Applications
1 Chapter 8 Object-Based Programming in VBA. 8 Chapter Objectives Declare and use object variables Create procedures that use built-in form methods Find.
Systems Architecture, Fourth Edition1 Internet and Distributed Application Services Chapter 13.
ASP.NET Programming with C# and SQL Server First Edition
Computer Science 101 Web Access to Databases Overview of Web Access to Databases.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
A Free sample background from © 2001 By Default!Slide 1.NET Overview BY: Pinkesh Desai.
Intro to COM What is it and what can it do for me?
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
A Scalable Application Architecture for composing News Portals on the Internet Serpil TOK, Zeki BAYRAM. Eastern MediterraneanUniversity Famagusta Famagusta.
1 Modular Software/ Component Software 2 Modular Software Code developed in modules. Modules can then be linked together to produce finished product/program.
Visual Basic: An Object Oriented Approach 12 – Creating and using ActiveX objects.
Beyond DHTML So far we have seen and used: CGI programs (using Perl ) and SSI on server side Java Script, VB Script, CSS and DOM on client side. For some.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
An Object-Oriented Approach to Programming Logic and Design
Obsydian OLE Automation Ranjit Sahota Chief Architect Obsydian Development Ranjit Sahota Chief Architect Obsydian Development.
Developing Workflows with SharePoint Designer David Coe Application Development Consultant Microsoft Corporation.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
COM vs. CORBA Computer Science at Azusa Pacific University September 19, 2015 Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department.
Using Visual Basic 6.0 to Create Web-Based Database Applications
Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.
Chapter 8: Writing Graphical User Interfaces
Virtual techdays INDIA │ Nov 2010 Developing Office Biz Application using WPF on Windows 7 Sarang Datye │ Sr. Consultant, Microsoft Sridhar Poduri.
Threading Models in Visual Basic Language Student Name: Danyu Xu Student ID:98044.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7.
COM/DCOM Implementation Basics of: Object creation and access Object Reuse Interface referencing.
OLE / COM Helia / Martti Laiho Sources: MSDN Kraig Brockschmidt: Inside OLE Orfali-Harkey-Erwards: Client/Server Survival Guide.
Introduction to COM and DCOM Organizational Communications and Technologies Prithvi N. Rao H. John Heinz III School of Public Policy and Management Carnegie.
COMCOM omponent bject odel After this presentation, you will: Be Able To: Conceptual understanding of COM How it works, What it’s used for Decode Acronyms.
ICONICS ActiveX ToolWorX V 6.1.
CHAPTER TEN AUTHORING.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Ni.com Understanding COM/ActiveX Jeff Paulter Staff Software Engineer Thurs Aug 17 10:15-11:30 a.m., 1:45-3:00 p.m. Ash (10A) Jeff Paulter Staff Software.
SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright © 1999.
Chapter 3: Creating ActiveX Controls By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.
Tutorial 91 Databases A database is an organized collection of related information stored in a file on a disk A database allows companies to store information.
1.
ME 142 Engineering Computation I Using Subroutines Effectively.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Seminarium on Component-based Software Engineering Feraaz Imami LIACS – Leiden University Fall 2005 Component Object Model (COM)
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Introduction to OOP CPS235: Introduction.
Advanced Object-oriented Design Patterns Creational Design Patterns.
7-1 Active Server and ADO Colorado Technical University IT420 Tim Peterson.
Copyright © 2004, Keith D Swenson, All Rights Reserved. OASIS Asynchronous Service Access Protocol (ASAP) Tutorial Overview, OASIS ASAP TC May 4, 2004.
JavaScript 101 Lesson 6: Introduction to Functions.
High degree of user interaction Interactive Systems: Model View Controller Presentation-abstraction-control.
Wednesday NI Vision Sessions
The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel.
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
Distribution and components
Out-of-Process Components
Deploying and Configuring SSIS Packages
Chapter 6 Programming with Office 97
Microsoft Access 2003 Illustrated Complete
VISUAL BASIC.
CIS16 Application Development Programming with Visual Basic
CS285 Introduction - Visual Basic
Out-of-Process Components
Presentation transcript:

Chapter 1: Creating ActiveX Clients By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual Basic 5, Microsoft Corporation

Objectives Animation Explain the major constructs of COM. Explain the major constructs of Automation. Create a client application that can manipulate other applications through Automation, use multiple interfaces on an object, and receive notifications from a server.

Outline The Component Object Model (COM) Implementing Automation Characteristics of Server Components Creating a Client in Visual Basic Creating a Client that Uses Microsoft Excel

The Component Object Model (COM) Overview of COM Advantages of Using COM The COM Specification Using Interfaces The IUnknown Interface Client and Server Communication

Overview of COM Component: A unit of executable code that provides functionality. COM specifies how components are created and how client applications connect to components. Object: A combination of code and data that can be treated as a unit. Automation: Part of the COM specification that defines a standard method for creating components and using objects.

Advantages of Using COM Binary Compatibility and Cross-Platform Development COM is independent of the development language and operating system. Code Reusability Component technology enables reuse of objects in a variety of applications without knowledge about the implementations of objects. Version Control New functionality can be added to a component (by adding or changing the implementation of an interface) without affecting clients that already use the component.

The COM Specification The COM specification defines: –How an object is created from a component. –How a client accesses features of the object. –The object's responsibility for destroying itself when it is no longer in use. All of these activities are handled by interfaces, which make using a server much easier than before.

Using Interfaces Interfaces are groups of functions that provide connection points. Interfaces provide standardized access to the methods and properties (functionality). They are a contract that ensures consistent access to functionality. They structure that access so that servers are easier to use.

Using Interfaces

A credit card object with two interfaces, IRetail and IManager, that provide functionality to a retail store client and a credit manager client.

The IUnknown Interface The COM specification requires the following functionality of all objects. –The object must be able to keep track of the number of connections made to it. When no longer in use, it must be able to destroy itself. –The object must be capable of being queried by a client for additional interfaces that it may support. In order to provide this functionality, all objects have a built-in interface named IUnknown.

Functions of the IUnknown Interface The IUnknown interface has three functions. The functions AddRef and Release keep track of the creation and destruction of the object QueryInterface lets clients query for other interfaces provided by the object.

Client and Server Communication COM supports three types of objects: in-process (DLL), out-of-process (EXE in separate process), and remote (DLL or EXE on a different machine via Distributed COM or DCOM) You use the exact same code to hook up to an in- process, out-of-process, or remote object.

Client and Server Communication A client and an in-process server share the same address space. A client and an out-of-process server has to communicate across the process boundary. The communication between a client and a remote server is even more complex.

COM and the Registry For a component to become available for clients, it must register itself with the operating system. It updates a section in the system registry. At run time, the client (in conjunction with COM) uses the information in the registry to call the server component. Animation

Implementing Automation Automation is a technology for manipulating objects that are defined by an application or library from outside the application. Automation is the part of COM that enables objects to be created and programmed by clients in a standard way. Automation works through a standard interface known as IDispatch. This interface exports any number of properties and methods supported by an object.

Type Libraries Clients get information about Automation objects from a server by inspecting the server component's type library. Also known as an object library. –Interfaces supported by the object. –The properties, methods, and events provided by the object. –The return types and parameters types of the object's methods and events. –The Dispatch IDs these methods and properties use. –The name of the Help file and Help topics.

The Object Browser An Object Browser is a tool that lets you view the methods and properties of an Automation object. Demonstration

Object Models When a server is designed, the objects that it offers can be structured into an object model. An object model organizes the objects in the server into a hierarchy that represents which objects contain other objects. This structure makes the server easier to use.

MS Excel Object Model

The IDispatch Interface The dispatch interface, IDispatch, is a standard Automation interface designed for exposing component methods and properties to a client. This interface contains four functions. The functions GetTypeInfoCount and GetTypeInfo obtain information about the interfaces, methods, and properties that a component supports.

The IDispatch Interface GetIDsOfNames takes one or more properties and/or methods, and returns their dispatch ID (dispID) values as defined in the type library. Invoke takes a dispID and a pointer to an array of parameters, and causes the object to execute the associated property or method

Binding Animation on how late binding works

Characteristics of Server Component Determining Where Server Components Run Notifying Clients

Determining Where Server Components Run In-process –Implemented as a dynamic-link library (DLL). –Run in the same process space as its client application. –Most efficient communication

Determining Where Server Components Run Out-of-process –Implemented as an executable file. –Run in its own process space. –Communication across process boundaries--slower than an in-process server. –A single instance of an out-of-process component can service many clients, share global dta, and insulate other client applications from a client’s problem.

Determining Where Server Components Run Remote –Out-of-process components, located on a separate machine. –Communication overhead is very high--slowest. –Allow processing on a more powerful computer. –The component can also be located closer to the work it is doing (such as located closer to a remote database).

Notifying Clients If you wanted the component to perform a task that you knew will need several minutes to complete, you would notify the client asynchronously when the server completed some task. Use events or callbacks to notify.

Creating a Client in Visual Basic 1. Set a reference to the type library of a component. 2. Declare an object variable. 3. Create an object. 4. Use the object’s methods and properties.

Setting References 1.On the Project menu, click References. 2.In the References dialog box, select the type library you want to connect to, and then click OK.

Declaring Object Variables Generic Object Variables Dim x As Object Specific Object Variables Dim ex as Excel.Application Specific variable declarations give better performance, and enable Visual Basic to display data about the available methods and properties.

Creating Objects Using the CreateObject Function Dim objMyObject as Project1.Class1 Set objMyObject = CreateObject("Project1.Class1") Using the New Keyword with a Set statement Dim objMyObject as Project1.Class1 Set objMyObject = New Project1.Class1 The second method is more efficient in some cases.

Using Automation Objects To invoke the methods and properties of the object you have created, use the dot (.) operator. object.[method]

Receiving Notification from Server: Events When building a client, you need the know which events are generated by the component, and implement each event with an event handler

Receiving Notification from Server: Events First, declaring the event interface Public WithEvents obj as Server.Object Second, implement the event handlers Private Sub obj_Event1() ' Take some action based on Event1. End Sub Private Sub obj_Event2(vNewValue As Variant) ' Take some action based on Event2. End Sub

Creating a Client that Uses Microsoft Excel How to: Use workbooks and charts. Use Automation to create an instance of Microsoft Excel, and then manipulate some of its object’s methods and properties. Get and set property values to create and manipulate a chart object. Demonstration

The Microsoft Excel Object Model

Use the dot (.) operator to reference objects xl.Workbooks("BOOK1.XLS").Worksheets("SHEET1").Range("A1").Value = 5 Use the Parent property to reference an object that is one level higher. xlwb.Parent 'refers to the Application object

The Microsoft Excel Object Model Create object variables to reference to objects that you use repeatedly. Dim xl as Excel.Application Dim xlSheet as Excel.Worksheet Set xl = CreateObject("Excel.Application") Set xlSheet = xl.Workbooks("BOOK1.XLS"). Worksheets("SHEET1") xlSheet.Range("A1").Value = 5 Set a reference to an object by name of by index. set xlwb = xl.Workbooks("MYBOOK.XLS") set xlwb = xl.Workbooks(1)

Creating an Instance of Microsoft Excel Set a reference to the MS Excel type library: Project-->References-->Microsoft Excel 5.0 Object Library. Using the New Keyword Dim xl as Excel.Application Dim xlchart as Excel.Chart Dim xlsheet as Excedl.Worksheet set xl = New Excel.Application set xlchart = New Excel.Chart set xlsheet = New Excel.Worksheet Close Microsoft Excel xl.Quit

Using Methods in MS Excel Add a new workbook to the Workbooks collection xl.Workbooks.Add () Open a specific workbook xl.Workbooks.Open "c:\book1.xls "

Using Methods in MS Excel Error handling when opening a workbook Dim xl as Excel.Application Dim xlwb as Excel.WorkBook On Error Resume Next set xl = CreateObject("Excel.Application") set xlwb = xl.Workbooks.Open ("c:\book1.xls") If Err <> 0 Then Msgbox "Unable to open workbook." Unload Me End If

Using Methods in MS Excel Close the active workbook xl.ActiveWorkbook.Close SaveChanges := False Close the workbook Book1.xls xl.Workbooks("BOOK1.XLS").Close SaveChanges :=False

Getting and Setting Values set xlsheet = _ xlapp.Workbooks("Book1").Worksheets("Sheet1") xlsheet.Range("A1").Value = 5 xlsheet.Range("myrange").Value = 10

Getting and Setting Values set xl = CreateObject("Excel.Application") ' Explicitly state the exact hierarchy. xl.Workbooks("x.xls").Worksheets("s").Range("A1").Value = 5 ' Assume the current active workbook and worksheet. xl.Range("A2").Value = 5 ' Establish object variable to reference objects. Set wb = xl.Workbooks("source.xls") Set ws = wb.Worksheets("sheet1") Set rg = ws.Range("A1") rg.Value = 5

Using the Charts Collection Select a series of cells on a worksheet and create a chart xlsheet.Range("A1:D4").Select set xlc = xl.Charts.Add() xlc.Type = xl3DColumn Set a reference to an existing chart, then change the type of chart set xlc = xl.workbooks("bk1.xls").Charts("mycht") xlc.Type = xl3DColumn

Using the Charts Collection Sub cmdCreateChart_Click() Dim xl as Excel.Application Dim xlc as Excel.Chart Set xl = CreateObject("Excel.Application") xl.Visible = True xl.Workbooks.Add xl.Range("A1").Value = 3 xl.Range("A2").Value = 2 xl.Range("A1:A2").Select Set xlc = xl.Charts.Add() xlc.Type = xl3DColumn End Sub

Lab (Demonstration) Exercise 1: Controlling Microsoft Excel In this exercise, you will control Microsoft Excel using Automation. Exercise 2: Creating a Client Application In this exercise, you will create a client application that uses the functionality of a version of the Credit Card component, which is created in later chapters.

Exercise 1: Controlling Microsoft Excel The cells C3 and C4 have been named “Growth” and “Inflation”, respectively. Also, Cell C16:E16 have been named “Net_Profit.”

Exercise 2: Creating a Client Application The Credit Card component exposes two interfaces. 1. The default interface validates the purchase of an item with these properties and methods. NameType CardNumberProperty ExpireDateProperty PurchaseAmountProperty ApproveMethod 2. The IManage interface is used to change the credit limit of a client. NameType CreditLimitProperty

Lab Report Handed in by 17 August, 3:30 pm. A 3-page summary of “ActiveX Demystified”. Answers to self-check questions with explanations of your answers. Complete code of lab exercise 1 and 2 with explanations of statements in the code.