COM组件IImagingFactory引申(一)

 

1.CoInitialize, CoInitialzeEx

      CoInitialize、CoInitializeEx都是windows的API,主要是告诉windows以什么方式为程序创建COM对象,原因是程序调用com库函数(除CoGetMalloc和内存分配函数)之前必须初始化com库。
      有哪些方式呢?单线程和多线程。
      CoInitialize指明以单线程方式创建。
      CoInitializeEx可以指定COINIT_MULTITHREADED以多线程方式创建。
      创建单线程方式的COM服务器时不用考虑串行化问题,多线程COM服务器就要考虑。
      在使用中,使用CoInitialize创建可使对象直接与线程连接,得到最高的性能。创建多线程对象可以直接接收所有线程的调用,不必像单线程那样需要消息排队,但却需要COM创建线程间汇集代理,这样访问效率不高。

注:新的应用程序应该调用CoInitializeEx而不是CoInitialize, 一般是在 Dll 中使用 COM 才会需要使用的。

2.CoInitializeEx编译通过的问题

在初始化Com套间的时候,调用CoInitializeEx(NULL, COINIT_MULTITHREADED); 可编译器总是提示编译:

 error C2065: 'CoInitializeEx' : undeclared identifier

error C2065: 'COINIT_MULTITHREADED' : undeclared identifier

最后,在stdafx.h文件中的增加红色代码就可以了。

......

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400           //使用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.例子

 

 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资源号
16  
17        if  (hRes == NULL)
18            return  FALSE;
19       HGLOBAL 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 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 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   }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值