Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC Proprietary 9/11/2015 2:16:16 AM 008_P2_CSC_white 1.

Similar presentations


Presentation on theme: "CSC Proprietary 9/11/2015 2:16:16 AM 008_P2_CSC_white 1."— Presentation transcript:

1 CSC Proprietary 9/11/2015 2:16:16 AM 008_P2_CSC_white 1

2 CSC Proprietary 9/11/2015 2:16:16 AM 008_fmt_wht 2 COM

3 CSC India 3 Before COM DLL with exported functions Windows API Dynamic data exchange Procedural programming

4 CSC India 4 From DLLs to COM Interoperability Versioning Language independence Transparent cross-process interoperability

5 CSC India 5 From DLLs to COM New generation requires applications, content, operating systems, networking, distributed services to be integrated as never before Monolithic solutions won’t work –No one vendor or one team of programmers can move fast enough –Need to leverage others’ work and to build and ship smaller pieces incrementally –Need to evolve smaller pieces asynchronously without sacrificing the whole

6 CSC India 6 Component Object Model * Components, Components, Components Binary standard Wire-level standard

7 CSC India 7 Examples Manufacturing process control Controls on the Internet/Intranet client Component based server applications –Active Server Pages –Microsoft Transaction Server (Viper) Multi-tier “client/server” applications

8 CSC India 8 3 Tier Client/Server Third-Tier - Databases, Legacy Data COMCOM SNA etc... CORBA Mid-Tier - HTTP, Biz Objects, Active Server Pages, Scripting COMCOM HTTPHTTP “Desktop” - Compound Documents, Controls, Scripting

9 CSC India 9 Interface based Programming An interface, defines a set of public methods without including any implementation. An object has an interface and outer world interacts only with the interfaces. With out interface object has no existence. Object “XYZ” Interface A1’

10 CSC India 10 COM Fundamentals A binary standard for function calling between components. A provision for strongly-typed groupings of functions into interfaces. A base interface providing: A way for components to dynamically discover the interfaces implemented by other components. Reference counting to allow components to track their own lifetime and delete themselves when appropriate

11 CSC India 11 COM Fundamentals A mechanism to uniquely identify components and their interfaces. A "component loader" to set up component interactions and additionally in the cross-process and cross-network cases to help manage component interactions.

12 CSC India 12 Binary Standard Standard way to lay out virtual function tables (vtables). Standard way to call functions through the vtables. An interface is actually a pointer to a vtable

13 CSC India 13 The Rules of the Component Object Model Rule: Must Implement IUnknown An object is not a Microsoft Component Object Model (COM) object unless it implements at least one interface that at minimum is IUnknown. COM defines one special interface, IUnknown, to implement some essential functionality

14 CSC India 14 The Rules of the Component Object Model IUnknown has three methods: –QueryInterface – AddRef – Release interface IUnknown{ virtual HRESULT QueryInterface(IID& iid, void** ppvObj) = 0; virtual ULONG AddRef() = 0; virtual ULONG Release() = 0; }

15 CSC India 15 The Rules of the Component Object Model QueryInterface COM Object Hi do you have a mobile phone ? Yes It is 9811xxxxx

16 CSC India 16 The Rules of the Component Object Model QueryInterface COM Object Interface X supported? Return pointer to Interface X Error Y Not Supported Interface Y supported?

17 CSC India 17 The Rules of the Component Object Model AddRef and Release are simple reference counting methods. A component object's AddRef method is called when another component object is using the interface; the component object's Release method is called when the other component no longer requires use of that interface.

18 CSC India 18 Globally Unique Identifiers Need to be identify interface uniquely. –Human readable names have risk and probability of collision almost 100% COM uses globally unique identifiers (GUIDs)-128-bit integers that are virtually guaranteed to be unique in the world across space and time-to identify every interface and every object class.

19 CSC India 19 Globally Unique Identifiers a GUID is a data structure defined as follows, where DWORD is 32-bits, WORD is 16-bits, and BYTE is 8- bits: typedef struct GUID { DWORD Data1; WORD Data2; WORD Data3; BYTE Data4[8]; } GUID;

20 CSC India 20 Globally Unique Identifiers Each COM interface has an associated GUID called an interface ID (IID) Each Component (Class) has an associated GUID called a class ID (CLSID).

21 CSC India 21 COM Interfaces An interface is not a class An interface is not an object Interfaces are strongly typed Interfaces are immutable

22 CSC India 22 Clients, Servers, and Object Implementers interaction between objects and the users of those objects in COM is based on a client/server model. A client that uses the services of an object is referred as client. An object supplies services, the implementer of that object is usually called the server

23 CSC India 23 Clients, Servers, and Object Object Client Server

24 CSC India 24 Object A unit of functionality that implements one or more interfaces to expose that functionality. For convenience, the word is used both to refer to an object class as well as an individual instantiation of a class. Note that an object class does not need a class identifier in the COM sense such that other applications can instantiate objects of that class—the class used to implement the object internally has no bearing on the externally visible COM class identifier.

25 CSC India 25 Client Any piece of code that is using the services of some object, wherever that object might be implemented An application that drives the flow of operation between itself an other objects and uses specific COM implementation locator services to instantiate or create objects through servers of various object classes.

26 CSC India 26 Server A piece of code that structures an object class in a specific fashion and assigns that class a COM class identifier. This enables a client to pass the class identifier to COM and ask for an object of that class. COM is able to load and run the server code, ask the server to create an object of the class, and connect that new object to the client. A server is specifically the necessary structure around an object that serves the object to the rest of the system and associates the class identifier: a server is not the object itself. The word server is used in discussions to emphasize the serving agent more than the object.

27 CSC India 27 Configuration

28 CSC India 28 Server : In-Process and Out-Of-Process Server in general is some piece of code that structures some object in such a way that COM implementer locator services can run that code and have it create objects –In-Process Server –Local Server –Remote Server

29 CSC India 29 In-Process Server A server that can be loaded into the client's process space and serves in-process objects. Typically these are implemented as dynamic link libraries or DLLs.

30 CSC India 30 Local Server A server that runs in a separate process on the same computer as the client and serves local objects. This type of server is another complete application of its own thus defining the separate process.

31 CSC India 31 Remote Server A server that runs on a separate computer and therefore always runs in another process as well to serve remote objects. Remote servers may be implemented in either.DLLs or.EXEs; if a remote server is implemented in a.DLL, a surrogate process will be created for it on the remote computer.

32 CSC India 32 Location Transparency COM is designed to allow clients to transparently communicate with objects regardless of where those objects are running, be it the same process, the same computer, or a different computer

33 CSC India 33

34 CSC India 34 COM Library In addition to being a specification, COM is also an implementation contained what is called The COM Library.

35 CSC India 35 COM Library A small number of fundamental API functions that facilitate the creation of COM applications, both clients and servers. For clients, COM supplies basic object creation functions; for servers the facilities to expose their objects.

36 CSC India 36 COM Library Implementation locator services through which COM determines from a class identifier which server implements that class and where that server is located. This includes support for a level of indirection, usually a system registry, between the identity of an object class and the packaging of the implementation such that clients are independent of the packaging which can change in the future.

37 CSC India 37 COM Library Transparent remote procedure calls when an object is running in a local or remote server. A standard mechanism to allow an application to control how memory is allocated within its process.

38 CSC India 38 COM Library Transparent remote procedure calls when an object is running in a local or remote server. A standard mechanism to allow an application to control how memory is allocated within its process.

39 CSC India 39 COM Architecture Component COM run time provider Security RPC RPC COM run time Security provider RPC RPC Protocol stack DCOM network- protocol Remote Client

40 CSC India 40 Distributed COM Just COM “with a longer wire” All COM components communicate in the same way –on the same machine In-process, Out-of-process –across a Local Area Network –across a Wide Area Network –across the Internet

41 CSC India 41 COM Runtime Architecture Pluggable Transports COM Client Component COM Runtime Server MachineClient Machine TCP, UDP SPX,IPX Net BUI HTTP “Falcon” COM Runtime Proxy Other Component

42 CSC India 42 Processes, Apartments, and Threads A process is a collection of virtual memory space, code, data, and system resources A thread is code that is to be serially executed within a process the simplest way to view COM’s threading architecture is to think of all the COM objects in the process as divided into groups called apartments

43 CSC India 43 Processes, Apartments, and Threads Every COM object is owned by an apartment. A given thread can execute in only one apartment at a time COM recognizes two kinds of apartment –Single-Threaded Apartment (STA) –Multi-Threaded Apartment (MTA).

44 CSC India 44 Single-threaded Apartments Each thread that uses Object in a separate “apartment”, and COM synchronizes all incoming calls with the windows message queue. A process with a single thread of execution is simply a special case of this model.

45 CSC India 45 Multi-threaded Apartments Multiple threads in a single free-threaded apartment use COM and calls to COM objects are synchronized by the objects themselves

46 CSC India 46 vTable as a Standard In-Memory Representation

47 CSC India 47 Type Libraries and Language Independence In COM, clients and objects communicate through vTable binding. Clients and objects must be savvy in their use of function pointers What about Visual Basic which does not have any built in support for function pointers?

48 CSC India 48 Type Libraries and Language Independence Visual Basic provides this assistance by adding support to its compiler and adding a Visual Basic-to-COM mapping layer in its runtime. On the client side, the Visual Basic compiler automatically creates the vTable binding code required to access a COM object.

49 CSC India 49 Type Libraries and Language Independence As Visual Basic uses a mapping layer, it hides many of the underlying details from its programmers. The Visual Basic compiler can't make sense of a C++ header file. Therefore, it can't use an abstract base class at compile time to build the client-side binding code.

50 CSC India 50 Type Libraries and Language Independence Visual Basic uses interface definitions that are published inside a special type of file called a type library. The type library tells the Visual Basic compiler what the vTable for each interface looks like

51 CSC India 51 Type Library A type library is a binary repository that contains type information about such things as interfaces and components. It's essentially a catalog that defines a collection of COM data type.

52 CSC India 52 Type Library Inside a type library, each interface is defined as a set of methods; each component is defined as a set of one or more implemented interfaces.

53 CSC India 53 ActiveX? A marketing name for a set of technologies and services, all based on COM (the model and the services)

54 CSC India 54 Active Components in Visual Basic ActiveX Control ActiveX DLL ActiveX Exe ActiveX Document Active Components that can be created by Visual Basic are basically COM Servers.

55 CSC India 55 Active Components in Visual Basic When a programmer compiles an ActiveX DLL or an ActiveX EXE, Visual Basic automatically creates a type library for the programmer and bundles it into the server's executable image. This type library contains definitions for the components (coclasses) provided by the server and a definition for each interface defined in the server's project

56 CSC India 56 Interface Definition Language Type libraries can be built using a special language called Interface Definition Language (IDL) While type libraries define interfaces and coclasses in a binary format, IDL does the same thing in a readable text-based format

57 CSC India 57 Interface Definition Language library DogServerLib { interface IDog { HRESULT Bark(); HRESULT RollOver([in] int Rolls); }; interface IwonderDog { HRESULT FetchSlippers(); }; coclass CBeagle { interface IDog; interface IWonderDog; }; };

58 CSC India 58 Type Library [uuid(3B46B8A8-CA17-11D1-920B-709024000000) ] interface _IDog { // methods }; [ uuid(3B46B8AB-CA17-11D1-920B-709024000000) ] coclass CBeagle { // interfaces }; };

59 CSC India 59 Automation A scripting client can't read an interface definition from a type library, it can't generate the required client-side binding code to access the vTables associated with a COM object. (Example VBScript and JavaScript) These clients still have a way to invoke methods in a special type of object known as an automation object. An automation object is an object that implements a special interface named IDispatch.

60 CSC India 60 IDispatch Automation relies on an interface named IDispatch, which allows clients to create method bindings at runtime in a process known as late binding. IDispatch extends IUnknown by adding four methods –GetIDsOfNames() –GetTypeInfoCount() –GetTypeInfo() –Invoke()

61 CSC India 61 IDispatch

62 CSC India 62 Automation and Late Binding Automation clients use the two methods GetIDsOfNames and Invoke to achieve late binding. After a client receives an IDispatch reference, it can ask an object whether it supports a particular method by calling GetIDsOfNames. If the object doesn't support the requested method, the call to GetIDsOfNames fails. If the method is supported, GetIDsOfNames returns a logical identifier for the method called a DISPID.

63 CSC India 63 Automation and Late Binding A DISPID is simply an integer that an object provides to identify one of its methods. Positive DISPIDs indicate that a method is user-defined, while negative DISPIDs are reserved for methods with special purposes. A client armed with a valid DISPID can execute a method through automation by calling Invoke. Invoke is the only way to execute a user-defined method through the IDispatch interface.

64 CSC India 64 Automation and Late Binding Late binding is great for clients that can't deal with direct vTable binding, but it offers pretty slow execution times compared with direct vTable binding. A Visual Basic client can access an automation object through IDispatch by using the Object data type.

65 CSC India 65 Dual Interfaces A dual interface is a hybrid interface of two earlier type of interfaces (One who reads from vTable and another that uses the late binding) The vTable behind a dual interface contains user- defined methods in addition to the methods of IDispatch. Clients can access a dual interface either through direct vTable binding or through late binding.

66 CSC India 66 Dual Interfaces

67 CSC India 67 Dual Interfaces Visual Basic objects can cater to both custom vTable- bound clients and scripting clients as they use Dual Interfaces.

68 CSC India 68 Interaction with COM Library

69 CSC India 69 Object Activation A client application can discover the CLSID of a coclass as well as which IIDs it supports at compile time through inspection of a type library. COM requires that no other dependencies be built between clients and components. As long as a client knows which CSLID and IID to use, it can create and bind to an object.

70 CSC India 70 Object Activation Once the binding has taken place, the client can invoke methods on the object by accessing the vTable associated with the IID The act of creating and binding to an object is called activation. When a client wants to activate an object, it must make a call to the COM library and pass a CLSID and an IID.

71 CSC India 71 Service Control Manager The COM service that helps clients with activation is known as the Service Control Manager (SCM). The SCM is entirely dependent on information stored in the Registry Every loadable component associated with an in-process server or an out-of-process server must have a Registry entry that associates it with a physical path to a server's binary image.

72 CSC India 72 CLSIDs and the Windows Registry The Registry key HKEY_CLASSES_ROOT\CLSID lists the components available to client applications on the machine. Each CLSID key contains one or more subkeys that hold configuration data about the associated component

73 CSC India 73 CLSIDs and the Windows Registry

74 CSC India 74 CLSIDs and the Windows Registry Components in an in-process server require an InprocServer32 sub key. Components in an out-of-process server require a LocalServer32 sub key. These keys hold the physical path to the server file; the SCM uses them to find and load the servers during object activation.

75 CSC India 75 Other Windows Registry The Registry key HKEY_CLASSES_ROOT\TypeLib tracks the physical location of every registered type library on the local machine Each IID plays an important role in binding an object to a client that's running in another process. Every interface that is remotable must have associated configuration data in the Registry key HKEY_CLASSES_ROOT\Interface.

76 CSC India 76 Registering a server All modern COM servers are self-registering Visual Basic automatically generates self-registration code when a server is build. In-process servers are passive and can't register or unregister themselves without a little help. One can register and unregister them using the Win32 SDK utility REGSVR32.EXE

77 CSC India 77 Registering a server REGSVR32.EXE ABC.DLL REGSVR32.EXE /u DOGSERVER.DLL REGSVR32.EXE doesn't actually register the server. It simply loads the server and calls a well-known function exposed by the DLL The DLL responds to this call by writing its required configuration information to the Registry.

78 CSC India 78 Registering a server Out-of-process servers register themselves each time they're launched.

79 CSC India 79 Service Control Manager & Registry Client passes a CLSID and an IID to the SCM. The SCM starts by looking up the CLSID in the Registry and finding the path to the associated DLL file. The SCM loads the DLL into the client's process. The SCM then calls into the DLL through a well-known entry point and forwards the activation request by passing both the CLSID and the IID.

80 CSC India 80 Service Control Manager in Action The server responds by creating a new instance of the requested class. It then passes an interface reference (based on the requested interface) back to the SCM. SCM simply forwards this interface reference back to the client. Once the client gets this interface reference, it has been successfully bound to the object.

81 CSC India 81 Service Control Manager in Action The client can now begin making method calls on the object. Every COM server must therefore provide support for object activation by exposing a well-known entry point through which the SCM can make activation requests.

82 CSC India 82 Class Factories When the SCM interacts with a server to activate an object, it must acquire a reference to a special type of object called a class factory. class factory object is an agent that creates instances of the class associated with a specific CLSID upon request. A COM server must provide a class factory object for each supported CLSID.

83 CSC India 83 Class Factories When the SCM receives an activation request, it must acquire a reference to the appropriate class factory object. It does this in different ways, depending on whether the server code is in an in-process DLL or an out-of-process EXE Visual Basic transparently builds in class factory support for each public creatable class

84 CSC India 84 Class Factories

85 CSC India 85 ProgIDs A ProgID is simply a text-based alias for a specific CLSID. ProgID consists of two parts. –The first part is the name of the server project that holds the component. –The second part is the friendly class name of the component itself. The self-registration code in a Visual Basic server adds Registry entries to map a ProgID to each supported CLSID to support activation for applications.

86 CSC India 86 Out of Process COM: Details COM's remoting architecture requires that a proxy/stub layer be introduced between the client and the object. The proxy and the stub establish an Remote Procedure Call (RPC) channel between them to transmit method- related data back and forth.

87 CSC India 87 Out of Process COM: Details When the client invokes a method on the proxy, the proxy forwards the request to the stub. To properly transmit this request, the proxy marshals the method's inbound parameters and then transmits them to the stub across the RPC channel. When the stub receives the request, it unmarshals the inbound parameters and performs the actual call on the object. the object completes the method and returns control back to the stub, the stub prepares a response by marshaling outbound parameters and a return value.

88 CSC India 88 Out of Process COM: Details The data is then transmitted back to the proxy Finally, the proxy unmarshals the data in the response and returns control back to the client. Proxies and stubs are created by COM Runtime. When a client calls an object in another process, the proxy and stub are created automatically by the system The client is bound to the proxy instead of the object. And it's the stub, not the client, that gets bound to the object. Neither the client nor the object can tell that calls are being remoted.

89 CSC India 89 Universal Marshaler The universal marshaler uses interface definitions in type libraries to build proxies and stubs. Interfaces that rely on the universal marshaler are defined in type libraries and in IDL with the [oleautomation] attribut. Every proxy/stub pair is based on a specific IID. When the COM runtime needs to build either a proxy or a stub, it examines the Registry key HKEY_CLASSES_ROOT\Interface to locate configuration information for the interface in question. There is a ProxyStubClsid32 key, which tells the COM runtime where to find the code to build proxies and stubs.

90 CSC India 90 Universal Marshaler The Registry settings for an interface marked with the [oleautomation] attribute tell the COM runtime to use the universal marshaler. They also tell the universal marshaler which type library holds the interface definition

91 CSC India 91 Universal Marshaler When the COM runtime determines that it needs to build a proxy or a stub for an oleautomation interface, it forwards the request to the universal marshaler. The universal marshaler then inspects the Registry to find the GUID for the type library associated with the IID. The universal marshaler then locates the type library Once the universal marshaler has the path, it can load the type library, read the interface definition, and build a proxy or a stub.

92 CSC India 92 Universal Marshaler The universal marshaler uses the TypeLib key to find the physical path to the type library. This path can point to either a stand-alone.TLB file or a COM server with an embedded type library.

93 CSC India 93 Thanks


Download ppt "CSC Proprietary 9/11/2015 2:16:16 AM 008_P2_CSC_white 1."

Similar presentations


Ads by Google