具体可见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
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 code | Description |
---|---|
| The COM library was initialized successfully on this thread.(成功初始化) |
| The COM library is already initialized on this thread.(已初始化) |
| 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.
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
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 code | Description |
---|---|
| The COM library was initialized successfully on this thread. |
| The COM library is already initialized on this thread. |
| 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.
|
Syntax
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.
Syntax
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 code | Description |
---|---|
| The COM library is already initialized on this apartment. |
| The versions of COMPOBJ.DLL and OLE2.DLL on your machine are incompatible with each other. |
| 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.