Visual Basic An Object Oriented Approach 12 Creating
Visual Basic: An Object Oriented Approach 12 – Creating and using Active. X objects
Component Object Technologies n Components are packages of software n n n Composed of one or more classes Can be used to provide service to programs Can be used flexibly n n a VB component can be used by a C++ program, a web page or an excel spreadsheet Current development trends emphasize the development and use of components n n n More flexible systems Reusability Easier team development
Active. X n Active. X is Microsoft’s take on components n A marketing term, covering several technologies n n Object Linking and Embedding (OLE) OLE Automation Component Object Model (COM) Active. X makes it possible to create components that can interact regardless of location, language or type n n COM is the standard that Active. X components conform to DCOM (Distributed COM) operates across local and wide area networks
OLE Automation n Use statements in a program to control objects within components n n Components are library files (DLLs) or separate executable files (EXEs) Controlling program can be written in almost any language n n VB, C++, VBScript, Java, Perl, Python… Execute efficient binaries from inefficient (but easy to use) scripting languages
Use of OLE Automation n Typical use is to build a complex Business application from prebuilt components n n e. g an Access database, an Excel calculating engine and a Word reporting engine Quick, simple and easy to maitain
Typical OLE Automation Code Sub Create. Customer. Document(Cust. Details As String) Dim WOLE As Word. Application Set WOLE = Create. Object(“Word. Application”) WOLE. Documents. Add “ATemplate”, 0 WOLE. Selection. Goto wd. Goto. Bookmark, wd. Goto. Absolute, _ 1, “Customer” WOLE. Selection. Find. Text = “”. Replacement. Text = “” End With Selection. Insert. After Cust. Details Application. Printout Set WOLE = Nothing End Sub Code uses Word to create a document (e. g. a Customer’s invoice) and print it out.
Where to get information on OLE Auto servers (Active. X servers) n n n Documentation – e. g. manuals supplied with libraries VB on-line help – for servers provided by Microsoft (e. g. Office applications) Primary source – The Object Browser n n Built in to VB Provides accurate interface information on any components on the system
VB’s Object Browser
VB’s Object Browser showing a user-defined class
Creating an Active. X server n Almost trivial in VB n n VB creates COM components without need for extra code (unlike VC++) Only changes required are type of project selected, and a few (very few) property settings n n Instancing – component’s strategy for creating objects from its classes Active. X type n n In-process (Dynamic Link Library – DLL) Out-of-process (Executable – EXE)
Instancing n States how objects can be created for use by the client application n n n Private: only creatable by another class in the same component (Server) Public. Not. Creatable: component creates objects for client application Single. Use: only one object of class allowed – client creates it Global Single. Use: as above, but no need to create object reference Multi. Use: possible to create many objects from the component – many clients can create them Global. Mulit. Use: can be used by many clients
Use of Instancing Property n Allows memory and processor allocations to be optimised depending on the type of component n n n e. g. no point in a Multi. Use Printing component if there is only one printer that can be used by one client at a time e. g. Many applications could make use of several Invoice objects simultaneously – Multi. Use obviously Careful design consideration at the stage of deciding on instancing setting can greatly improve application efficiency
Process allocation n Server can be in-process or out-of-process n In-process uses the client application’s memory allocation n n Client finds it inherently easier to work with because it is inside its own memory space Faster Limited to one client per instance of the component Out-of-process runs in its own memory allocation n n Clients need to communicate with it using operating system resources Slower and more resource hungry Several clients can make use of the same server Server can execute in its own right (without a cllient) n E. g. Word, Excel, Access etc.
Building an Active. X project n Before use, server component must be compiled n n It will be executed by another program out-with Visual Basic This can complicate debugging, since client is required to make component work n n n Can crate VB Project groups (for in-process components) Can run multiple instances of VB (for out-ofprocess components) Can use the Immediate Window (to do limited test and debug)
Deploying an Active. X Component n Must be registered on the system it will be used on n n VB does this automatically on the system it was developed on To move the component to another system, must register it explicitly n n n Use Reg. Svr 32. exe, or… Build a setup program using the Packaging and Deployment Wizard – registers component automatically Old components can clutter up a system n Use Reg. Server 32 /unregserver to remove registration
Version Compatibility n Once a component is registered n n n Windows maintains its registry entry Can not replace it with an incompatible version VB allows three forms of compatibility setting n n None – Windows does not check for compatibility Project – for use while a project is under development n n Changes to component interfaces will result in registration information being updated accordingly to allow compatibility between components and clients Binary – for use in deployed components n changes made to the interface of one or more classes will result in VB updating the registry information to ensure that incompatible client programs do not try to interoperate with the component
Creating a Server n n Start an Active. X EXE or DLL Project Implement server classes as required n n n Set appropriate instancing for each class Create suitable public methods and properties for communication with client Compile project to target form (EXE or DLL) For an Active. X EXE, run it once on the target computer For an Active. X DLL, use Package and Deployment Wizard, or execute Reg. Svr 32. exe… Reg. Svr 32 c: My. ComponentsNew. Component. dll
Using a Server in a VB Program n n Register the server with the program using Project/References menu and checking on the server component Create an instance of the component and use it’s methods
- Slides: 18