Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "OLE Object Linking & Embedding 主講人:虞台文. Content Basic Concept OLE Technologies Component Object Model (COM) Object IDs and Interface IDs System Registry."— Presentation transcript:

1 OLE Object 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 OLE Object Linking & Embedding Basic Concept

4 Objects and Interfaces What functions these objects provide? 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.

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

10 OLE Object 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. 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.

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

15 Clients and Severs Sever Object Component Object Client Function calls to object interfaces 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) Methods (member functions)

19 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. Object Implementation of interface functions Object Implementation of interface functions Interface function table, or vtable lpVtbl Private object data Pointer to Function1 Pointer to Function2 Pointer to Function3... Interface pointer 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.

20 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. Object Implementation of interface functions Object Implementation of interface functions Interface function table, or vtable lpVtbl Private object data Pointer to Function1 Pointer to Function2 Pointer to Function3... Interface pointer Can be implemented using any languages A client access the object only through the interface pointer. A client accesses a specific feature of an object through a particular interface.

21 Simplified Notation on OLE Objects & OLE Interfaces Object 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.

22 Interface pointer Multiple Interfaces Object 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.

23 Multiple Interfaces Object 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? The answer is COM.

24 OLE Technologies

25

26 OLE Object 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...... IUnknown IOnOff IAudioControl AV-Device Object AV-Device Object IDVDPlayControl In convention, the names of COM interface start with character ‘ I ’.

29 IUnknown The root of all evil...... IUnknown IOnOff IAudioControl AV-Device Object AV-Device Object IDVDPlayControl

30 IUnknown The root of all evil...... IUnknown IOnOff IAudioControl AV-Device Object AV-Device Object 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.

31 IUnknown The root of all evil...... IUnknown IOnOff IAudioControl AV-Device Object AV-Device Object 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. // Example interface IOnOff : IUnknown { virtual HRESULT On()=0; virtual HRESULT Off()=0; }; // Example interface IOnOff : IUnknown { virtual HRESULT On()=0; virtual HRESULT Off()=0; };

32 QueryInterface AddRef Release Function table for IUnknown Function table for another interface QueryInterface AddRef Release Pointer to fn1 Pointer to fn2 Pointer to fn3 Pointer to fn4 A pointer to this interface can also be used as a pointer to IUnknown Function Tables for COM Interfaces

33 OLE Object 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; }; interface IUnknown { virtual HRESULT QueryInterface(REFIID riid, void **ppvObject)=0; virtual ULONG AddRef(void)=0; virtual ULONG Release(void)=0; };...... IUnknown IOnOff IAudioControl AV-Device Object AV-Device Object IDVDPlayControl In COM, every interface is also an IUnknown interface. IUnknown

35 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 IAudioControl AV-Device Object AV-Device Object IDVDPlayControl In COM, every interface is also an IUnknown interface. Query an interface by giving the reference of the interface ID. Who assign the ID?

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 Data4[8]; } GUID, *PGUID; typedef struct _GUID { ULONG Data1; WORD Data2; WORD Data3; UCHAR Data4[8]; } GUID, *PGUID;

38 GUID Generator (GUDIGEN.EXE) UUID Generation

39 GUID Generator (GUDIGEN.EXE) // {920EA478-7DEA-4176-861E-823418B23191} DEFINE_GUID( >, 0x920ea478, 0x7dea, 0x4176, 0x86, 0x1e, 0x82, 0x34, 0x18, 0xb2, 0x31, 0x91);

40 Class IDs (CLSID) and Interface IDs (IID) // {920EA478-7DEA-4176-861E-823418B23191} DEFINE_GUID( >, 0x920ea478, 0x7dea, 0x4176, 0x86, 0x1e, 0x82, 0x34, 0x18, 0xb2, 0x31, 0x91); <<name>> CLSID_Xxx or IID_Xxx CLSID_Xxx or IID_Xxx

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

42 COM API Functions for GUIDs Function Purpose CoCreateGuid Allocates a new GUID IsEqualGUID Compares two GUIDs for equivalence IsEqualCLSID Typesafe version of IsEqualGUID for CLSIDs IsEqualIID Typesafe version of IsEqualGUID for IIDs StringFromCLSID Typesafe conversion of a CLSID to a text string StringFromIID Typesafe version of StringFromCLSID for IIDs StringFromGUID2 Converts a GUID to a text string, storing the string in a caller-allocated buffer CLSIDFromString Converts a text string to a typesafe CLSID IIDFromString Typesafe version of CLSIDFromString for IIDs

43 #include 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 OLE Object 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

48 Example: Microsoft Word Document

49 Convert to new version

50 Example: Microsoft Word Document Convert to new version

51 ProgID Programmatic Identifier

52

53 A higher-level abstraction of CLSID – To allow for Binary Compatible objects and to make it easier to get at COM objects Standard format.. e.g., Microsoft.Chart.5 or Lotus.AmiProDocument.4.2

54 Functions for CLSID & ProgID Function Purpose ProgIDFromCLSID Returns the ProgID associated with a given CLSID CLSIDFromProgID Returns the CLSID associated with a given ProgID

55 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

56 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

57 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

58 Registry Functions Windows Window Mobile

59 OLE Object 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_, e.g., IID_IConnectionPoint or IID_IUnknown

62 Interface Definition //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 */

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

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

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

68 Macros #include #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 #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

69 Macros #include #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 #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 not returning HRESULT

70 Macros #include #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;

71 HRESULT FacilityCode 313016015 Severity 0 Success 1 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.

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

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->memberFun1(...); if(SUCCEEDED(hr))............ hr = pInterface->memberFun2(...); if(FAILED(hr))............ HRESULT hr; IAnInterface * pInterface = NULL;............. // get pInterface using some method hr = pInterface->memberFun1(...); if(SUCCEEDED(hr))............ hr = pInterface->memberFun2(...); if(FAILED(hr))............

74 Error Lookup ( ErrLook.exe)

75

76 Exercise 1. 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; }; interface IUnknown { virtual HRESULT QueryInterface(REFIID riid, void **ppvObject)=0; virtual ULONG AddRef(void)=0; virtual ULONG Release(void)=0; };

78 Methods to Get the 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

79 Reference Counting 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 count=1 CreateISomeObject(&pISome2); //Some2 ref count=1 pCopy=pISome1; //Some1 count=1 pCopy->AddRef(); //AddRef new copy, Some1=2 [Do things.] pCopy->Release(); //Release before overwrite, Some1=1 pCopy=pISome2; //Some2=1 pCopy->AddRef(); //Some2=2 [What kinds of things do you do?] pCopy->Release(); //Release before overwrite, Some2=1 pCopy=NULL; [Things that make us go.] pISome2->Release(); //Release when done, Some2=0, Some2 freed pISome2=NULL; pISome1->Release(); //Release when done, Some1=0, Some1 freed pISome1=NULL; 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 count=1 CreateISomeObject(&pISome2); //Some2 ref count=1 pCopy=pISome1; //Some1 count=1 pCopy->AddRef(); //AddRef new copy, Some1=2 [Do things.] pCopy->Release(); //Release before overwrite, Some1=1 pCopy=pISome2; //Some2=1 pCopy->AddRef(); //Some2=2 [What kinds of things do you do?] pCopy->Release(); //Release before overwrite, Some2=1 pCopy=NULL; [Things that make us go.] pISome2->Release(); //Release when done, Some2=0, Some2 freed pISome2=NULL; pISome1->Release(); //Release when done, Some1=0, Some1 freed pISome1=NULL;

80 Interface Attributes Interfaces are not classes Interfaces are not objects Interfaces are strongly typed Interfaces are immutable

81 OLE Object 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 interface IAnimal : IUnknown { HRESULT Eat(...); HRESULT Sleep(...); HRESULT Procreate(...); } interface IRabbit : IAnimal { HRESULT RaidGardens(...); HRESULT Hop(...); HRESULT DigWarrens(...); } interface IKoala : IAnimal { HRESULT ClimbEucalyptusTrees(...); HRESULT PouchOpensDown(...); HRESULT SleepForHoursAfterEating(...); } How about if IAnimal need to be changed? Inheritance

84 Polymorphism by COM interface IAnimal : IUnknown { HRESULT Eat(...); HRESULT Sleep(...); HRESULT Procreate(...); } interface IRabbit : IUnknown { HRESULT RaidGardens(...); HRESULT Hop(...); HRESULT DigWarrens(...); } interface IKoala : IUnknown { HRESULT ClimbEucalyptusTrees(...); HRESULT PouchOpensDown(...); HRESULT SleepForHoursAfterEating(...); } How about if IAnimal need to be changed?

85 Polymorphism by COM How about if IAnimal need to be changed? Rabbit Object Rabbit Object IAnimal IRabbit Koala Object Koala Object IAnimal IKoala IAnimal2

86 Reusability Animal IAnimal IUnknown A ready to used object Koala IUnknown IAnimal IKoala An object to be built

87 Reusability by COM Containment Aggregation

88 Containment Koala (outer object) Koala (outer object) IUnknown IAnimal IKoala IUnknown knows IKoala and IAnimal Animal (Inner Object) Animal (Inner Object) IAnimal IUnknown IUnknown knows only IAnimal Koala uses Animal’s implementation of IAnimal, as would any client.

89 Aggregation Koala (outer object) Koala (outer object) IUnknown IAnimal IKoala Animal (Inner Object) Animal (Inner Object) IUnknown Direct exposure

90 Aggregation Koala (outer object) Koala (outer object) IUnknown IAnimal IKoala Animal (Inner Object) Animal (Inner Object) IUnknown IUnknown knows IKoala 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

91 OLE Object Linking & Embedding Using COM Objects

92 COM/OLE Library Initialization & Uninitialization InitializationUninitialization CoInitialize[Ex] 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 Allocator Object Allocator Object IMalloc IUnknown 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); }; 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 Allocator Object Allocator Object IMalloc IUnknown 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 ); 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 1. 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 ); 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_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);

101 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 coclass IID of IActiveDesktop

102 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 coclass IID of IActiveDesktop

103 Example: Desktop Management Create a COM object from the 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 Desktop Object Active Desktop Object IActiveDesktop IUnknown 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; }; 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 Shell Link Object Shell Link Object IShellLink IUnknown IPersistFile Example: Desktop Management Create a COM object from the 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

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

107 Example: Desktop Management Interface IPersistFile

108 Exercise 1. White an MFC application to set a particular image file as the desktop wallpaper. 2. Write an MFC application to simulate the active-desktop application provided by OS for wallpaper selection. 3. Develop a desktop management program by your creativeness.


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

Similar presentations


Ads by Google