OLE Object Linking - PowerPoint PPT Presentation

1 / 108
About This Presentation
Title:

OLE Object Linking

Description:

Component Object Model (COM) Object IDs and Interface IDs. System Registry ... RegEdit.EXE or RegEdt32.EXE. OLE information is installed ... Entries. REGEDIT ... – PowerPoint PPT presentation

Number of Views:232
Avg rating:3.0/5.0
Slides: 109
Provided by: taiwe
Category:
Tags: ole | linking | object | regedit

less

Transcript and Presenter's Notes

Title: OLE Object Linking


1
OLEObject Linking Embedding
  • ??????

2
Content
  • Basic Concept
  • OLE Technologies
  • Component Object Model (COM)
  • Object IDs and Interface IDs
  • System Registry
  • Object Interfaces
  • Object Polymorphism and Reusability
  • Using COM Objects
  • Memory Management
  • Desktop Management

3
OLEObject Linking Embedding
  • Basic Concept

4
Objects and Interfaces
Knowing what interfaces an object provides then
we know what can be done by the object, i.e., the
features the object possesses.
What functions these objects provide?
How to access these objects?
5
Objects and Interfaces
Knowing what interfaces an object provides then
we know what can be done by the object, i.e., the
features the object possesses.
  • To access the objects you need to know
  • What interfaces they provide?
  • How to operate their interfaces?

6
OLE Object Linking and Embedding
How to achieve embedding and linking to objects
from different sources?
Systematically, these objects must be equipped
with dedicated interfaces for object embedding
and linking.
7
In-Place Editing
8
ActiveX Controls
9
Exercises
  • What are the differences between object linking
    and object embedding? You also give an example
    using Microsoft Office.
  • What is OLE automation?
  • Design a web page making use an ActiveX control
    to display multimedia content.
  • What are the differences between Java applets and
    ActiveX controls.

10
OLEObject Linking Embedding
  • OLE Technologies

11
Overview
  • Object oriented programming (OOP)
  • effective in developing reusable software
    components.
  • How to build an object-oriented operating system?
  • Microsoft solution ? OLE.
  • OLEs contribution
  • Facilitates and enables the integration of
    components.
  • Works independently of programming languages
    through a binary standard.
  • Provides multiple interfaces to access an objects.

12
Why OLE?
  • Need network applications and distributed
    computation.
  • Need interoperability between applications.
  • Software revision ? version handling
  • To use a system service in a polymorphic fashion
  • Software integration
  • components developed using different languages
  • components running in separate processes or on
    separate machines
  • Self-expressibility of software
  • Tradition service models makes it nearly
    impossible for the service provider to express
    new, enhanced, or unique capabilities to
    potential clients in a uniform fashion.

13
Component Software
  • Software component objects are much like
    integrated circuit (IC) component, and component
    software is the integrated circuitry of tomorrow.
  • OLE offers a solution and future extensible
    standards and mechanism to enable software
    developers to package their functionality, and
    content, into reusable components, like an
    integrated circuit.
  • Plug and play ? OLE allows you to buy a binary
    component and reuse it, plugging into it through
    its external interfaces.

14
OLE Defined
OLE is no longer given a version number.
  • OLE is a unified environment of object-based
    services
  • with the capability of
  • customizing those services and
  • arbitrarily extending the architecture through
    custom services,
  • with the overall purpose of
  • enabling rich integration between components.

15
Clients and Severs
Sever
Object
Client
Object
How to define Objects Interfaces?
16
Object-Based Components COM
  • Component Object Model (COM) is a binary standard
    for software componentry introduced by Microsoft
    in 1993.
  • It is used to enable interprocess communication
    and dynamic object creation in any programming
    language that supports the technology.
  • The term COM is often used in the software
    development world as an umbrella term that
    encompasses the OLE, OLE Automation, ActiveX,
    COM and DCOM technologies.

17
Classical Objects vs. OLE Objects
  • Classic Objects
  • Encapsulation
  • Polymorphism
  • Inheritance
  • OLE Objects
  • Encapsulation
  • Polymorphism
  • Reusabilility

? Source code must be available
? Using off-the-shelf objects directly
18
Object Implementation
  • Any object can be seen as a set of
  • properties (data members or content) and
  • methods (member functions).

Object
Properties (data members, content)
19
OLE Objects OLE Interfaces
A client accesses a specific feature of an object
through a particular interface.
Hence, an interface is, in fact, a group of
semantically related functions to expose a
specific feature.
  • The nature of an OLE object, then, is not
    expressed in how it is implemented internally but
    rather in how it is exposed externally.

Interface function table, or vtable
Object Implementation of interface functions
20
OLE Objects OLE Interfaces
A client accesses a specific feature of an object
through a particular interface.
  • The nature of an OLE object, then, is not
    expressed in how it is implemented internally but
    rather in how it is exposed externally.

Can be implemented using any languages
A client access the object only through the
interface pointer.
Interface function table, or vtable
Object Implementation of interface functions
21
Simplified Notation on OLE Objects OLE
Interfaces
A client accesses a specific feature of an object
through a particular interface.
Interfaces are drawn as plug-in jacks extending
from the object.
Interface pointer
22
Multiple Interfaces
A client accesses a specific feature of an object
through a particular interface.
An object may have multiple features meaning that
it may provide multiple interfaces.
Interfaces are drawn as plug-in jacks extending
from the object.
Object
Interface pointer
Interface_1 pointer
Interface_2 pointer
Interface_n pointer
23
Multiple Interfaces
A client accesses a specific feature of an object
through a particular interface.
An object may have multiple features meaning that
it may provide multiple interfaces.
How to know what interfaces that a object
provides?
Interfaces are drawn as plug-in jacks extending
from the object.
The answer is COM.
Object
Interface_1 pointer
Interface_2 pointer
Interface_n pointer
24
OLETechnologies
25
OLETechnologies
26
OLEObject Linking Embedding
  • Component Object Model (COM)

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

28
COM Objects and Interfaces
In convention, the names of COM interface start
with character I.
IUnknown
AV-Device Object
IOnOff
IAudioControl
IDVDPlayControl
29
IUnknown The root of all evil
IUnknown
AV-Device Object
IOnOff
IAudioControl
IDVDPlayControl
30
IUnknown The root of all evil
interface IUnknown virtual HRESULT
QueryInterface(REFIID riid, void ppvObject)0
virtual ULONG AddRef(void)0 virtual ULONG
Release(void)0
IUnknown
IUnknown is the base interface of every custom
COM interface.
AV-Device Object
IOnOff
IAudioControl
IDVDPlayControl
31
IUnknown The root of all evil
interface IUnknown virtual HRESULT
QueryInterface(REFIID riid, void ppvObject)0
virtual ULONG AddRef(void)0 virtual ULONG
Release(void)0
IUnknown
IUnknown is the base interface of every custom
COM interface.
AV-Device Object
IOnOff
// Example interface IOnOff IUnknown
virtual HRESULT On()0 virtual HRESULT
Off()0
IAudioControl
IDVDPlayControl
32
Function Tables for COM Interfaces
Function table for IUnknown
Function table for another interface
A pointer to this interface can also be used
as a pointer to IUnknown
33
OLEObject Linking Embedding
  • Object IDs
  • and
  • Interface IDs

34
IUnknown
interface IUnknown virtual HRESULT
QueryInterface(REFIID riid, void ppvObject)0
virtual ULONG AddRef(void)0 virtual ULONG
Release(void)0
IUnknown
In COM, every interface is also an IUnknown
interface.
35
IUnknownQueryInterface
interface IUnknown virtual HRESULT
QueryInterface(REFIID riid, void ppvObject)0
virtual ULONG AddRef(void)0 virtual ULONG
Release(void)0
Query an interface by giving the reference of the
interface ID.
Who assign the ID?
In COM, every interface is also an IUnknown
interface.
36
GUID Globally Unique Identifier
  • GUID is a special type of identifier used in
    software applications in order to provide a
    reference number which is unique in any context
    (hence, "Globally").
  • It is a 128-bit randomly generated number
    developed by Microsoft based on a carefully
    developed algorithm
  • The chance of generating duplicate GUIDs in two
    different places at different times, even without
    a network card, is about the same as two random
    atoms in the universe colliding to form a small
    California avocado mated to a New York City sewer
    rat. In other words, don't worry about it.
  • Represented in form of
  • F9043C85-F6F2-101A-A3C9-08002B2F49FB

37
GUID Globally Unique Identifier
  • GUID is a special type of identifier used in
    software applications in order to provide a
    reference number which is unique in any context
    (hence, "Globally").
  • It is a 128-bit randomly generated number
    developed by Microsoft based on a carefully
    developed algorithm
  • The chance of generating duplicate GUIDs in two
    different places at different times, even without
    a network card, is about the same as two random
    atoms in the universe colliding to form a small
    California avocado mated to a New York City sewer
    rat. In other words, don't worry about it.
  • Represented in form of
  • F9043C85-F6F2-101A-A3C9-08002B2F49FB

typedef struct _GUID ULONG Data1
WORD Data2 WORD Data3 UCHAR
Data48 GUID, PGUID
38
GUID Generator (GUDIGEN.EXE)
UUID Generation
39
GUID Generator (GUDIGEN.EXE)
// 920EA478-7DEA-4176-861E-823418B23191 DEFINE_G
UID(ltltnamegtgt, 0x920ea478, 0x7dea, 0x4176, 0x86,
0x1e, 0x82, 0x34, 0x18, 0xb2, 0x31, 0x91)
40
Class IDs (CLSID) and Interface IDs (IID)
CLSID_Xxx or IID_Xxx
// 920EA478-7DEA-4176-861E-823418B23191 DEFINE_G
UID(ltltnamegtgt, 0x920ea478, 0x7dea, 0x4176, 0x86,
0x1e, 0x82, 0x34, 0x18, 0xb2, 0x31, 0x91)
ltltnamegtgt
41
Class IDs (CLSID) and Interface IDs (IID)
  • CLSIDs are GUIDs which are used by Windows to
    identify software components without having to
    know their name
  • Naming convention CLSID_Xxx, e.g.,
  • // GUID for HTML viewer is 25336920-03F9-11cf-8F
    D0-00AA00686F13
  • DEFINE_GUID(CLSID_HTMLViewer,
  • 0x25336920, 0x3f9, 0x11cf, 0x8f, 0xd0, 0x0, 0xaa,
    0x0, 0x68, 0x6f, 0x13)
  • IIDs are GUIDs associated with interfaces
  • Naming convention IID_IXxx, e.g.,
  • // 00000000-0000-0000-C000-000000000046
  • DEFINE_GUID(IID_IUnknown,
  • 0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x46)
  • // 00000001-0000-0000-C000-000000000046
  • DEFINE_GUID(IID_IClassFactory,
  • 0x00000001, 0x0000, 0x0000, 0xC0, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x46)

42
COM API Functions for GUIDs
43
include ltinitguid.hgt
  • Any code that ever refers to any GUID, be it a
    CLSID or an IID, must include a standard OLE
    header file, INITGUID.H, once and only once in
    the entire compilation of a DLL or an EXE.

44
OLEObject Linking Embedding
  • System Registry

45
RegEdit.EXE or RegEdt32.EXE
OLE information is installed
46
CLSID Registration
OLE information is installed
all the information about component classes is
installed
47
Object IDs
all the information about component classes is
installed
48
ExampleMicrosoft Word Document
49
ExampleMicrosoft Word Document
Convert to new version
50
ExampleMicrosoft Word Document
Convert to new version
51
ProgID Programmatic Identifier
52
ProgID Programmatic Identifier
53
ProgID Programmatic Identifier
  • A higher-level abstraction of CLSID
  • To allow for Binary Compatible objects and to
    make it easier to get at COM objects
  • Standard format
  • ltVendorgt.ltComponentgt.ltVersiongt
  • e.g.,
  • Microsoft.Chart.5 or Lotus.AmiProDocument.4.2

54
Functions for CLSID ProgID
55
Creation of Registry Entries
TatungComponent.reg
REGEDIT HKEY_CLASSES_ROOT\Tatung.Component.3
Tatung Component Version 3.0 HKEY_CLASSES_ROOT\Tat
ung.Component.3\CLSID B02AF1A5-1F64-49b0-8D7A-0
EDE811630DC HKEY_CLASSES_ROOT\Tatung.Component
Tatung Component HKEY_CLASSES_ROOT\Tatung.Componen
t\CurVer Tatung.Component.3 HKEY_CLASSES_ROOT\CL
SID\B02AF1A5-1F64-49b0-8D7A-0EDE811630DC
Tatung Component 3.0 HKEY_CLASSES_ROOT\CLSID\B02A
F1A5-1F64-49b0-8D7A-0EDE811630DC\ProgID
Tatung.Component.3 HKEY_CLASSES_ROOT\CLSID\B02AF1
A5-1F64-49b0-8D7A-0EDE811630DC\VersionIndependent
ProgID Tatung.Component
56
Creation of Registry Entries
TatungComponent.reg
REGEDIT HKEY_CLASSES_ROOT\Tatung.Component.3
Tatung Component Version 3.0 HKEY_CLASSES_ROOT\Tat
ung.Component.3\CLSID B02AF1A5-1F64-49b0-8D7A-0
EDE811630DC HKEY_CLASSES_ROOT\Tatung.Component
Tatung Component HKEY_CLASSES_ROOT\Tatung.Componen
t\CurVer Tatung.Component.3 HKEY_CLASSES_ROOT\CL
SID\B02AF1A5-1F64-49b0-8D7A-0EDE811630DC
Tatung Component 3.0 HKEY_CLASSES_ROOT\CLSID\B02A
F1A5-1F64-49b0-8D7A-0EDE811630DC\ProgID
Tatung.Component.3 HKEY_CLASSES_ROOT\CLSID\B02AF1
A5-1F64-49b0-8D7A-0EDE811630DC\VersionIndependent
ProgID Tatung.Component
57
Creation of Registry Entries
TatungComponent.reg
REGEDIT HKEY_CLASSES_ROOT\Tatung.Component.3
Tatung Component Version 3.0 HKEY_CLASSES_ROOT\Tat
ung.Component.3\CLSID B02AF1A5-1F64-49b0-8D7A-0
EDE811630DC HKEY_CLASSES_ROOT\Tatung.Component
Tatung Component HKEY_CLASSES_ROOT\Tatung.Componen
t\CurVer Tatung.Component.3 HKEY_CLASSES_ROOT\CL
SID\B02AF1A5-1F64-49b0-8D7A-0EDE811630DC
Tatung Component 3.0 HKEY_CLASSES_ROOT\CLSID\B02A
F1A5-1F64-49b0-8D7A-0EDE811630DC\ProgID
Tatung.Component.3 HKEY_CLASSES_ROOT\CLSID\B02AF1
A5-1F64-49b0-8D7A-0EDE811630DC\VersionIndependent
ProgID Tatung.Component
58
Registry Functions
Windows
Window Mobile
59
OLEObject Linking Embedding
  • Object Interfaces

60
Interfaces
  • An object is an entity that can be experienced or
    used only through its interfaces.
  • Interfaces are nothing more than semantically
    related groups of functions.

61
Interfaces
  • An object is an entity that can be experienced or
    used only through its interfaces.
  • Interfaces are nothing more than semantically
    related groups of functions.
  • Each interface uniquely identifies an object's
    support for a particular feature across time and
    space by virtue of its IID
  • Symbolic name IID_ltInterfacegt, e.g.,
    IID_IConnectionPoint or IID_IUnknown

62
InterfaceDefinition
//This is a convenience for documentation. define
interface struct typedef / unique /
IUnknown __RPC_FAR LPUNKNOWN EXTERN_C const IID
IID_IUnknown if defined(__cplusplus)
!defined(CINTERFACE) interface IUnknown
public virtual HRESULT __stdcall
QueryInterface( / in / REFIID
riid, / out / void __RPC_FAR
__RPC_FAR ppvObject) 0 virtual ULONG
__stdcall AddRef(void) 0 virtual ULONG
__stdcall Release(void) 0 else / C
style interface / typedef struct
IUnknownVtbl HRESULT ( __stdcall
__RPC_FAR QueryInterface )( IUnknown
__RPC_FAR This, / in / REFIID
riid, / out / void __RPC_FAR
__RPC_FAR ppvObject) ULONG ( __stdcall
__RPC_FAR AddRef )( IUnknown
__RPC_FAR This) ULONG ( __stdcall
__RPC_FAR Release )( IUnknown
__RPC_FAR This) IUnknownVtbl
interface IUnknown CONST_VTBL
struct IUnknownVtbl __RPC_FAR lpVtbl
endif / C style interface /
63
Interface Description Language
// IPlayDvd.idl uuid(0112114b-1111-2233-3f79-0000
00001146), object interface IPlayDvd
IUnknown import "unknwn.idl" HRESULT
Play(in int iSpeed) HRESULT Stop(void)
HRESULT Pause(void) HRESULT DvdReady(out
int bDvdReady) HRESULT JumpTo(in, out int
nPosition)
64
Interface Description Language
// IPlayDvd.idl uuid(0112114b-1111-2233-3f79-0000
00001146), object interface IPlayDvd
IUnknown import "unknwn.idl" HRESULT
Play(in int iSpeed) HRESULT Stop(void)
HRESULT Pause(void) HRESULT DvdReady(out
int bDvdReady) HRESULT JumpTo(in, out int
nPosition)
65
MIDL.EXE (Microsoft IDL Compiler)
  • Generating header file
  • for implementation
  • For client
  • Generating source code for a proxy and stub that
    can marshal the interface across a process
    boundary
  • Generating type library
  • for tools

66
MIDL.EXE (Microsoft IDL Compiler)
67
MIDL.EXE (Microsoft IDL Compiler)
68
Macros
IUnknown.h
include ltobjbase.hgt undef INTERFACE define
INTERFACE IUnknown DECLARE_INTERFACE(IUnknown)
STDMETHOD(QueryInterface)(THIS_ REFIID riid,
void ppvObject) PURE STDMETHOD_(ULONG,
AddRef)(THIS) PURE STDMETHOD_(ULONG,
Release)(THIS) PURE // 00000000-0000-0000-C0
00-000000000046 DEFINE_GUID(IID_IUnknown, 0x00000
000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x46) typedef IUnknown
LPUNKNOWN
69
Macros
IUnknown.h
For functions returning HRESULT
include ltobjbase.hgt undef INTERFACE define
INTERFACE IUnknown DECLARE_INTERFACE(IUnknown)
STDMETHOD(QueryInterface)(THIS_ REFIID riid,
void ppvObject) PURE STDMETHOD_(ULONG,
AddRef)(THIS) PURE STDMETHOD_(ULONG,
Release)(THIS) PURE // 00000000-0000-0000-C0
00-000000000046 DEFINE_GUID(IID_IUnknown, 0x00000
000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x46) typedef IUnknown
LPUNKNOWN
For functions not returning HRESULT
70
Macros
include ltobjbase.hgt undef INTERFACE define
INTERFACE IFoo DECLARE_INTERFACE_(IFoo,
IUnknown) // IUnknown methods
STDMETHOD(QueryInterface)(THIS_ REFIID, void )
PURE STDMETHOD_(ULONG, AddRef)(THIS) PURE
STDMETHOD_(ULONG, Release)(THIS) PURE
STDMETHOD (SetValue)(THIS_ int) PURE
STDMETHOD (GetValue)(THIS_ int ) PURE //
A46C12C0-4E88-11ce-A6F1-00AA0037DEFB DEFINE_GUID
(IID_IFoo, 0xa46c12c0, 0x4e88, 0x11ce, 0xa6,
0xf1, 0x0, 0xaa, 0x0, 0x37, 0xde, 0xfb) typedef
IFoo LPFOO
71
HRESULT
Describes what actually took place, error or
otherwise.
0 Success 1 Error
Indicates which group of status codes this
belongs to. Help to determine when or where the
error occurred.
72
HRESULT
Commonly used HRESULT/SCODE
S_OK Function succeeded. Also used for functions
that semantically return a Boolean TRUE result
to indicate that the function succeeded.
S_FALSE Used for functions that semantically
return a Boolean FALSE result to indicate that
the function succeeded. E_NOINTERFACE QueryInter
face did not recognize the requested interface.
E_NOTIMPL Member function contains no
implementation. E_FAIL Unspecified failure.
E_OUTOFMEMORY Function failed to allocate
necessary memory.
73
HRESULT
Using macros SUCCEEDED and FAILED to determine
whether a function invocation succeeded or failed.
HRESULT hr IAnInterface pInterface NULL . .
. . . . . . . . . . . // get pInterface using
some method hr pInterface-gtmemberFun1(...) if(S
UCCEEDED(hr)) ............ hr
pInterface-gtmemberFun2(...) if(FAILED(hr))
............
74
Error Lookup (ErrLook.exe)
75
Error Lookup (ErrLook.exe)
76
Exercise
  • Using ErrLook.exe to lookup some errors defined
    in WinError.h

77
Review IUnknown
interface IUnknown virtual HRESULT
QueryInterface(REFIID riid, void ppvObject)0
virtual ULONG AddRef(void)0 virtual ULONG
Release(void)0
78
Methods to Getthe 1st Interface Pointer of an
Object
  • Call an API function that creates an object of
    only one type and returns only one type of
    interface pointer, e.g.,
  • CreateTypeLib returns ICreateTypeLib
  • Call a member function through an interface of an
    object that you already have, which returns an
    interface pointer to a different object. e.g.,
  • IStorageOpenStream returns IStream
  • Implement on an object of your own an interface
    through which other objects will pass their own
    interface pointers, e.g.,
  • IConnectionPointAdvise
  • Call an API function that given a class
    identifier creates an object and returns any type
    of interface pointer you request, e.g.,
  • CoCreateInstance

79
ReferenceCounting
LPSOMEINTERFACE pISome1 NULL //Some1
object LPSOMEINTERFACE pISome2 NULL //Some2
object LPSOMEINTERFACE pCopy NULL //A
function that creates the pointer calls
AddRef. CreateISomeObject(pISome1) //Some1 ref
count1 CreateISomeObject(pISome2) //Some2 ref
count1 pCopypISome1 //Some1
count1 pCopy-gtAddRef() //AddRef new copy,
Some12 Do things. pCopy-gtRelease()
//Release before overwrite, Some11 pCopypISome2
//Some21 pCopy-gtAddRef() //Some22 What
kinds of things do you do? pCopy-gtRelease()
//Release before overwrite, Some21 pCopyNULL
Things that make us go. pISome2-gtRelease()
//Release when done, Some20, Some2
freed pISome2NULL pISome1-gtRelease() //Release
when done, Some10, Some1 freed pISome1NULL
80
Interface Attributes
  • Interfaces are not classes
  • Interfaces are not objects
  • Interfaces are strongly typed
  • Interfaces are immutable

81
OLEObject Linking Embedding
  • Object Polymorphism and Reusability

82
Polymorphism
  • The capability to treat objects from multiple
    classes identically because they all share one or
    more interfaces in common
  • ?Existing clients that understand the prototype
    can immediately use those new classes.
  • An interface once defined will never be changed
  • You have to define a new interface if you want to
    change its definition.

83
Polymorphism by C Style
Inheritance
How about if IAnimal need to be changed?
interface IRabbit IAnimal HRESULT
RaidGardens(...) HRESULT Hop(...)
HRESULT DigWarrens(...)
interface IAnimal IUnknown HRESULT
Eat(...) HRESULT Sleep(...) HRESULT
Procreate(...)
interface IKoala IAnimal HRESULT
ClimbEucalyptusTrees(...) HRESULT
PouchOpensDown(...) HRESULT
SleepForHoursAfterEating(...)
84
Polymorphism by COM
How about if IAnimal need to be changed?
interface IRabbit IUnknown HRESULT
RaidGardens(...) HRESULT Hop(...)
HRESULT DigWarrens(...)
interface IAnimal IUnknown HRESULT
Eat(...) HRESULT Sleep(...) HRESULT
Procreate(...)
interface IKoala IUnknown HRESULT
ClimbEucalyptusTrees(...) HRESULT
PouchOpensDown(...) HRESULT
SleepForHoursAfterEating(...)
85
Polymorphism by COM
How about if IAnimal need to be changed?
86
Reusability
A ready to used object
An object to be built
87
Reusability by COM
  • Containment
  • Aggregation

88
Containment
IUnknown knows IKoala and IAnimal
Koala uses Animals implementation of IAnimal,
as would any client.
IUnknown knows only IAnimal
89
Aggregation
IUnknown
Koala (outer object)
IKoala
IUnknown
Animal (Inner Object)
IAnimal
Direct exposure
90
Aggregation
IUnknown
IUnknown knows IKoala and IAnimal
Koala (outer object)
Animal delegates IAnimals IUnknown calls to the
outer IUnknown
IKoala
Koala holds Animals IUnknown pointer to control
Animals lifetime.
IUnknown
IUnknown knows only IAnimal
Animal (Inner Object)
IAnimal
Direct exposure
91
OLEObject Linking Embedding
  • Using COM Objects

92
COM/OLE Library Initialization Uninitialization
Initialization
Uninitialization
CoInitializeEx or OleInitialize
CoUninitialize or OleUninitialize
93
Example Memory Management
  • Define a standard allocation mechanism accessible
    to both object and client so that memory can pass
    freely between them, even across processes.
  • The mechanism is COM's task memory allocation
    service, based on the memory management APIs of
    the underlying system.

94
The Allocator Object
interface IMalloc IUnknown void
Alloc(ULONG cb) void Realloc(void pv,
ULONG cb) void Free(void pv) ULONG
GetSize(void pv) int DidAlloc(void pv)
void HeapMinimize(void)
95
Get the Allocator Object
HRESULT CoGetMalloc( DWORD dwMemContext,
//Indicates if memory is private or shared
LPMALLOC ppMalloc //Address of output variable
that receives a //
pointer to the memory allocator )
96
Example Memory Management
97
Exercise
  • Complete the demonstrating program and enhance it.

98
Instantiating COM Objects
STDAPI CoCreateInstance( REFCLSID rclsid,
//Class identifier (CLSID) of the object
LPUNKNOWN pUnkOuter, //Pointer to controlling
IUnknown DWORD dwClsContext, //Context for
running executable code REFIID riid,
//Reference to the identifier of the interface
LPVOID ppv //Address of output variable
that receives // the
interface pointer requested in riid )
99
Example Desktop Management
  • Provide a mechanism through IActiveDesktop
    interface to allows a client program to manage
    the desktop items and wallpaper on a local
    computer.

100
Example Desktop Management
CLSID of Active Desktop coclass IID of
IActiveDesktop
// 75048700-EF1F-11D0-9888-006097DEACF9 DEFINE_G
UID( CLSID_ActiveDesktop, 0x75048700L, 0xEF1F,
0x11D0, 0x98, 0x88, 0x00, 0x60, 0x97, 0xDE, 0xAC,
0xF9) // F490EB00-1240-11D1-9888-006097DEACF9
DEFINE_GUID(IID_IActiveDesktop, 0xF490EB00L,
0x1240, 0x11D1, 0x98, 0x88, 0x00, 0x60, 0x97,
0xDE, 0xAC, 0xF9)
101
Example Desktop Management
CLSID of Active Desktop coclass IID of
IActiveDesktop
// 75048700-EF1F-11D0-9888-006097DEACF9 DEFINE_G
UID( CLSID_ActiveDesktop, 0x75048700L, 0xEF1F,
0x11D0, 0x98, 0x88, 0x00, 0x60, 0x97, 0xDE, 0xAC,
0xF9) // F490EB00-1240-11D1-9888-006097DEACF9
DEFINE_GUID(IID_IActiveDesktop, 0xF490EB00L,
0x1240, 0x11D1, 0x98, 0x88, 0x00, 0x60, 0x97,
0xDE, 0xAC, 0xF9)
102
Example Desktop Management
CLSID of Active Desktop coclass IID of
IActiveDesktop
// 75048700-EF1F-11D0-9888-006097DEACF9 DEFINE_G
UID( CLSID_ActiveDesktop, 0x75048700L, 0xEF1F,
0x11D0, 0x98, 0x88, 0x00, 0x60, 0x97, 0xDE, 0xAC,
0xF9) // F490EB00-1240-11D1-9888-006097DEACF9
DEFINE_GUID(IID_IActiveDesktop, 0xF490EB00L,
0x1240, 0x11D1, 0x98, 0x88, 0x00, 0x60, 0x97,
0xDE, 0xAC, 0xF9)
103
Example Desktop Management
Create a COM object from the Active Desktop
coclass
HRESULT hr IActiveDesktop pIADNULL //
.................................... // Create a
COM object from the Active Desktop coclass. hr
CoCreateInstance( CLSID_ActiveDesktop,
NULL,
CLSCTX_INPROC_SERVER,
IID_IActiveDesktop,
(void) pIAD )
IActiveDesktop
104
Example Desktop Management
Interface IActiveDesktop
DECLARE_INTERFACE_( IActiveDesktop, IUnknown )
// IUnknown methods STDMETHOD
(QueryInterface)(THIS_ REFIID riid, void ppv)
PURE STDMETHOD_(ULONG, AddRef) ( THIS )
PURE STDMETHOD_(ULONG, Release) ( THIS )
PURE // IActiveDesktop methods
STDMETHOD (ApplyChanges)(THIS_ DWORD dwFlags)
PURE STDMETHOD (GetWallpaper)(THIS_ LPWSTR
pwszWallpaper, UINT cchWallpaper, DWORD
dwReserved) PURE STDMETHOD
(SetWallpaper)(THIS_ LPCWSTR pwszWallpaper, DWORD
dwReserved) PURE STDMETHOD
(GetWallpaperOptions)(THIS_ LPWALLPAPEROPT pwpo,
DWORD dwReserved) PURE STDMETHOD
(SetWallpaperOptions)(THIS_ LPCWALLPAPEROPT pwpo,
DWORD dwReserved) PURE STDMETHOD
(GetPattern)(THIS_ LPWSTR pwszPattern, UINT
cchPattern, DWORD dwReserved) PURE STDMETHOD
(SetPattern)(THIS_ LPCWSTR pwszPattern, DWORD
dwReserved) PURE STDMETHOD
(GetDesktopItemOptions)(THIS_ LPCOMPONENTSOPT
pco, DWORD dwReserved) PURE STDMETHOD
(SetDesktopItemOptions)(THIS_ LPCCOMPONENTSOPT
pco, DWORD dwReserved) PURE STDMETHOD
(AddDesktopItem)(THIS_ LPCCOMPONENT pcomp, DWORD
dwReserved) PURE STDMETHOD
(AddDesktopItemWithUI)(THIS_ HWND hwnd,
LPCOMPONENT pcomp, DWORD dwReserved) PURE
STDMETHOD (ModifyDesktopItem)(THIS_ LPCCOMPONENT
pcomp, DWORD dwFlags) PURE STDMETHOD
(RemoveDesktopItem)(THIS_ LPCCOMPONENT pcomp,
DWORD dwReserved) PURE STDMETHOD
(GetDesktopItemCount)(THIS_ LPINT lpiCount, DWORD
dwReserved) PURE STDMETHOD
(GetDesktopItem)(THIS_ int nComponent,
LPCOMPONENT pcomp, DWORD dwReserved) PURE
STDMETHOD (GetDesktopItemByID)(THIS_ DWORD dwID,
LPCOMPONENT pcomp, DWORD dwReserved) PURE
STDMETHOD (GenerateDesktopItemHtml)(THIS_ LPCWSTR
pwszFileName, LPCOMPONENT pcomp, DWORD
dwReserved) PURE STDMETHOD (AddUrl)(THIS_
HWND hwnd, LPCWSTR pszSource, LPCOMPONENT pcomp,
DWORD dwFlags) PURE STDMETHOD
(GetDesktopItemBySource)(THIS_ LPCWSTR
pwszSource, LPCOMPONENT pcomp, DWORD dwReserved)
PURE
105
Example Desktop Management
Create a COM object from the Shell Link coclass
HRESULT hr IShellLink pISLNULL //
.................................... // Create a
COM object from the Shell Link coclass. hr
CoCreateInstance( CLSID_SHELLLINK,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink, (void)
pISL )
IShellLink
106
Example Desktop Management
Interface IShellLink
DECLARE_INTERFACE_(IShellLinkW, IUnknown)
// sl // IUnknown methods
STDMETHOD(QueryInterface) (THIS_ REFIID riid,
LPVOID ppvObj) PURE STDMETHOD_(ULONG,AddRef
) (THIS) PURE STDMETHOD_(ULONG,Release)
(THIS) PURE // IShellLink methods
STDMETHOD(GetPath)(THIS_ LPWSTR pszFile, int
cchMaxPath, WIN32_FIND_DATAW pfd, DWORD fFlags)
PURE STDMETHOD(GetIDList)(THIS_
LPITEMIDLIST ppidl) PURE
STDMETHOD(SetIDList)(THIS_ LPCITEMIDLIST pidl)
PURE STDMETHOD(GetDescription)(THIS_ LPWSTR
pszName, int cchMaxName) PURE
STDMETHOD(SetDescription)(THIS_ LPCWSTR pszName)
PURE STDMETHOD(GetWorkingDirectory)(THIS_
LPWSTR pszDir, int cchMaxPath) PURE
STDMETHOD(SetWorkingDirectory)(THIS_ LPCWSTR
pszDir) PURE STDMETHOD(GetArguments)(THIS_
LPWSTR pszArgs, int cchMaxPath) PURE
STDMETHOD(SetArguments)(THIS_ LPCWSTR pszArgs)
PURE STDMETHOD(GetHotkey)(THIS_ WORD
pwHotkey) PURE STDMETHOD(SetHotkey)(THIS_
WORD wHotkey) PURE STDMETHOD(GetShowCmd)(THI
S_ int piShowCmd) PURE STDMETHOD(SetShowCmd)
(THIS_ int iShowCmd) PURE
STDMETHOD(GetIconLocation)(THIS_ LPWSTR
pszIconPath, int cchIconPath, int piIcon) PURE
STDMETHOD(SetIconLocation)(THIS_ LPCWSTR
pszIconPath, int iIcon) PURE
STDMETHOD(SetRelativePath)(THIS_ LPCWSTR
pszPathRel, DWORD dwReserved) PURE
STDMETHOD(Resolve)(THIS_ HWND hwnd, DWORD fFlags)
PURE STDMETHOD(SetPath)(THIS_ LPCWSTR
pszFile) PURE
107
Example Desktop Management
Interface IPersistFile
108
Exercise
  • White an MFC application to set a particular
    image file as the desktop wallpaper.
  • Write an MFC application to simulate the
    active-desktop application provided by OS for
    wallpaper selection.
  • Develop a desktop management program by your
    creativeness.
Write a Comment
User Comments (0)
About PowerShow.com