[ Pobierz całość w formacie PDF ]
.If you select an existing interface, its attributes arealready specified in its type library.If your object s interface is not marked asOleAutomation, you must either create an in-process server or write your ownmarshalling code.You can optionally add a description of your COM object.This description appearsin the type library for your object.Using the Automation object wizardThe Automation object wizard performs the following tasks:" Creates a new unit." Defines a new class that descends from the ATL classes CComObjectRootEx andCComCoClass.For more information on the base classes, see  Code generated bywizards on page 32-20." Adds a type library to your project and adds your object and its interface to thetype library.Before you create an Automation object, create or open the project for an applicationcontaining functionality that you want to expose.The project can be either anapplication or ActiveX library, depending on your needs.To display the Automation wizard:1 Choose File|New.2 Select the tab labeled, ActiveX.3 Double-click the Automation Object icon.In the wizard dialog, specify the following:" CoClass name: This is the name of the object as it appears to clients.Your object sdefault interface is created with a name based on this CoClass name with an  Iprepended, and the class created to implement your object has this name with a  Tprepended." Threading Model: Typically, client requests to your object enter on differentthreads of execution.You can specify how COM serializes these threads when itcalls your object.Your choice of threading model determines how the object isregistered.You are responsible for providing any threading support implied bythe model you choose.For information on the different possibilities, see Choosing a threading model on page 35-5.For information on how to providethread support to your application, see Chapter 7,  Writing multi-threadedapplications." Event support: You must indicate whether you want your object to generateevents to which clients can respond.The wizard can provide support for theinterfaces required to generate events and the dispatching of calls to client event35-4 Dev el oper  s Gui de Us i n g t h e Au t o ma t i o n o b j e c t wi z a r dhandlers.For information on how events work and what you need to do whenimplementing them, see  Exposing events to clients on page 35-10.You can optionally add a description of your COM object.This description appearsin the type library for your object.The Automation object implements a dual interface, which supports both early(compile-time) binding through the VTable and late (runtime) binding through theIDispatch interface.For more information, see  Dual interfaces on page 35-12.Choosing a threading modelWhen creating an object using a wizard, you select a threading model that yourobject agrees to support.By adding thread support to your COM object, you canimprove its performance, because multiple clients can access your application at thesame time.Table 35.1 lists the different threading models you can specify.Table 35.1 Threading models for COM objectsThreading model Description Implementation pros and consSingle The server provides no thread Clients are handled one at a time sosupport.COM serializes client no threading support is needed.requests so that the applicationNo performance benefit.receives one request at a time.Apartment (or Single- COM ensures that only one client Objects can safely access their ownthreaded apartment) thread can call the object at a time.instance data, but global data mustAll client calls use the thread in be protected using critical sectionswhich the object was created.or some other form of serialization.The thread s local variables arereliable across multiple calls.Some performance benefits.Free (also called multi- Objects can receive calls on any Objects must protect all instancethreaded apartment) number of threads at any time.and global data using criticalsections or some other form ofserialization.Thread local variables are notreliable across multiple calls.Cr eat i ng si mpl e COM s er v er s 35-5 Us i n g t h e Au t o ma t i o n o b j e c t wi z a r dTable 35.1 Threading models for COM objects (continued)Threading model Description Implementation pros and consBoth This is the same as the Free- Maximum performance andthreaded model except that flexibility.outgoing calls (for example,Does not require the application tocallbacks) are guaranteed toprovide thread support forexecute in the same thread.parameters supplied to outgoingcallsNeutral Multiple clients can call the object You must guard against threadon different threads at the same conflicts involving global data andtime, but COM ensures that no any instance data that is accessedtwo calls conflict.by multiple methods.This model should not be usedwith objects that have a userinterface (visual controls).This model is only available underCOM+.Under COM, it is mappedto the Apartment modelNote Local variables (except those in callbacks) are always safe, regardless of the threadingmodel.This is because local variables are stored on the stack and each thread has itsown stack.Local variables may not be safe in callbacks when using free-threading.The threading model you choose in the wizard determines how the object isregistered in the system Registry.You must make sure that your objectimplementation adheres to the threading model you have chosen.For generalinformation on writing thread-safe code, see Chapter 7,  Writing multi-threadedapplications.Note In order for COM to call your object according to the specified threading model, itmust be initialized to support that kind of model.You can specify how COM isinitialized using the ATL page of the project options dialog.For in-process servers, setting the threading model in the wizard sets the threadingmodel key in the CLSID registry entry.Out-of-process servers are registered as EXE, and COM must be initialized for thehighest threading model required.For example, if an EXE includes a free-threadedobject, it is initialized for free threading, which means that it can provide theexpected support for any free-threaded or apartment-threaded objects contained inthe EXE.To specify how COM is initialized, use the ATL page of the project optionsdialog.Writing an object that supports the free threading modelUse the free threading (or both) model rather than apartment threading wheneverthe object needs to be accessed from more than one thread.A common example is aclient application connected to an object on a remote machine.When the remoteclient calls a method on that object, the server receives the call on a thread from thethread pool on the server machine.This receiving thread makes the call locally to theactual object; and, because the object supports the free threading model, the threadcan make a direct call into the object [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • personata.xlx.pl