AfxOleInit()/CoInitialize()/CoInitializeEx()

来源:互联网 发布:数值优化 编辑:程序博客网 时间:2024/06/06 07:30

具体可见msdn开发人员中心:

(一)AfxOleInit()

1、函数说明:Initializes OLE support for the application.

2、函数原型:

BOOL AFXAPI AfxOleInit( );

Nonzero if successful; 0 if initialization fails, possibly because incorrect versions of the OLE system DLLs are installed.

 

 Remarks

Call this function to initialize the OLE support for an MFC application. When this function is called, the following actions occur:

  • Initializes the COM library on the current apartment of the calling application. For more information, seeOleInitialize.

  • Creates a message filter object, implementing the IMessageFilter interface. This message filter can be accessed with a call to AfxOleGetMessageFilter.

3、注意:If AfxOleInit is called from an MFC DLL, the call will fail. The failure occurs because the function assumes that, if it is called from a DLL, the OLE system was previously initialized by the calling application。

(二)CoInitialize()

1、函数说明:Initializes the COM library on the current thread and identifies the concurrency model as single-thread apartment (STA).初始化当前线程中调用的COM组件,而且是单线程。

2、函数原型:

Syntax

C++
HRESULT CoInitialize(  __in_opt  LPVOID pvReserved);

Parameters

pvReserved [in, optional]

This parameter is reserved and must be NULL.

Return Value

This function can return the standard return values E_INVALIDARG, E_OUTOFMEMORY, and E_UNEXPECTED, as well as the following values.(异常结果)

Return codeDescription
S_OK

The COM library was initialized successfully on this thread.(成功初始化)

S_FALSE

The COM library is already initialized on this thread.(已初始化)

RPC_E_CHANGED_MODE

A previous call to CoInitializeEx specified the concurrency model for this thread as multithread apartment (MTA). This could also indicate that a change from neutral-threaded apartment to single-threaded apartment has occurred.

              (其他线程调用)

3、注意:New applications should call CoInitializeEx instead of CoInitialize.

 

 

(三)CoInitializeEx Function

1、函数说明:Initializes the COM library for use by the calling thread, sets the thread's concurrency model, and creates a new apartment for the thread if one is required.

2、函数原型:

Syntax

C++
HRESULT CoInitializeEx(  __in_opt  LPVOID pvReserved,  __in      DWORD dwCoInit);

Parameters

pvReserved [in, optional]

This parameter is reserved and must be NULL.

dwCoInit [in]

The concurrency model and initialization options for the thread. Values for this parameter are taken from theCOINIT enumeration. Any combination of values from COINIT can be used, except that the COINIT_APARTMENTTHREADED and COINIT_MULTITHREADED flags cannot both be set. The default is COINIT_MULTITHREADED.

Return Value

This function can return the standard return values E_INVALIDARG, E_OUTOFMEMORY, and E_UNEXPECTED, as well as the following values.

Return codeDescription
S_OK

The COM library was initialized successfully on this thread.

S_FALSE

The COM library is already initialized on this thread.

RPC_E_CHANGED_MODE

A previous call to CoInitializeEx specified the concurrency model for this thread as multithread apartment (MTA). This could also indicate that a change from neutral-threaded apartment to single-threaded apartment has occurred.

 

 

 
 
(四)OleInitialize
1、函数说明:Initializes the COM library on the current apartment, identifies the concurrency model as single-thread apartment (STA), and enables additional functionality described in the Remarks section below. Applications must initialize the COM library before they can call COM library functions other than CoGetMalloc and memory allocation functions.
2、函数原型:
 
(五)OleUnInitialize()和 CoUninitialize
1、函数说明:Closes the COM library on the apartment, releases any class factories, other COM objects, or servers held by the apartment, disables RPC on the apartment, and frees any resources the apartment maintains.
2、函数原型:

Syntax

C++
void OleUninitialize(void);

Parameters

This function has no parameters.

Return Value

This function does not return a value.

3、OleUninitialize calls the CoUninitialize function internally to shut down the OLE Component Object(COM) Library.

If the COM library was initialized on the apartment with a call to CoInitialize or CoInitializeEx, it must be closed with a call toCoUninitialize.

(四)其他:
2、用CoInitializeEx()要注意的是:MFC applications must be initialized as single threaded apartment (STA). If you callCoInitializeEx in your InitInstance override, specify COINIT_APARTMENTTHREADED (rather than COINIT_MULTITHREADED).
3、You need to initialize the COM library on an apartment before you call any of the library functions except CoGetMalloc, to get a pointer to the standard allocator, and the memory allocation functions.
 
 
 
 
 
 
 

Syntax

C++
HRESULT OleInitialize(  __in  LPVOID pvReserved);

Parameters

pvReserved [in]

This parameter is reserved and must be NULL.

Return Value

This function returns S_OK on success. Other possible values include the following.

Return codeDescription
S_FALSE

The COM library is already initialized on this apartment.

OLE_E_WRONGCOMPOBJ

The versions of COMPOBJ.DLL and OLE2.DLL on your machine are incompatible with each other.

RPC_E_CHANGED_MODE

A previous call to CoInitializeEx specified the concurrency model for this apartment as multithread apartment (MTA). This could also mean that a change from neutral threaded apartment to single threaded apartment occurred.

 

Remarks

Applications that use the following functionality must call OleInitialize before calling any other function in the COM library:

  • Clipboard
  • Drag and Drop
  • Object linking and embedding (OLE)
  • In-place activation

4、注意:

OleInitialize calls CoInitializeEx internally to initialize the COM library on the current apartment. Because OLE operations are not thread-safe, OleInitialize specifies the concurrency model as single-thread apartment.

0 0