COM using C for Beginners Ranjit R. Sawant Developer Support Engineer Office Integration Microsoft C - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

COM using C for Beginners Ranjit R. Sawant Developer Support Engineer Office Integration Microsoft C

Description:

COM using C for Beginners. Ranjit R. Sawant. Developer Support Engineer. Office Integration ... Server activation and component creation. COM servers in EXE ... – PowerPoint PPT presentation

Number of Views:148
Avg rating:3.0/5.0
Slides: 37
Provided by: supportM
Category:

less

Transcript and Presenter's Notes

Title: COM using C for Beginners Ranjit R. Sawant Developer Support Engineer Office Integration Microsoft C


1
COM using C for BeginnersRanjit R.
SawantDeveloper Support EngineerOffice
Integration Microsoft Corporation
2
Agenda
  • Component architecture
  • Component Object ModelCOM
  • Interfaces
  • IUnknown
  • Component life time
  • Components and servers
  • HRESULTS and GUIDS

3
Agenda (continued)
  • COM registry entries
  • COM library functions
  • Class factories
  • Server activation and component creation
  • COM servers in EXE
  • Interface description language (IDL)
  • MIDL
  • Type libraries

4
Component Architecture
Component Application
Monolithic Application
Component C
Component A
Component D
Component B
Component Application
Component C
Component A
New Improved ,Component D
Component B
5
Component Architecture (continued)
  • Component benefits
  • Application evolves over time
  • Application customization
  • Component libraries
  • Distributed components
  • Component Requirements
  • Dynamic linking
  • Encapsulation

6
Component Architecture (continued)
  • Encapsulation leads to following constraints
  • Components should have language independence
  • Components should be in binary form
  • Components must be upgradeable
  • Components must be transparently relocateable on
    the network

7
Component Object Model
  • Component object model (COM) is a specification
    for a way of creating components and building
    applications from these components.
  • COM provides a library of standard functions.

8
Component Object Model (continued)
  • COM components meet all the requirements of
    component architecture
  • COM components link dynamically.
  • COM components can be encapsulated
  • Language independent
  • Binary form
  • Upgradeable
  • Relocateable

9
Interfaces
  • An interface provides a connection between two
    different objects.
  • Implementing a COM interface in Visual C

// define interface struct interface IX
virtual void __stdcall Fx1()0 virtual void
__stdcall Fx2()0
Class IX public virtual void Fx1() 0
virtual void Fx2() 0
10
Interfaces (continued)
  • Interface Features
  • Interfaces are immutable
  • Interfaces provide polymorphism
  • Memory layout of an interface

interface IX
IX
pIX virtual
void __stdcall Fx1() 0 virtual void
__stdcall Fx2() 0 virtual void __stdcall
Fx3() 0 virtual void __stdcall Fx4() 0

Virtual
Function Table
vtbl pointer
Fx1
Fx2
Fx3
Fx4
11
Interfaces (continued)
  • Instance Data for the class and Vtbl

Client IX
Class pIX
Virtual Function
Table
vtbl pointer
Fx1
Fx1 Fx2 Fx3 Fx4
Fx2
m_data1
Fx3
m_data2
Fx4
12
IUnknown
  • Definition of IUnknown

interface IUnknown virtual HRESULT __stdcall
QueryInterface(const IID iid, void ppv) 0
virtual ULONG __stdcall AddRef() 0 virtual
ULONG __stdcall Release() 0
13
IUnknown (continued)
  • Every COM Interface derives from Iunknown.

interface IXIUnknown virtual HRESULT
__stdcall Fx() 0 Memory layout of IX
Client IX
Class pIX
Virtual Function
Table
vtbl pointer
QueryInterface
QueryInterface AddRef Release
Fx
AddRef
Release
Fx
14
QueryInterface
  • QueryInterface definition
  • Client using QueryInterface

HRESULT __stdcall QueryInterface(const IID iid ,
void ppv)
void CallFx(IUnknown pI) //Define
a pointer to the interface that you need
IX pIX NULL   HRESULT hr
pI-gtQueryInterface(IID_IX, (void)pIX)
if (SUCCEEDED(hr)) //Check the return
value pIX-gtFx()
//Use Interface to call method Fx on it
 
15
QueryInterface (continued)
  • Component implementing QueryInterface

Class CApublic IX , public IY. //class
implements IX and IY interfaces   HRESULT
__stdcall CAQueryInterface(const IID iid,
void ppv) if (iid IID_IUnknown)
ppv static_castltIXgt(this) //The
client wants the IUnknown interface
else if (iid IID_IX) ppv
static_cast ltIXgt(this) //The client wants the
IX interface else if (iid IID_IY)
ppv static_cast ltIYgt(this) //The
client wants the IY interface else
ppv NULL return
E_NOINTERFACE static_castltIUnknowngt(p
pv)-gtAddRef() //for reference counting
return S_OK  
16
QueryInterface (continued)
  • QueryInterface Rules
  • You always get the same IUnknown
  • You can get an Interface if You Got it Before
  • QueryInterface is Symmetric
  • QueryInterface is Transitive
  • QueryInterface is Reflexive

QI(IX)-gtIUnknown QI(IY)-gtUnknown
If QI(IX)-gtIY is true once, then QI(IX)-gtIY is
true always
If QI(IX)-gtIY is true, then QI(QI(IX)-gtIY)-gtIX
must be true as well
If QI(QI(IX)-gtIY)-gtIZ is True ,Then QI(IX)-gtIZ
must be true as well
QI(IX)-gtIX
17
Component Life Time
  • In COM, the component should take care of
    controlling its own life time
  • Reference counting
  • Implementing AddRef and Release

ULONG __stdcall AddRef() return m_cref
//m_cref is a private member of the
class. ULONG __stdcall Release() if (
--m_cRef 0) delete this
return 0 return m_cRef  
18
Rules of Reference Counting
  • Call AddRef before returning
  • Call Release when you are done
  • Call AddRef after assignment

HRESULT __stdcall CAQueryInterface(const IID
iid, void ppv) if (iid IID_IUnknown)
ppv static_castltIXgt(this) //The
client wants the IUnknown interface .
static_castltIUnknowngt(ppv)-gtAddRef()
return S_OK
HRESULT hr pIUnknown-gtQueryInterface(IID_IX,(voi
d)pIX) if (SUCCEEDED(hr)) pIX-gtFx()
//Use interface IX. pIX-gtRelease() //Done
with IX. Call Release on it.
IX pIX2 pIX //Make a copy of
pIX. pIX2-gtAddRef() //Increment the reference
count
19
Components and Servers
  • COM component is a unit of functionality that
    implements one or more interfaces.
  • COM object server is either an executable or a
    DLL that hosts and can create one or more COM
    components.

20
COM Object Server in a DLL
  • COM DLL server exports the following five
    functions
  • DllMain
  • DllCanUnloadNow
  • DllGetClassObject
  • DllRegisterServer
  • DllUnregisterServer

21
HRESULTS and GUIDS
  • HRESULT
  • 31 30
    16 15
    0
  • Globally Unique Identifier or GUID

FACILITY
RETURN CODE
typedef struct _GUID DWORD Data1
WORD Data2 WORD Data3 BYTE
Data48 GUID   Examples of GUID
16622323-AE18-11CF-A6AE-0080C7B27789 32bb8129,
0xb41A, 0x11CA,0xA6,0Xbb,0x0,0x80,

0xC7,0xB2,0xD6,0x82  
22
COM Registry Entries
  • COM components use registry to convey information
    about the location of their servers.
  • A COM DLL server is required to make these
    entries when the exported function
    DLLRegisterServer is called.
  • The COM library function CoGetClassObject takes
    in a Class ID of the component and then looks
    into the registry to find the file name of the
    server in which the component is hosted.

23
COM Registry Entries (continued)
My Computer
  • HKEY_CLASSES_ROOT

CLSID
16685BA0-8890-2343-A873-00340ACE09AC
My Adder Component
InprocServer32
C\Adder\MyAddercomponent.dll
ProgID
Adder.MyAdderComponent.1
Adder.MyAdderComponent.1
My Adder Component
CLSID
16685BA0-8890-2343-A873-00340ACE09AC
24
COM Registry Entries (continued)
  • Other COM registry entries
  • PROGID or Programmatic ID
  • AppID
  • Component Categories
  • Interface
  • TypeLib

25
COM Library Functions
  • The COM Library is implemented in Ole32.dll.
  • Some important COM Library functions
  • CoInitializeEx
  • CoUninitialize
  • CoGetClassObject
  • CoCreateInstance
  • This is a helper function implemented as
    follows

CoGetClassObject(rclsid,dwClsContext,NULL,
IID_IClassFactory, pCF) hresult
pCF-gtCreateInstance(pUnkOuter,riid,ppvObj)
pCF-gtRelease()
26
Class Factories
  • A class factory is a COM component that creates
    other components that correspond only to a
    single, specific CLSID.
  • The standard interface for creating components is
    IClassFactory or IClassFactory2.
  • Declaration of IClassFactory

interface IClassFactoryIUnknown HRESULT
__stdcall CreateInstance(IUnknown pUnknownOuter,
const IID iid,
void ppv)
HRESULT __stdcall LockServer(BOOL bLock)  
27
Server Activation and Component Creation
  • Client creating a component

COM LIBRARY
COM DLL
CLIENT
1Calls CoGetClassObject
2 Calls DLLGetClassObject

Calls CoGetClassObject pIClassFactory pIX


CoGetClassObject
DllGetClassObject
3 Creates Class Factory
4 Returns IClassFactory
5 Calls IClassFactoryCreateInstance
IClassFactory
7Returns IX
6Creates Component
8Calls IXFx
IX
28
Implementation of DLLGetClassObject
STDAPI DLLGetClassObject(const CLSID clsid,
const IID iid, void ppv) CFactory1
pFactory1 if (clsid CLSID_Comp1) // Is
the requested Component hosted by us? pFactory1
new Cfactory1 //create Class Factory
associated with CLSID_Comp1 if (pFactory1
NULL) return E_OUTOFMEMORY
HRESULT hr pFactory1-gtQueryInterface(iid,ppv)
//Get the requested interface
pFactory1-gtRelease() return hr else if
(clsid CLSID_Comp2) //create
Class Factory associated with CLSID_Comp2 else
//return error return CLASS_E_CLASSNOTAVAILABLE

29
Implementation for IClassFactoryCreateInstance
HRESULT __stdcall CFactory1CreateInstance(IUnkno
wn pUnknownOuter,

const IID iid, void ppv)
//Create component CA pA new CA if (pA
NULL) return E_OUTOFMEMORY //get the
requested interface HRESULT hr
pA-gtQueryInterface(iid,ppv)   //Release the
IUnknown pointer pA-gtRelease() return hr
30
COM Servers in EXE
  • Local Procedure call, marshalling, and Proxy/stub
    DLLs
  • Function call for out-of-proc components

EXE
EXE
CLIENT
COM COMPONENT
PROCESS BOUNDARY
STUB unmarshals parameters.
PROXY marshals parameters.
Local Procedure Call
31
Interface Description Language
  • Interface Description Language (IDL) is used to
    describe COM interfaces in a standard way.
  • IDL is used to create proxy/stub DLLs and type
    library.

32
Sample IDL File
//IX.IDL //Import other IDL definitions to be
used in this file import
unknwn.idl , iy.idl , iz.idl //Interface
IX //interface
header object , //defines a COM
interface uuid(32455554-ad34-1213-a55e-0023bcd3456
a), //IID helpstring(IX Interface), //help
string that appears in TypeLibrary pointer_default
(unique) //how to treat pointers if no attributes
is given interface IX IUnknown
//interface body HRESULT FxStringIn(in,string
wchar_t szIn) //in describes input
HRESULT FxStringOut(out,string wchar_t
szout) //out describes output HRESULT
GetIY(out IY pIMy) //IY described in iy.idl
HRESULT GetIZ(out IZ pIZ) //IX described
in iz.idl   //describe other interfaces .
       
33
MIDL
  • Microsoft IDL (MIDL) compiler is used to generate
    proxy and stub DLLs from IDL. MIDL also generates
    other helper files.
  • Command line midl IX.idl
  • Files generated by MIDL compiler

IX.H
IX_P.C
C Compiler and Linker
MIDL.EXE
IX.IDL
IX.DLL
IX_I.C
DLLDATA.C
makefile
IX.DEF
34
Type Libraries
  • Type library provides type information of
    components, interfaces, methods, properties,
    arguments, and structures.

 //sample.idl import unknwn.idl // Define
Interface IX . //Component and Type Library
description uuid(D3011AE1-B943-9800-A558-00238709
1234),version(1.0),helpstring(Type
library) library SampleLib //Component
class description uuid(0CA06289-AA10-9800-A558-
002387091234), helpstring(MyComponent
description) coclass MyComponent
default interface IX
35
Type Libraries (continued)
  • Distributing type libraries
  • Using type libraries
  • LoadRegTypeLib
  • LoadTypeLib
  • RegisterTypeLib
  • UnRegisterTypeLib
  • Importing type library definitions in C
  • import "filename" attributes

36
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com