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. C# 调用浏览器打开网址

    private void button1_Click(object sender, EventArgs e) { //调用系统默认的浏览器 System.Diagnostics.Process.Sta ...

  2. C语言初学 比较五个整数并输出最大值和最小值1

    #include<stdio.h> #include<math.h> int max(int x,int y) { if(x>y) return x; else retu ...

  3. 资本助力转型 TCL攻守转换(转型、并购、过冬前一定要保留强大的战略预备队)

    吴侨发 9月份,TCL集团(3.92, 0.00, 0.00%)160多位高管启动四天戈壁行.在戈壁行第三天,30多公里的戈壁,差不多等于平路50公里,最后两只队伍走了13个小时.路途的艰辛几乎超过这 ...

  4. DateTime字段控件值显示短格式的做法

    后台取dateTime格式,前台格式化就好了 <input type="text" name="txtPartyTime" id="txtPar ...

  5. struct2(六) 为表单添加验证

    简介 为表单添加验证 添加校验的方法: 1. first name 不能为null 2. Email address 不能为null 3. age 必须大于18岁 为了在用户提交的时候,能够校验这个表 ...

  6. cf443B Kolya and Tandem Repeat

    B. Kolya and Tandem Repeat time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  7. MVC几个系统常用的Filter过滤器

    1.AcceptVerbs 规定页面的访问形式,如 [AcceptVerbs(HttpVerbs.Post)] public ActionResult Example(){ return View() ...

  8. cocos2dx lua 加密

    cocos2dx-lua项目发布时,为了保护lua源码,需要对lua进行加密.通常分为两种方式:加密文件和编译为字节码. 1.加密文件 前提是你不用luajit,而使用lua.这样这种方法是真正加密, ...

  9. FreeBsdb FAMP Lamp环境

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA1IAAAHlCAIAAABwFFq0AAAgAElEQVR4nO3d23WruhYA0JTmciiGTm

  10. 【译】Javascript中的数据类型

    这篇文章通过四种方式获取Javascript中的数据类型:通过隐藏的内置[[Class]]属性:通过typeof运算符:通过instanceof运算符:通过函数Array.isArray().我们也会 ...