TOP Visual C++ API Microsoft Win32API VARIANT HTML

Component Object Model

CComPtr vs _com_ptr_t

CComPtrATL class
_com_ptr_tan COM support classes to support non ATL COM development

header,lib

comdef.h
comsuppw.lib

COM support classes

_bstr_t
_com_error
_com_ptr_t
_variant_t

wrappers of basic COM functionality.
like COM smart pointer typedefs over raw COM inteface classes works just by using tlb file.

view sourceprint?
1 _COM_SMARTPTR_TYPEDEF(IMyInterface, __uuidof(IMyInterface));
This above line declares _com_ptr_t specialization IMyInterfacePtr.

The library to link to for using COM support classes is comsuppw.lib.

Interface

基本的なCOMのインタフェース:IUnknown

Basic

Intro

  • COM is : a method for sharing binary code across different applications and languages.
  • COM specifies that the binary modules (the DLLs and EXEs) must be compiled to match a specific structure.
  • The standard also specifies exactly how COM objects must be organized in memory.
  • The binaries must also not depend on any feature of any programming language (such as name decoration in C++).
  • Unicode

Element

interfaceMultiple inheritance is not allowed with interfaces
component object classcontained in a DLL or EXE
implement interfaces
a COM objectan instance of a component object class in memory
a COM servera binary (DLL or EXE)
Registrationthe process of creating registry entries
Unregistrationremoving the registry entries
a GUIDglobally unique identifier,a 128-bit number
a class ID
CLSID
a GUID that names a component object class
an interface ID
IID
a GUID that names an interface
HRESULTan integral type used by COM to return error and success codes

Create/Delete a object

CreateDelete
C++use operator new~use operator delete
create an object on the stacklet a stack object go out of scope
COMcall an API in the COM libraryall objects keep their own reference counts.
The caller must tell the object when the caller is done using the object.
COM objects free themselves from memory when the reference count reaches 0
HRESULT CoCreateInstance (
REFCLSID rclsid,
LPUNKNOWN pUnkOuter,
DWORD dwClsContext,
REFIID riid,
LPVOID* ppv );
pISL->Release();
params
paramcontentsmemo
rclsidCLSIDex) CLSID_ShellLink
pUnkOuterAggregating COM objectswhich is a way of taking an existing component object class and adding new methods to it
NULL to indicate not using aggregation
dwClsContextkind of COM serversex) CLSCTX_INPROC_SERVER
you should not use CLSCTX_ALL (which is the default in ATL) because it will fail on Windows 95 systems that do not have DCOM installed
riidIIDex)IID_IShellLink
ppvAddress of an interface pointer

IUnknown

methods:
AddRef()Tells the COM object to increment its reference countUse when to have a copy of an interface pointer
Release()Tells the COM object to decrement its reference count
QueryInterface()Requests an interface pointer from a COM objectuse when a component object class implements more than one interface
IUnknown::QueryInterface
HRESULT IUnknown::QueryInterface (
    REFIID iid,
    void** ppv );
iidIID
ppvAddress of an interface pointer

Step (

Nr.single REFIIDmultiple REFIID
1Initialize the COM libraryCoInitialize(NULL);
2Create a COM object A, and get an interface aCoCreate(...,&a);
3Call the method of the COM objecta->func()
4N/ACall QueryInterface() on the COM object B and get another interface ba->QueryInterface(...,&b);
5N/ACall the method of the COM objectb->func()
6Release the interface aa->Release()
7N/ARelease the interface bb->Release()
8Uninitialize the COM libraryCoUninitialize();

1.Initialize the COM library.
2.Create a COM object used to create shortcuts, and get an IShellLink interface.
3.Call the SetPath() method of the IShellLink interface.
4..
5.Call the Save() method of the IPersistFile interface.
6.Release the interfaces.
7.Uninitialize the COM library.

Error Messages

c:\Program Files\Microsoft SDKs\Windows\v6.0\Include\WinError.h

HRESULT

severity bitindicate success or failure
the facility codesource component/program of HREULT16-bits
|the status code||.
HRESULTFacilitySeverityStatus
REGDB_E_READREGDBREGDB(registry database)E(error}READREGDB (a description of the error}
S_OK(none)(generic)S (success)OK(description of status)

ex

HRESULTcases
0x800401F0CO_E_NOTINITIALIZEDforgot to call CoInitialize() before CoCreateInstance()

util

USES_CONVERSION

ATL と MFC の文字列変換マクロ
USES_CONVERSIONマクロは、ATL(Active Template Library)で提供される文字コード変換用マクロを利用する前に必ず宣言しなければならなかったが、
ユニコード統一で、不要になった。

.NET

C++/CLIでCreateInstance呼んでいるところと
COM の初期化 不要
共通言語ランタイムは、モジュールが初期化されると自動的に COM を初期化します (自動的に初期化される場合、COM は MTA として初期化されます)。その結果、明示的に COM を初期化すると、COM が既に初期化されていることを示すリターン コードが生成されます。COM が CLR によって既にいずれかのスレッド モデルに初期化されている場合、別のスレッド モデルを使用して明示的に COM を初期化しようとすると、アプリケーションが失敗するおそれがあります。

管理人/副管理人のみ編集できます