COM的一些基本概念

?

  1. Windows lets you share code at the binary level using DLLs. After all, that‘s how Windows apps function - reusing kernel32.dll, user32.dll, etc. But since the DLLs are written to a C interface, they can only be used by C or languages that understand the C calling convention. This puts the burden of sharing on the programming language implementer, instead of on the DLL itself.

    MFC introduced another binary sharing mechanism with MFC extension DLLs. But these are even more restrictive - you can only use them from an MFC app.

    COM solves all these problems by defining a?binary standard, meaning that COM specifies that the binary modules (the DLLs and EXEs) must be compiled to match a specific structure. The standard also specifies exactly how COM objects must be organized in memory. The binaries must also not depend on any feature of any programming language (such as name decoration in C++). Once that‘s done, the modules can be accessed easily from any programming language. A binary standard puts the burden of compatibility on the compiler that produces the binaries, which makes it much easier for the folks who come along later and need to use those binaries.

    ?

  2. A?coclass?(short for?component?object?class) is contained in a DLL or EXE, and contains the code behind one or more interfaces. The coclass is said to?implement?those interfaces. A?COM object?is an instance of a coclass in memory. Note that a COM "class" is not the same as a C++ "class", although it is often the case that the implementation of a COM class is a C++ class.
  3. A?class ID, or?CLSID, is a GUID that names a coclass. An?interface ID, or?IID, is a GUID that names an interface.
  4. An?HRESULT?is an integral type used by COM to return error and success codes. It is not a "handle" to anything, despite the?H?prefix. I‘ll have more to say about HRESULTs and how to test them later on.
  5. The?COM library?is the part of the OS that you interact with when doing COM-related stuff. Often, the COM library is referred to as just "COM,"
  6. Every language has its own way of dealing with objects. For example, in C++ you create them on the stack, or use new?to dynamically allocate them. Since COM must be language-neutral, the COM library provides its own object-management routines. A comparison of COM and C++ object management is listed below:

    Creating a new object

  • In C++, use?operator?new?or create an object on the stack.
  • In COM, call an API in the COM library.

    Deleting objects

  • In C++, use?operator?delete?or let a stack object go out of scope.
  • In COM, all objects keep their own reference counts. The caller must tell the object when the caller is done using the object. COM objects free themselves from memory when the reference count reaches 0.

    Now, in between those two stages of creating and destroying the object, you actually have to?use?it. When you create a COM object, you tell the COM library what interface you need. If the object is created successfully, the COM library returns a pointer to the requested interface. You can then call methods through that pointer, just as if it were a pointer to a regular C++ object.

Handling HRESULTs

I‘ve already shown some simple error handling, using the?SUCCEEDED?and?FAILED?macros. Now I‘ll give some more details on what to do with the?HRESULTs returned from COM methods.

An?HRESULT?is a 32-bit signed integer, with nonnegative values indicating success, and negative values indicating failure. An?HRESULT?has three fields: the severity bit (to indicate success or failure), the facility code, and the status code. The "facility" indicates what component or program the?HRESULT?is coming from. Microsoft assigns facility codes to the various components, for example COM has one, the Task Scheduler has one, and so on. The "code" is a 16-bit field that has no intrinsic meaning; the codes are just an arbitrary association between a number and a meaning, just like the values returned by?GetLastError().

If you look up error codes in the?winerror.h?file, you‘ll see a lot of?HRESULTs listed, with the naming convention [facility]_[severity]_[description]. Generic?HRESULTs that can be returned by any component (likeE_OUTOFMEMORY) have no facility in their name. Examples:

  • REGDB_E_READREGDB: Facility = REGDB, for "registry database"; E = error; READREGDB is a description of the error (couldn‘t read the database).
  • S_OK: Facility = generic; S = success; OK is a description of the status (everything‘s OK).

    Fortunately, there are easier ways to determine the meaning of an?HRESULT?than looking through?winerror.h.HRESULTs for built-in facilities can be looked up with the Error Lookup tool. For example, say you forgot to callCoInitialize()?before?CoCreateInstance().?CoCreateInstance()?will return a value of 0x800401F0. You can enter that value into Error Lookup and you‘ll see the description: "CoInitialize has not been called."

    You can also look up?HRESULT?descriptions in the debugger. If you have an?HRESULT?variable called?hres, you can view the description in the Watch window by entering "hres,hr" as the value to watch. The ",hr" tells VC to display the value as an?HRESULT?description.

时间: 2024-10-25 19:18:21

COM的一些基本概念的相关文章

WPF 依赖属性概念

理解依赖属性 在 WPF 中变成相比较于 传统 Windows Forms 变成发生了较大的改变. 属性现在以一组服务的形式提供给开发人员. 这组服务就叫做属性系统. 由 WPF 属性系统所支持的属性成为依赖属性. 依赖属性的概念 WPF 在依赖属性中提供了标准属性无法提供的功能, 特性如下: 决定属性值: 依赖属性的属性值可以在运行时有其他元素或者是其他信息所决定, 决定的过程具有一个优先次序. 自动验证或变更通知: 依赖属性哟一个自定的回调方法, 当属性值变更时被执行, 这个回调能验证新的值

Docker的概念及剖析原理和特点

一.docker的简介: 应用容器是个啥样子呢,一个做好的应用容器长的就像一个装好了一组特定应用的虚拟机一样,比如我现在想用mysql数据库,我直接找个装好了的MySQL的容器就可以了,想用的时候一运行容器,MySQL服务就起来了,就可以使用MySQL了 为什么不能直接安装一个MySQL?或者是SqlServer呢也可以啊? 答:因为有的时候根据每个人的电脑的不同,在物理机安装的时候会出现各种各样的错误,突然你的机器中病毒了或者是挂了,你所有的服务都需要重新安装. 注意:    但是有了dock

老男孩教育每日一题-2017年5月11-基础知识点: linux系统中监听端口概念是什么?

1.题目 老男孩教育每日一题-2017年5月11-基础知识点:linux系统中监听端口概念是什么? 2.参考答案 监听端口的概念涉及到网络概念与TCP状态集转化概念,可能比较复杂不便理解,可以按照下图简单进行理解? 将整个服务器操作系统比喻作为一个别墅 服务器上的每一个网卡比作是别墅中每间房间 服务器网卡上配置的IP地址比喻作为房间中每个人 而房间里面人的耳朵就好比是监听的端口 当默认采用监听0.0.0.0地址时,表示房间中的每个人都竖起耳朵等待别墅外面的人呼唤当别墅外面的用户向房间1的人呼喊时

Tensorflow一些常用基本概念与函数(四)

摘要:本系列主要对tf的一些常用概念与方法进行描述.本文主要针对tensorflow的模型训练Training与测试Testing等相关函数进行讲解.为'Tensorflow一些常用基本概念与函数'系列之四. 1.序言 本文所讲的内容主要为以下列表中相关函数.函数training()通过梯度下降法为最小化损失函数增加了相关的优化操作,在训练过程中,先实例化一个优化函数,比如 tf.train.GradientDescentOptimizer,并基于一定的学习率进行梯度优化训练: optimize

Tensorflow一些常用基本概念与函数(三)

摘要:本系列主要对tf的一些常用概念与方法进行描述.本文主要针对tensorflow的数据IO.图的运行等相关函数进行讲解.为'Tensorflow一些常用基本概念与函数'系列之三. 1.序言 本文所讲的内容主要为以下相关函数: 操作组 操作 Data IO (Python functions) TFRecordWrite,rtf_record_iterator Running Graphs Session management,Error classes 2.tf函数 2.1 数据IO {Da

数据结构基础概念

1.数据的特点:可以输入到计算机,可以被计算机程序处理 2.数据是一个抽象的概念,将其进行分类后得到程序设计语言中的类型.如:int float char等等 3.数据元素-组成数据的基本单位,数据项:一个数据元素由若干数据项组成 4.数据对象 -性质相同的数据元素的集合 5.数据元素之间不是独立的,存在特定的关系,这些关系即结构 6.数据结构指数据对象中数据元素之间的关系,编写一个"好"的程序之前,必须分析待处理问题中各个对象的特性,以及对象之间的关系 7.逻辑结构 集合结构--数据

Data guard概念篇一(转载)

本文转载至以下链接,感谢作者分享! http://tech.it168.com/db/2008-02-14/200802141545840_1.shtml 一.Data Guard配置(Data Guard Configurations) Data Guard是一个集合,由一个primary数据库(生产数据库)及一个或多个standby数据库(最多9个)组成.组成Data Guard的数据库通过Oracle Net连接,并且有可能分布于不同地域.只要各库之间可以相互通信,它们的物理位置并没有什么

js的闭包概念

一.变量的作用域要懂得闭包,起首必须懂得Javascript特别的变量作用域.变量的作用域无非就是两种:全局变量和局部变量.Javascript说话的特别之处,就在于函数内部可以直接读取全局变量. Js代码 var n=999; function f1(){ alert(n); } f1(); // 999另一方面,在函数外部天然无法读取函数内的局部变量.Js代码 function f1(){ var n=999; } alert(n); // error这里有一个处所须要重视,函数内部声明变量

【干货】如何使用C++11实现C#属性概念设计

目录(原创博客,版权所有,转载请注明出处 http://www.cnblogs.com/feng-sc) 1.概述 2.C#属性的概念  2.1.简单示例代码介绍C#中的属性  2.2.C++实现效果与C#效果对比(熟悉C#属性读者可从此小节开始) 3.如何使用C++11实现C#属性的概念模型 3.1.property_rw介绍 3.2.property_r.property_w介绍 3.3.完整属性测试代码介绍 4.总结 1.概述 本人对“C++实现C#属性概念”的研究决定并非一时冲动,而是原

weblogic一些基本概念

<收藏过来的----------http://www.cnblogs.com/cocowool/archive/2012/04/01/2428861.html> WebLogic中的一些基本概念 WebLogic 中的基本概念 上周参加了单位组织的WebLogic培训,为了便于自己记忆,培训后,整理梳理了一些WebLogic的资料,会陆续的发出来,下面是一些基本概念. Domain : 域是作为单元进行管理的一组相关的 WebLogic Server 资源.一个域包含一个或多个 WebLogi