Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 1: Creating ActiveX Clients By Noppadon Kamolvilassatian Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual Basic."— Presentation transcript:

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

2 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.

3 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

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

5 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.

6 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.

7 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.

8 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.

9 Using Interfaces

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

11 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.

12 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.

13 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.

14 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.

15 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

16 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.

17 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.

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

19 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.

20 MS Excel Object Model

21 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.

22 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

23 Binding Animation on how late binding works

24 Characteristics of Server Component Determining Where Server Components Run Notifying Clients

25 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

26 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.

27 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).

28 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.

29 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.

30 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.

31 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.

32 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.

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

34 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

35 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

36 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

37 The Microsoft Excel Object Model

38 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

39 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)

40 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

41 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 "

42 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

43 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

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

45 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

46 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

47 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

48 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.

49 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.”

50 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

51 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.


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

Similar presentations


Ads by Google