ole object linking & embedding 主講人:虞台文. content basic concept ole technologies...

108
OLE Object Linking & Embedding 主主主 主主主

Upload: dennis-poole

Post on 15-Jan-2016

245 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

OLEObject Linking & Embedding

主講人:虞台文

Page 2: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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

Page 3: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

OLEObject Linking & Embedding

Basic Concept

Page 4: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Objects and Interfaces

What functions these objects provide?What functions these objects provide?

How to access these objects?How to access these objects?

Knowing what interfaces an object provides then we know what can be done by the object, i.e., the features the object possesses.

Page 5: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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?

Page 6: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

OLE Object Linking and Embedding

0

5

10

15

20

25

30

35

一月 二月 三月 四月 五月 六月

食物

天然氣

旅館

0

5

10

15

20

25

30

35

一月 二月 三月 四月 五月 六月

食物

天然氣

旅館

This is a Word document which includes some objects from ProwerPoint

and Excel

Spring $1,000,000.00 19.14%Summer $25,000.00 0.48%Fall $1,200,000.00 22.97%Winter $3,000,000.00 57.42%Total $5,225,000.00 100.00%

This is a Word document which includes some objects from ProwerPoint

and Excel

Spring $1,000,000.00 19.14%Summer $25,000.00 0.48%Fall $1,200,000.00 22.97%Winter $3,000,000.00 57.42%Total $5,225,000.00 100.00%

2 2( 2) ( 5)49

25 36

x y

2 2( 2) ( 5)49

25 36

x y

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.

Page 7: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

In-Place Editing

Page 8: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

ActiveX Controls

Page 9: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Exercises

1. What are the differences between object linking and object embedding? You also give an example using Microsoft Office.

2. What is OLE automation?

3. Design a web page making use an ActiveX control to display multimedia content.

4. What are the differences between Java applets and ActiveX controls.

Page 10: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

OLEObject Linking & Embedding

OLE Technologies

Page 11: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Overview

Object oriented programming (OOP) – effective in developing reusable software components.

How to build an object-oriented operating system?– Microsoft solution OLE.

OLE’s contribution– Facilitates and enables the integration of components.– Works independently of programming languages

through a binary standard.– Provides multiple interfaces to access an objects.

Page 12: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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 mac

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

Page 13: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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.

Page 14: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

OLE Defined

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.

OLE is no longer given a version number.

Page 15: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Clients and Severs

SeverSever

Object

Component

Object

ClientClientFunction calls to object interfaces

How to define Objects & Interfaces?

How to define Objects & Interfaces?

Page 16: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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.

Page 17: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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

Page 18: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Object Implementation

Any object can be seen as a set of – properties (data members or content); and– methods (member functions).

ObjectObject

Properties(data members, content)

Methods(member functions)

Page 19: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

OLE Objects & OLE Interfaces

The nature of an OLE object, then, is not expressed in how it is implemented internally but rather in how it is exposed externally.

ObjectImplementation

of interface functions

ObjectImplementation

of interface functions

Interface function table,or vtable

lpVtbl

Privateobjectdata

Pointer to Function1Pointer to Function2Pointer to Function3

. . .

Interfacepointer

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.

Page 20: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

OLE Objects & OLE Interfaces

The nature of an OLE object, then, is not expressed in how it is implemented internally but rather in how it is exposed externally.

ObjectImplementation

of interface functions

ObjectImplementation

of interface functions

Interface function table,or vtable

lpVtbl

Privateobjectdata

Pointer to Function1Pointer to Function2Pointer to Function3

. . .

Interfacepointer

Can be implemented using

any languages

Can be implemented using

any languagesA client access the object only through the interface

pointer.

A client access the object only through the interface

pointer.

A client accesses a specific feature of an object through a particular interface.

Page 21: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Simplified Notation on OLE Objects & OLE Interfaces

ObjectObject

Interface pointer

Interfaces are drawn as plug-in jacks extending from the object.

A client accesses a specific feature of an object through a particular interface.

Page 22: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Interface pointer

Multiple Interfaces

ObjectObject

Interface_1 pointer

Interfaces are drawn as plug-in jacks extending from the object.

An object may have multiple features meaning that it may provide multiple interfaces.

.

.

.

Interface_2 pointer

Interface_n pointer

A client accesses a specific feature of an object through a particular interface.

Page 23: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Multiple Interfaces

ObjectObject

Interface_1 pointer

Interfaces are drawn as plug-in jacks extending from the object.

An object may have multiple features meaning that it may provide multiple interfaces.

.

.

.

Interface_2 pointer

Interface_n pointer

A client accesses a specific feature of an object through a particular interface.

How to know what interfaces that a object provides?

How to know what interfaces that a object provides?

The answer is COM.The answer is COM.

Page 24: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

OLETechnologies

Page 25: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

OLETechnologies

Page 26: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

OLEObject Linking & Embedding

Component Object Model (COM)

Page 27: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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

Page 28: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

COM Objects and Interfaces

.

.

.

IUnknown

IOnOff

IAudioControl AV-DeviceObject

AV-DeviceObject

IDVDPlayControl

In convention, the names of COM interface start with character ‘I’.

Page 29: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

IUnknown The root of all evil

.

.

.

IUnknown

IOnOff

IAudioControl AV-DeviceObject

AV-DeviceObject

IDVDPlayControl

Page 30: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

IUnknown The root of all evil

.

.

.

IUnknown

IOnOff

IAudioControl AV-DeviceObject

AV-DeviceObject

IDVDPlayControl

interface IUnknown

{

virtual HRESULT QueryInterface(REFIID riid, void **ppvObject)=0;

virtual ULONG AddRef(void)=0;

virtual ULONG Release(void)=0;

};

interface IUnknown

{

virtual HRESULT QueryInterface(REFIID riid, void **ppvObject)=0;

virtual ULONG AddRef(void)=0;

virtual ULONG Release(void)=0;

};

IUnknown is the base interface of every custom COM interface. IUnknown is the base interface of every custom COM interface.

Page 31: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

IUnknown The root of all evil

.

.

.

IUnknown

IOnOff

IAudioControl AV-DeviceObject

AV-DeviceObject

IDVDPlayControl

interface IUnknown

{

virtual HRESULT QueryInterface(REFIID riid, void **ppvObject)=0;

virtual ULONG AddRef(void)=0;

virtual ULONG Release(void)=0;

};

interface IUnknown

{

virtual HRESULT QueryInterface(REFIID riid, void **ppvObject)=0;

virtual ULONG AddRef(void)=0;

virtual ULONG Release(void)=0;

};

IUnknown is the base interface of every custom COM interface. IUnknown is the base interface of every custom COM interface.

// Example

interface IOnOff : IUnknown

{

virtual HRESULT On()=0;

virtual HRESULT Off()=0;

};

// Example

interface IOnOff : IUnknown

{

virtual HRESULT On()=0;

virtual HRESULT Off()=0;

};

Page 32: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

QueryInterfaceQueryInterface

AddRefAddRef

ReleaseRelease

Function table for IUnknown

Function table for another interface

QueryInterfaceQueryInterface

AddRefAddRef

ReleaseRelease

Pointer to fn1Pointer to fn1

Pointer to fn2Pointer to fn2

Pointer to fn3Pointer to fn3

Pointer to fn4Pointer to fn4

A pointer to this interface can also be used as a pointer to IUnknown

Function Tables for COM Interfaces

Page 33: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

OLEObject Linking & Embedding

Object IDs

and

Interface IDs

Page 34: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

IUnknown

interface IUnknown

{

virtual HRESULT QueryInterface(REFIID riid, void **ppvObject)=0;

virtual ULONG AddRef(void)=0;

virtual ULONG Release(void)=0;

};

interface IUnknown

{

virtual HRESULT QueryInterface(REFIID riid, void **ppvObject)=0;

virtual ULONG AddRef(void)=0;

virtual ULONG Release(void)=0;

};

.

.

.

IUnknown

IOnOff

IAudioControlAV-Device

Object

AV-DeviceObject

IDVDPlayControl

In COM, every interface is also an IUnknown interface.

IUnknown

Page 35: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

IUnknown::QueryInterface

interface IUnknown

{

virtual HRESULT QueryInterface(REFIID riid, void **ppvObject)=0;

virtual ULONG AddRef(void)=0;

virtual ULONG Release(void)=0;

};

interface IUnknown

{

virtual HRESULT QueryInterface(REFIID riid, void **ppvObject)=0;

virtual ULONG AddRef(void)=0;

virtual ULONG Release(void)=0;

};

.

.

.

IUnknown

IOnOff

IAudioControlAV-Device

Object

AV-DeviceObject

IDVDPlayControl

In COM, every interface is also an IUnknown interface.

Query an interface by giving the reference of the interface ID.

Query an interface by giving the reference of the interface ID.

Who assign the ID?Who assign the ID?

Page 36: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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}

Page 37: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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 Data4[8];} GUID, *PGUID;

typedef struct _GUID{ ULONG Data1; WORD Data2; WORD Data3; UCHAR Data4[8];} GUID, *PGUID;

Page 38: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

GUID Generator (GUDIGEN.EXE)

UUID Generation

Page 39: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

GUID Generator (GUDIGEN.EXE)

// {920EA478-7DEA-4176-861E-823418B23191}DEFINE_GUID(<<name>>, 0x920ea478, 0x7dea, 0x4176, 0x86, 0x1e, 0x82, 0x34, 0x18, 0xb2, 0x31, 0x91);

Page 40: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Class IDs (CLSID) and Interface IDs (IID)

// {920EA478-7DEA-4176-861E-823418B23191}DEFINE_GUID(<<name>>, 0x920ea478, 0x7dea, 0x4176, 0x86, 0x1e, 0x82, 0x34, 0x18, 0xb2, 0x31, 0x91);

<<name>>

CLSID_Xxxor

IID_Xxx

CLSID_Xxxor

IID_Xxx

Page 41: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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-8FD0-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);

Page 42: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

COM API Functions for GUIDs

Function PurposeCoCreateGuid Allocates a new GUIDIsEqualGUID Compares two GUIDs for equivalenceIsEqualCLSID Typesafe version of IsEqualGUID for CLSIDsIsEqualIID Typesafe version of IsEqualGUID for IIDsStringFromCLSID Typesafe conversion of a CLSID to a text stringStringFromIID Typesafe version of StringFromCLSID for IIDsStringFromGUID2 Converts a GUID to a text string, storing the string in a

caller-allocated bufferCLSIDFromString Converts a text string to a typesafe CLSIDIIDFromString Typesafe version of CLSIDFromString for IIDs

Page 43: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

#include <initguid.h>

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.

Page 44: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

OLEObject Linking & Embedding

System Registry

Page 45: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

RegEdit.EXE or RegEdt32.EXE

OLE information is installed

Page 46: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

CLSID Registration

OLE information is installed

all the information about component classes is installed

Page 47: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

all the information about component classes is installed

Object IDs

Page 48: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Example:Microsoft Word Document

Page 49: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Example:Microsoft Word Document

Convert to new version

Page 50: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Example:Microsoft Word Document

Convert to new version

Page 51: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

ProgID Programmatic Identifier

Page 52: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

ProgID Programmatic Identifier

Page 53: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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<Vendor>.<Component>.<Version>e.g., Microsoft.Chart.5 or Lotus.AmiProDocument.4.2

Page 54: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Functions for CLSID & ProgID

Function Purpose ProgIDFromCLSID Returns the ProgID associated with a given CLSID CLSIDFromProgID Returns the CLSID associated with a given ProgID

Page 55: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Creation of Registry Entries

REGEDIT

HKEY_CLASSES_ROOT\Tatung.Component.3 = Tatung Component Version 3.0

HKEY_CLASSES_ROOT\Tatung.Component.3\CLSID = {B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}

HKEY_CLASSES_ROOT\Tatung.Component = Tatung Component

HKEY_CLASSES_ROOT\Tatung.Component\CurVer = Tatung.Component.3

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC} = Tatung Component 3.0

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}\ProgID = Tatung.Component.3

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}\VersionIndependentProgID = Tatung.Component

REGEDIT

HKEY_CLASSES_ROOT\Tatung.Component.3 = Tatung Component Version 3.0

HKEY_CLASSES_ROOT\Tatung.Component.3\CLSID = {B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}

HKEY_CLASSES_ROOT\Tatung.Component = Tatung Component

HKEY_CLASSES_ROOT\Tatung.Component\CurVer = Tatung.Component.3

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC} = Tatung Component 3.0

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}\ProgID = Tatung.Component.3

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}\VersionIndependentProgID = Tatung.Component

TatungComponent.reg

Page 56: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Creation of Registry Entries

REGEDIT

HKEY_CLASSES_ROOT\Tatung.Component.3 = Tatung Component Version 3.0

HKEY_CLASSES_ROOT\Tatung.Component.3\CLSID = {B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}

HKEY_CLASSES_ROOT\Tatung.Component = Tatung Component

HKEY_CLASSES_ROOT\Tatung.Component\CurVer = Tatung.Component.3

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC} = Tatung Component 3.0

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}\ProgID = Tatung.Component.3

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}\VersionIndependentProgID = Tatung.Component

REGEDIT

HKEY_CLASSES_ROOT\Tatung.Component.3 = Tatung Component Version 3.0

HKEY_CLASSES_ROOT\Tatung.Component.3\CLSID = {B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}

HKEY_CLASSES_ROOT\Tatung.Component = Tatung Component

HKEY_CLASSES_ROOT\Tatung.Component\CurVer = Tatung.Component.3

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC} = Tatung Component 3.0

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}\ProgID = Tatung.Component.3

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}\VersionIndependentProgID = Tatung.Component

TatungComponent.reg

Page 57: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Creation of Registry Entries

REGEDIT

HKEY_CLASSES_ROOT\Tatung.Component.3 = Tatung Component Version 3.0

HKEY_CLASSES_ROOT\Tatung.Component.3\CLSID = {B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}

HKEY_CLASSES_ROOT\Tatung.Component = Tatung Component

HKEY_CLASSES_ROOT\Tatung.Component\CurVer = Tatung.Component.3

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC} = Tatung Component 3.0

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}\ProgID = Tatung.Component.3

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}\VersionIndependentProgID = Tatung.Component

REGEDIT

HKEY_CLASSES_ROOT\Tatung.Component.3 = Tatung Component Version 3.0

HKEY_CLASSES_ROOT\Tatung.Component.3\CLSID = {B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}

HKEY_CLASSES_ROOT\Tatung.Component = Tatung Component

HKEY_CLASSES_ROOT\Tatung.Component\CurVer = Tatung.Component.3

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC} = Tatung Component 3.0

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}\ProgID = Tatung.Component.3

HKEY_CLASSES_ROOT\CLSID\{B02AF1A5-1F64-49b0-8D7A-0EDE811630DC}\VersionIndependentProgID = Tatung.Component

TatungComponent.reg

Page 58: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Registry Functions

Windows

Window Mobile

Page 59: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

OLEObject Linking & Embedding

Object Interfaces

Page 60: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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.

.

.

.

IUnknown

IOnOff

IAudioControlAV-Device

Object

AV-DeviceObject

IDVDPlayControl

.

.

.

IUnknown

IOnOff

IAudioControlAV-Device

Object

AV-DeviceObject

IDVDPlayControl

IUnknown

ObjectI mplementation

of interf ace functions

ObjectI mplementation

of interf ace functions

I nterface function table,or vtable

lpVtbl

Privateobjectdata

lpVtbl

Privateobjectdata

Pointer to Function1Pointer to Function2Pointer to Function3

. . .

Pointer to Function1Pointer to Function2Pointer to Function3

. . .

Pointer to Function1Pointer to Function2Pointer to Function3

. . .

I nterfacepointer

I nterfacepointer

Page 61: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Interfaces

An object is an entity that can be experienced or used only through its interfaces.– Interfaces are nothing more than semantically related gr

oups 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_<Interface>, e.g., IID_IConnectionPoint or IID_IUnknown

.

.

.

IUnknown

IOnOff

IAudioControlAV-Device

Object

AV-DeviceObject

IDVDPlayControl

.

.

.

IUnknown

IOnOff

IAudioControlAV-Device

Object

AV-DeviceObject

IDVDPlayControl

IUnknown

Page 62: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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 */

//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 */

Page 63: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Interface Description Language

// IPlayDvd.idl[uuid(0112114b-1111-2233-3f79-000000001146), 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);}

// IPlayDvd.idl[uuid(0112114b-1111-2233-3f79-000000001146), 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);}

Page 64: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Interface Description Language

// IPlayDvd.idl[uuid(0112114b-1111-2233-3f79-000000001146), 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);}

// IPlayDvd.idl[uuid(0112114b-1111-2233-3f79-000000001146), 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);}

Page 65: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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

Page 66: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

MIDL.EXE (Microsoft IDL Compiler)

Page 67: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

MIDL.EXE (Microsoft IDL Compiler)

Page 68: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Macros

#include <objbase.h>

#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-C000-000000000046}

DEFINE_GUID(IID_IUnknown,

0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);

typedef IUnknown * LPUNKNOWN;

#include <objbase.h>

#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-C000-000000000046}

DEFINE_GUID(IID_IUnknown,

0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);

typedef IUnknown * LPUNKNOWN;

IUnknown.h

Page 69: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Macros

#include <objbase.h>

#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-C000-000000000046}

DEFINE_GUID(IID_IUnknown,

0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);

typedef IUnknown * LPUNKNOWN;

#include <objbase.h>

#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-C000-000000000046}

DEFINE_GUID(IID_IUnknown,

0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);

typedef IUnknown * LPUNKNOWN;

IUnknown.h

For functions returning HRESULT

For functions returning HRESULT

For functions not returning HRESULT

For functions not returning HRESULT

Page 70: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Macros#include <objbase.h>

#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;

#include <objbase.h>

#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;

Page 71: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

HRESULT

Facility Code31 30 16 015

Severity0 Success1 Error

Indicates which group of status codes this belongs

to.Help to determine when or where the error occurred.

Describes what actually took place, error or

otherwise.

Page 72: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

HRESULTFacility Code

31 30 16 015

Severity

Facility Code31 30 16 015

Facility Code31 30 16 015

Severity0 Success1 Error

I ndicates which group of status codes this belongs to.Help to determine when or where the error occurred.

Describes what actually took place, error or otherwise.

Commonly used HRESULT/SCODE

S_OK Function succeeded. Also used for functions that semanticallyreturn 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 QueryInterface 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.

Page 73: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

HRESULTFacility Code

31 30 16 015

Severity

Facility Code31 30 16 015

Facility Code31 30 16 015

Severity0 Success1 Error

I ndicates which group of status codes this belongs to.Help to determine when or where the error occurred.

Describes what actually took place, error or otherwise.

Using macros SUCCEEDED and FAILED to determine whether a function invocation succeeded or failed.

HRESULT hr;IAnInterface * pInterface = NULL;. . . . . . . . . . . . .// get pInterface using some methodhr = pInterface->memberFun1(...);if(SUCCEEDED(hr)) ............

hr = pInterface->memberFun2(...);if(FAILED(hr)) ............

HRESULT hr;IAnInterface * pInterface = NULL;. . . . . . . . . . . . .// get pInterface using some methodhr = pInterface->memberFun1(...);if(SUCCEEDED(hr)) ............

hr = pInterface->memberFun2(...);if(FAILED(hr)) ............

Page 74: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Error Lookup (ErrLook.exe)

Page 75: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Error Lookup (ErrLook.exe)

Page 76: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Exercise

1. Using ErrLook.exe to lookup some errors defined in WinError.h

Page 77: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Review IUnknown

interface IUnknown

{

virtual HRESULT QueryInterface(REFIID riid, void **ppvObject)=0;

virtual ULONG AddRef(void)=0;

virtual ULONG Release(void)=0;

};

interface IUnknown

{

virtual HRESULT QueryInterface(REFIID riid, void **ppvObject)=0;

virtual ULONG AddRef(void)=0;

virtual ULONG Release(void)=0;

};

Page 78: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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.,– IStorage::OpenStream returns IStream *

Implement on an object of your own an interface through which other objects will pass their own interface pointers, e.g.,– IConnectionPoint::Advise

Call an API function that given a class identifier creates an object and returns any type of interface pointer you request, e.g.,– CoCreateInstance

Page 79: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

ReferenceCounting LPSOMEINTERFACE pISome1 = NULL; //Some1 object

LPSOMEINTERFACE pISome2 = NULL; //Some2 objectLPSOMEINTERFACE pCopy = NULL;

//A function that creates the pointer calls AddRef.CreateISomeObject(&pISome1); //Some1 ref count=1CreateISomeObject(&pISome2); //Some2 ref count=1

pCopy=pISome1; //Some1 count=1pCopy->AddRef(); //AddRef new copy, Some1=2

[Do things.]

pCopy->Release(); //Release before overwrite, Some1=1pCopy=pISome2; //Some2=1pCopy->AddRef(); //Some2=2

[What kinds of things do you do?]

pCopy->Release(); //Release before overwrite, Some2=1pCopy=NULL;

[Things that make us go.]

pISome2->Release(); //Release when done, Some2=0, Some2 freedpISome2=NULL;pISome1->Release(); //Release when done, Some1=0, Some1 freedpISome1=NULL;

LPSOMEINTERFACE pISome1 = NULL; //Some1 objectLPSOMEINTERFACE pISome2 = NULL; //Some2 objectLPSOMEINTERFACE pCopy = NULL;

//A function that creates the pointer calls AddRef.CreateISomeObject(&pISome1); //Some1 ref count=1CreateISomeObject(&pISome2); //Some2 ref count=1

pCopy=pISome1; //Some1 count=1pCopy->AddRef(); //AddRef new copy, Some1=2

[Do things.]

pCopy->Release(); //Release before overwrite, Some1=1pCopy=pISome2; //Some2=1pCopy->AddRef(); //Some2=2

[What kinds of things do you do?]

pCopy->Release(); //Release before overwrite, Some2=1pCopy=NULL;

[Things that make us go.]

pISome2->Release(); //Release when done, Some2=0, Some2 freedpISome2=NULL;pISome1->Release(); //Release when done, Some1=0, Some1 freedpISome1=NULL;

Page 80: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Interface Attributes

Interfaces are not classes

Interfaces are not objects

Interfaces are strongly typed

Interfaces are immutable

Page 81: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

OLEObject Linking & Embedding

Object Polymorphism

and Reusability

Page 82: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Polymorphism

The capability to treat objects from multiple classes identically because they all share one or more interfaces in commonExisting 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.

Page 83: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Polymorphism by C++ Style

interface IAnimal : IUnknown{ HRESULT Eat(...); HRESULT Sleep(...); HRESULT Procreate(...);}

interface IAnimal : IUnknown{ HRESULT Eat(...); HRESULT Sleep(...); HRESULT Procreate(...);}

interface IRabbit : IAnimal{ HRESULT RaidGardens(...); HRESULT Hop(...); HRESULT DigWarrens(...);}

interface IRabbit : IAnimal{ HRESULT RaidGardens(...); HRESULT Hop(...); HRESULT DigWarrens(...);}

interface IKoala : IAnimal{ HRESULT ClimbEucalyptusTrees(...); HRESULT PouchOpensDown(...); HRESULT SleepForHoursAfterEating(...);}

interface IKoala : IAnimal{ HRESULT ClimbEucalyptusTrees(...); HRESULT PouchOpensDown(...); HRESULT SleepForHoursAfterEating(...);}

How about if IAnimal need to be changed?How about if IAnimal need to be changed?

Inheritance

Page 84: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Polymorphism by COM

interface IAnimal : IUnknown{ HRESULT Eat(...); HRESULT Sleep(...); HRESULT Procreate(...);}

interface IAnimal : IUnknown{ HRESULT Eat(...); HRESULT Sleep(...); HRESULT Procreate(...);}

interface IRabbit : IUnknown{ HRESULT RaidGardens(...); HRESULT Hop(...); HRESULT DigWarrens(...);}

interface IRabbit : IUnknown{ HRESULT RaidGardens(...); HRESULT Hop(...); HRESULT DigWarrens(...);}

interface IKoala : IUnknown{ HRESULT ClimbEucalyptusTrees(...); HRESULT PouchOpensDown(...); HRESULT SleepForHoursAfterEating(...);}

interface IKoala : IUnknown{ HRESULT ClimbEucalyptusTrees(...); HRESULT PouchOpensDown(...); HRESULT SleepForHoursAfterEating(...);}

How about if IAnimal need to be changed?How about if IAnimal need to be changed?

Page 85: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Polymorphism by COM

How about if IAnimal need to be changed?How about if IAnimal need to be changed?

RabbitObject

RabbitObject

IAnimal

IRabbit

KoalaObject

KoalaObject

IAnimal

IKoala

IAnimal2 IAnimal2

Page 86: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Reusability

AnimalAnimalIAnimal

IUnknown

A ready to used object

KoalaKoala

IUnknown

IAnimal

IKoala

An object to be built

Page 87: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Reusability by COM

Containment

Aggregation

Page 88: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Containment

Koala(outer object)

Koala(outer object)

IUnknown

IAnimal

IKoala

IUnknown knowsIKoala and IAnimal

Animal(Inner Object)

Animal(Inner Object)

IAnimal

IUnknownIUnknown knowsonly IAnimal

Koala uses Animal’s implementation of

IAnimal, as would any client.

Page 89: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Aggregation

Koala(outer object)

Koala(outer object)

IUnknown

IAnimal

IKoala

Animal(Inner Object)

Animal(Inner Object)

IUnknown

Direct exposure

Page 90: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Aggregation

Koala(outer object)

Koala(outer object)

IUnknown

IAnimal

IKoala

Animal(Inner Object)

Animal(Inner Object)

IUnknown

IUnknown knowsIKoala and IAnimal

IUnknown knows only IAnimal

Koala holds Animal’s IUnknown pointer to control Animal’s lifetime.

Animal delegates IAnimal’s IUnknown calls to the outer IUnknown

Direct exposure

Page 91: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

OLEObject Linking & Embedding

Using COM Objects

Page 92: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

COM/OLE Library Initialization & Uninitialization

Initialization Uninitialization

CoInitialize[Ex]

or

OleInitialize

CoUninitialize

or

OleUninitialize

Page 93: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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.

Page 94: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

The Allocator Object

AllocatorObject

AllocatorObject

IMalloc

IUnknowninterface 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);

};

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

};

Page 95: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Get the Allocator Object

AllocatorObject

AllocatorObject

IMalloc

IUnknownHRESULT CoGetMalloc( DWORD dwMemContext, //Indicates if memory is private or shared LPMALLOC * ppMalloc //Address of output variable that receives a // pointer to the memory allocator);

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

Page 96: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Example: Memory Management

Page 97: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Exercise

1. Complete the demonstrating program and enhance it.

Page 98: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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

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

Page 99: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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.

Page 100: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Example: Desktop Management

CLSID of Active Desktop coclassIID of IActiveDesktop

// {75048700-EF1F-11D0-9888-006097DEACF9}DEFINE_GUID( 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);

// {75048700-EF1F-11D0-9888-006097DEACF9}DEFINE_GUID( 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);

Page 101: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Example: Desktop Management

// {75048700-EF1F-11D0-9888-006097DEACF9}DEFINE_GUID( 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);

// {75048700-EF1F-11D0-9888-006097DEACF9}DEFINE_GUID( 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);

CLSID of Active Desktop coclassIID of IActiveDesktop

Page 102: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Example: Desktop Management

// {75048700-EF1F-11D0-9888-006097DEACF9}DEFINE_GUID( 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);

// {75048700-EF1F-11D0-9888-006097DEACF9}DEFINE_GUID( 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);

CLSID of Active Desktop coclassIID of IActiveDesktop

Page 103: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Example: Desktop Management

Create a COM object fromthe Active Desktop coclass

HRESULT hr;IActiveDesktop* pIAD=NULL;// ....................................

// Create a COM object from the Active Desktop coclass.hr = CoCreateInstance( CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void**) &pIAD );

HRESULT hr;IActiveDesktop* pIAD=NULL;// ....................................

// Create a COM object from the Active Desktop coclass.hr = CoCreateInstance( CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void**) &pIAD );

Active DesktopObject

Active DesktopObjectIActiveDesktop

IUnknown

IActiveDesktop

Page 104: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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;};

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;};

Page 105: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Shell LinkObject

Shell LinkObjectIShellLink

IUnknown

IPersistFile

Example: Desktop Management

Create a COM object fromthe Shell Link coclass

HRESULT hr;IShellLink* pISL=NULL;// ....................................

// Create a COM object from the Shell Link coclass.hr = CoCreateInstance( CLSID_SHELLLINK, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**) &pISL );

HRESULT hr;IShellLink* pISL=NULL;// ....................................

// Create a COM object from the Shell Link coclass.hr = CoCreateInstance( CLSID_SHELLLINK, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**) &pISL );

IShellLink

Page 106: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

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)(THIS_ 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;};};

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)(THIS_ 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;};};

Page 107: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Example: Desktop Management

Interface IPersistFile

Page 108: OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry

Exercise

1. White an MFC application to set a particula

r image file as the desktop wallpaper.

2. Write an MFC application to simulate the ac

tive-desktop application provided by OS fo

r wallpaper selection.

3. Develop a desktop management program

by your creativeness.