1. coinitialize, coinitialzeex coinitialize and coinitializeex are all windows APIs. They mainly tell windows how to create COM objects for programs. The reason is that the program calls com library functions (except cogetmalloc and memory allocation functions) you must initialize the com library before.
What are the methods? Single-thread and multi-thread.
Coinitialize indicates that it is created in a single thread mode.
Coinitializeex can specify coinit_multithreaded to be created in multiple threads.
When creating a single-threaded COM server, you do not need to consider serialization.
In use, you can use coinitialize to create an object that can be directly connected to a thread to achieve the highest performance. Creating a multi-threaded object can directly receive calls from all threads, without queuing of messages as a single thread, but with the need for com to create a collection proxy between threads, the access efficiency is not high.
Note: New applications should call coinitializeex instead of coinitialize, which is generally required to use com in DLL. 2. coinitializeex Compilation
Call coinitializeex (null, coinit_multithreaded) When initializing the com suite. The Compiler always prompts Compilation:
Error c2065: 'coinitializeex ': Undeclared identifier
Error c2065: 'coinit _ multithreaded': Undeclared identifier
Finally, add the red code in the stdafx. h file.
......
# Ifndef _ win32_winnt
# DEFINE _ win32_winnt 0x0400 // use coinitializeex
# Endif
# Include <afxwin. h> // MFC core and standard components
# Include <afxext. h> // MFC extensions
# Include <afxcview. h>
# Include <afxdisp. h> // MFC automation classes
# Include <afxdtctl. h> // MFC support for Internet Explorer 4 common controls
# Ifndef _ afx_no_afxcmn_support
# Include <afxcmn. h> // MFC support for Windows Common controls
# Endif // _ afx_no_afxcmn_support
......
3. Example
1
# Include
<
Initguid. h
>
2
# Include
<
Imgguids. h
>
3
# Include
<
Imaging. h
>
4
Mywnd: mydraw ()
5
{
6
7
Iimagingfactory
*
Pimgfac;
8
Iimage
*
M_pimage;
9
Imageinfo m_pimageinfo;
10
If
(Failed (cocreateinstance (clsid_imagingfactory, null, clsctx_inproc_server, iid_iimagingfactory ,(
Void
**
)
&
Pimgfac )))
11
Return
True;
12
13
Hrsrc hres
=
Findresource (m_hwnd,
14
Makeintresource (idr_jpg_board ),
15
Rt_rcdata );
//
Idr_jpg_board resource no.
16
17
If
(Hres
=
Null)
18
Return
False;
19
Hglobal
=
Loadresource (m_hwnd, hres );
20
DWORD size
=
Sizeofresource (m_hwnd, hres );
21
Lpvoid pdata
=
Lockresource (hglobal );
22
23
If
(Pimgfac
=
Null
|
Pdata
=
Null
|
Size
=
0
)
24
Return
NULL;
25
If
(Succeeded (pimgfac
->
Createimagefrombuffer (pdata, size, bufferdisposalflagglobalfree,
&
M_pimage )))
26
{
27
M_pimage
->
Getimageinfo (
&
M_pimageinfo );
28
HDC
=
: Getdc (null );
29
Memhdc
=
: Createcompatibledc (HDC );
30
Himg
=
Createcompatiblebitmap (HDC, m_pimageinfo.width, m_pimageinfo.height );
31
SelectObject (memhdc, himg );
32
33
Rsweight
=
M_pimageinfo.width;
34
Rshight
=
M_pimageinfo.height;
35
Rect
=
{
0
,
0
, M_pimageinfo.width, m_pimageinfo.height };
36
M_pimage
->
Draw (memhdc,
&
Rect, null );
37
}
38
Else
39
{
40
Return
NULL;
41
}
42
Return
True;
43
44
}