From: http://blog.csdn.net/zhoubl668/archive/2009/04/30/4139933.aspx

OLE是建立在COM之上的技术,层次比COM要高。AfxOleInit()调用的是OleInitialize(),而OleInitialize()除了调用CoInitializeEx()来初始化COM库外,还进行一些其它的操作,这些操作对OLE应用来说是必须的,这些OLE应用包括:   
  (1)Clipboard;   
  (2)Drag   and   drop;   
  (3)Object   linking   and   embedding(现在的OLE,已不再仅仅是Object   linking   and   embedding的概念);   
  (4)In-place   activation;   
  与AfxOleInit()对应的是,AfxOleTerm()。

CoInitialize和CoUninitialize必须成对使用,后者不必。

AfxOleInit()和AfxOleTerm()其实也是需要成对的,但是,在你的程序中,AfxOleTerm()可以不出现,这是因为,MFC已经帮你做好了(有兴趣的话,你可以仔细研究一下CWinThread::m_lpfnOleTermOrFreeLib,而CWinApp是从CWinThread继承的)。

////////////////////////////////////////

//以下是AfxOleInit的源码   
  --------------------------------------------------------------------------------   
  BOOL   AFXAPI   AfxOleInit()   
  {   
  _AFX_THREAD_STATE*   pState   =   AfxGetThreadState();   
  ASSERT(!pState->m_bNeedTerm);         //   calling   it   twice?   
    
  //   Special   case   DLL   context   to   assume   that   the   calling   app   initializes   OLE.   
  //   For   DLLs   where   this   is   not   the   case,   those   DLLs   will   need   to   initialize   
  //   OLE   for   themselves   via   OleInitialize.     This   is   done   since   MFC   cannot   provide   
  //   automatic   uninitialize   for   DLLs   because   it   is   not   valid   to   shutdown   OLE   
  //   during   a   DLL_PROCESS_DETACH.   
  if   (afxContextIsDLL)   
  {   
  pState->m_bNeedTerm   =   -1;     //   -1   is   a   special   flag   
  return   TRUE;   
  }   
    
  //   first,   initialize   OLE   
  SCODE   sc   =   ::OleInitialize(NULL);   
  if   (FAILED(sc))   
  {   
  //   warn   about   non-NULL   success   codes   
  TRACE1("Warning:   OleInitialize   returned   scode   =   %s.\n",   
  AfxGetFullScodeString(sc));   
  goto   InitFailed;   
  }   
  //   termination   required   when   OleInitialize   does   not   fail   
  pState->m_bNeedTerm   =   TRUE;   
    
  //   hook   idle   time   and   exit   time   for   required   OLE   cleanup   
  CWinThread*   pThread;   pThread   =   AfxGetThread();   
  pThread->m_lpfnOleTermOrFreeLib   =   AfxOleTermOrFreeLib;   
    
  //   allocate   and   initialize   default   message   filter   
  if   (pThread->m_pMessageFilter   ==   NULL)   
  {   
  pThread->m_pMessageFilter   =   new   COleMessageFilter;   
  ASSERT(AfxOleGetMessageFilter()   !=   NULL);   
  AfxOleGetMessageFilter()->Register();   
  }   
  return   TRUE;   
    
  InitFailed:   
  AfxOleTerm();   
  return   FALSE;   
  }   
    
  可见,AfxOleInit()主要是封装了OleInitialize(),而OleInitialize内部调用了ConInitialize   
  OleInitialize比ConInitialize多了以下支持:   
  Clipboard     
  Drag   and   drop     
  Object   linking   and   embedding   (OLE)     
  In-place   activation     
    
  如果你不需要这些附加功能,就用CoInitialize或CoInitializeEx.

///////////////////////////////////////////////////////////////////////////////////////////

多线程问题的

AfxOleInit实际上调用了OleInitialize,虽然它在内部也调用了CoInitializeEx,但它只能处理单线程,这是AfxOleInit和CoInitialize   主要区别:   
  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.     
    
  Once   the   concurrency   model   for   an   apartment   is   set,   it   cannot   be   changed.   A   call   to   OleInitialize   on   an   apartment   that   was   previously   initialized   as   multithreaded   will   fail   and   return   RPC_E_CHANGED_MODE.     
    
  所以,你最初调用AfxOleInit()失败,就是因为你的程序在多线程的状态

AfxOleInit()和::CoInitialize(NULL)区别的更多相关文章

  1. memset与NULL区别

    memset与NULL区别 NULL与0 的区别 为什么强调一个malloc对应一个free 在一个结构体malloc,然后free,但是她的成员变量的malloc并没有free,还需要特别的free ...

  2. Java进阶(二十一)java 空字符串与null区别

    java 空字符串与null区别 1.类型 null表示的是一个对象的值,而并不是一个字符串.例如声明一个对象的引用,String a = null ; ""表示的是一个空字符串, ...

  3. >/dev/null 2>&1和2>&1 >/dev/null区别

    >/dev/null 2>&1和2>&1 >/dev/null区别 >/dev/null 2>&1 //会将标准输出,错误输出都重定向至/d ...

  4. C#中string.Empty ,"" , null 区别

    引言 String类型作为使用最频繁的类型之一,相信大家都非常熟悉,对于string赋予空值,通常有以下三种方式: String str1=null; String str2=””; String s ...

  5. SQL中空值与NULL区别

    很多人都有过这样的问题吧   在SQL中填充空值与NULL有什么区别 现在我以一个实例给大家分享一下自己的想法  恳请大家给予批评也指正 谢谢 创建一个监时表 CREATE TABLE #temp ( ...

  6. javascript 中 undefined 和 null 区别

    1.相同点 如果我们直接用 undefined == null  比较他们是相等的返回的将是 true. 2.区别 当我们用undefined === null 比较的时候最后返回的将是 false. ...

  7. JavaScript-undefined与null区别

    JavaScript中的null在其他编程语言中也很常见,但是JavaScript在设计的过程中null自动转换为0,为了更好表示空,这个时候undefined出现了,null通过typeof结果是“ ...

  8. MySQL的空值和NULL区别

    从本质上区别: 1.空值不占空间 2.null值占空间   通俗的讲: 空值就像是一个真空转态杯子,什么都没有,而null值就是一个装满空气的杯子,虽然看起来都是一样的,但是有着本质的区别.     ...

  9. 来理解undefined 和 null 区别

    之前虽然也知道这两个之间的区别,但是让我描述的话,感觉上还是说的不是很清楚.今天也详细看了一次这个知识点,现在来说说这两者间的区别. null: Null类型,代表“空值”,代表一个空对象指针,使用t ...

随机推荐

  1. 转载:如何查看用户当前shell和修改用户登陆时的默认shell

    转载网址:http://www.51testing.com/html/44/211244-197042.html   1.查看当前使用的shell类型 # echo $SHELL/usr/bin/bs ...

  2. 4月13日 php

    用php打印乘法表: <html> <head> <title>我的第一个PHP页面</title> </head> <body> ...

  3. 网狐6603 cocos2dx 棋牌、捕鱼、休闲类游戏《李逵捕鱼》手机端完整源码分析及分享

    该资源说明: cocos2d 棋牌.捕鱼.休闲类游戏<李逵捕鱼>手机端完整源码,网狐6603配套手机版源码,可以选桌子,适合新手学习参考,小编已亲测试,绝对完整可编译手机端,下载后将文件考 ...

  4. 第四届CCF大数据学术会议征文通知

    第四届CCF大数据学术会议征文通知 2016年10月,兰州 近几年,大数据是各界高度关注积极布局的热点方向.2015年8月,国务院发表<促进大数据发展行动纲要>,正式将大数据提升为国家战略 ...

  5. 开发纯ndk程序之环境搭配

    安装ndk 从安卓官网下载,ndk,双击解压到当前文件夹.建议想装在那个文件夹便解压到那个文件夹,而且文件夹的路径中不要有空格,因为gcc编译的时候会把空格前后两个字符串作为两个文件夹来对待. 使用g ...

  6. Sql数据保存到Excel文件中

    public string ExportExcel( DataSet ds,string saveFileName) { try { if (ds == null) return "数据库为 ...

  7. 关于DDMS查看Data文件夹

    真机是无法查看的,只有通过模拟器查看

  8. FJ省队集训DAY4 T3

    #include<cstdio> #include<iostream> #include<cmath> #include<cstring> #inclu ...

  9. Bring it on

    I am going to open a whole new English Blog here. Most blogs here would be computer technologies, in ...

  10. Hibernate试题解析

    1.在Hibernate中,以下关于主键生成器说法错误的是(AC). A.increment可以用于类型为long.short或byte的主键(byte类型不可以) B.identity用于如SQL ...