COM常见问题译

7 篇文章 0 订阅
6 篇文章 0 订阅

这几天项目收尾,在研究WPF的同时,找到一篇com相关的文章,基础的东西。

原网址:http://www.codeproject.com/KB/COM/COM_InterviewQuestions.aspx

英文看起来没有中文快,于是译之,译得不好大家不要见怪,如下:

 

  1. What is IUnknown? What methods are provided by IUnknown? It is a generally good idea to have an answer for this question if you claim you know COM in your resume. Otherwise, you may consider your interview failed at this point. IUnknown is the base interface of COM. All other interfaces must derive directly or indirectly from IUnknown. There are three methods in that interface: AddRef, Release and QueryInterfae.                                                    

         什么IUnknown接口?它都提供什么方法?你在回顾COM学习时应该能很清楚的回答这个问题,否则你的COM基本上就等于没学了。IUnknown接口 COM最顶层的接口,所有Com接口都必须直接或者间接的从IUnkown继承。IUnknown接口提供以下三个方法:

AddRef,Release,QueryInterface

  1. What are the purposes of AddRef, Release and QueryInterface functions? AddRef increments reference count of the object, Release decrements reference counter of the object and QueryInterface obtains a pointer to the requested interface.

AddRef,Release,QueryInterface这三个方法有何作用,为何IUnkown接口要提供这三个方法?AddRef 将对象的引用计数器加1,Release则相反,QueryInterace可以返回一个指向被请求的接口的指针。

  1. What should QueryInterface functions do if requested object was not found? Return E_NOINTERFACE and nullify its out parameter.

如果请求的对象或者接口不存在的话,QueryInterface会返回什么?返回E_NOINTERFACE,并且将它的输出参数置空。(输出参数即vc6为接口添加方法时的out参数)

  1. How can would you create an instance of the object in COM? Well, it all depends on your project. Start your answer from CoCreateInstance or CoCreateInstanceEx, explain the difference between them. If interviewer is still not satisfied, youll have to explain the whole kitchen behind the scenes, including a difference between local server and inproc server, meaning and mechanism of class factory, etc. You may also mention other methods of object creation like CoGetInstanceFromFile, but discussion will likely turn to discussion of monikers then.

如何才能创建一个Com对象的实例? 这与你使用Com的方式有关.首先你必须要明白CoCreateInstance CoCreateInstanceEx不同之外。如果这两个方法也不明白,那么就必须解释整个COM的背景了,包括本地服务和远程服务的不同之处,类工厂的目的等等。当然也可以使用其它的方法创建COM对象,如CoGetInstanceFromFile,但是这样的话又得讨论一下相关的方法了。

  1. What happens when client calls CoCreateInstance? Again, all depends on the level of detail and expertise of interviewer. Start with simple explanation of class object and class factory mechanism. Further details would depend on a specific situation.

客户端调用CoCreateInstance方法时会发发生什么? 呵呵,这又要求你懂一些相关的技术了,首先要明白类工厂的机制,特定的情形下会更容易明白类工厂的机制。

  1. What the limitations of CoCreateInstance? Well, the major problems with CoCreateInstance is that it is only able to create one object and only on local system. To create a remote object or to get several objects, based on single CLSID, at the same time, one should use CoCreateInstanceEx.

CoCreateInstance有何限制吗?嗯,使用CoCreateInstance方法只能创建一个对象,并且只能本地调用。要创建远程对象或者要同时获取几个具有不同CLSID的对象,要使用CoCreateInstanceEx方法。

  1. What is aggregation? How can we get an interface of the aggregated object? Aggregation is the reuse mechanism, in which the outer object exposes interfaces from the inner object as if they were implemented on the outer object itself. This is useful when the outer object would always delegate every call to one of its interfaces to the same interface in the inner object. Aggregation is actually a specialized case of containment/delegation, and is available as a convenience to avoid extra implementation overhead in the outer object in these cases. We can get a pointer to the inner interface, calling QueryInterface of the outer object with IID of the inner interface.

什么是聚合?如何获取聚合对象的接口? 聚合是一种重用机制,即当内部对象的某些接口由外部对象实现时,再将这些接口从内部对象中暴露出来。当外部对象总是委托内部对象来调用自己的接口时,这非常有用。聚合是包容/委托的一个特例,它非常适合避免调用外部对象的额外的接口。通过传入内部接口的IID参数,调用外部对象的QueryInterface接口,我们就能获取到指向内部接口的指针。

  1. C is aggregated by B, which in turn aggregated by A. Our client requested C. What will happen? QueryInterface to A will delegate request to B which, in turn, will delegate request for the interface to C. This pointer will be returned to the client.

C通过B来聚合,并且C也被A聚合了。我们客户端请求C对象,会发生什么? A 中的 QueryInterface 将将请求委托给B,然后B再委托给C,这里是具有传递性的。

  1. What is a moniker ? An object that implements the IMoniker interface. A moniker acts as a name that uniquely identifies a COM object. In the same way that a path identifies a file in the file system, a moniker identifies a COM object in the directory namespace.

什么是moniker一个对象如果实现了IMoniker接口的话,一个moniker实际上相当于COM对象中的唯一标记,就如同在文件系统中用文件名来作为文件的唯一标记一样,一个moniker指向一个唯一标记的COM对象。

  1. Whats the difference, if any, between OLE and COM? OLE is build on top of COM. The question is not strict, because OLE was built over COM for years, while COM as a technology was presented by Microsoft a few years ago. You may mention also that COM is a specification, while OLE is a particular implementation of this specification, which in todays world is not exactly true as well, because what people call COM today is likely implementation of COM spec by Microsoft.

OLECOM有何区别?OLE建立在COM的基础上。这个问题不是很严谨,因为OLE在COM基础上很多年了,但是几年前微软才向外界面展示COM技术。也许你会说COM是一个规范,但现在来看这样说也不确切,因为人们今天调用COM只能依赖于微软件的COM实现文档。惨

  1. Whats the difference between COM and DCOM? Again, the question does not require strict answer. Any DCOM object is yet a COM object (DCOM extends COM) and any COM object may participate in DCOM transactions. DCOM introduced several improvements/optimizations for distributed environment, such as MULTI_QI (multiple QueryInterface()), security contexts etc. DCOM demonstrated importance of surrogate process (you cannot run in-proc server on a remote machine. You need a surrogate process to do that.) DCOM introduced a load balancing.

COM与DCOM又有何区别呢?再次声明,这样问题问得不太严谨,因为所有的DCOM对象都是一个COM对象(因为DCOM本来就是扩展的COM嘛),并且所有的COM对象是可以与DCOM密切相关的。DCOM在分布式环境下做了改进和优化,例如MULTI_QI(多个QueryInterface()),安全上下文等等。代理进程(你无法在远程机器上启动一个进程),负载均衡等都是DCOM的特色。

  1. What is a dual interface? Dual interface is one that supports both - IDispatch interface and vtbl-based interface. Therefore, it might be used in scripting environment like VBScript and yet to use power and speed of vtbl-based interface for non-scripting environment. Discussion then may easily transform into analyzing of dual interface problems - be prepared to this twist.

什么是双接口?双接口也就是有两个IDispatch接口和两个虚函数表(当然这应该只有c++才有此概念了)。因此,它能在VBScript中使用,甚至在一些非脚本语言的环境中使用虚函数表的高性能。讨论他们很容易就会涉及到双接口的问题。

  1. Can you have two dual interfaces in one class? Yes. You may have two dual interfaces in one class, but only one of them may be default. The bottom line is that you cannot work with two dual interfaces at the same time due to nature of dual interface! To support two dual interfaces in VB you would write something like:

14.            dim d1 as IDualInterface1

15.            dim d2 as IDualInterface2

16.            set d1 = new MyClassWithTwoDuals

17.            set d2 = d1

   

In ATLs class you would have to use macro COM_INTERFACE_ENTRY2(IDispatch,
IDualInterface1), to distinguish between different dual interfaces.

我能在一个类中实现两接口吗? 当然啦!你可以在一个类中实现双接口,但是只能有一个接口可以作为默认的接口,双接口就注定了你不能同时使用两个接口。你可像以下VB的代码一样使用双接口。

  1. What is marshalling by value? Some objects can essentially be considered static: regardless of which methods are called, the state of the object does not change. Instead of accessing such an object remotely, it is possible to copy the static state of the object and create a new object with the same state information on the caller side. The caller wont be able to notice the difference, but calls will be more efficient because they do not involve network round trips. This is called marshaling by value.

   

  1. What is a multi-threaded apartment (MTA)? Single-threaded apartment (STA)? This is pretty difficult question to describe shortly. Anyway, apartments were introduced by Microsoft in NT 3.51 and late Windows 95 to isolate the problem of running legacy non-thread safe code into multithreaded environment. Each thread was encapsulated into so called single-threaded apartment. The reason to create an object in apartment is thread-safety. COM is responsible synchronize access to the object even if the object inside of the apartment is not thread-safe. Multithreaded apartments (MTA, or free threading apartment) were introduced in NT 4.0. Idea behind MTA is that COM is not responsible to synchronize object calls between threads. In MTA the developer is responsible for that. See Professional DCOM Programming of Dr. Grimes et al. or Essential COM of Don Box for the further discussion on this topic.
  2. Lets assume we have object B and aggregated object C (in-proc server), created by B. Can you access any interface of B from C? Whats the difference between aggregated and contained objects? Yes, you can. This is fundamental postulate of COM: If you can get there from here, you can get there from anywhere, i.e. QIing for IUnknown you may proceed and to get a pointer to any other interface, supported by the object. Aggregated object exposes its interface directly, without visible intervention of the object container. Contained object is created within the object container and its interfaces might be altered or filtered by the object container.
  3. What is ROT ? GIT ? Count pros and cons of both. By definition, running object table (ROT) is a globally accessible table on each computer that keeps track of all COM objects in the running state that can be identified by a moniker. Moniker providers register an object in the table, which increments the objects reference count. Before the object can be destroyed, its moniker must be released from the table. Global Interface Table (GIT) allows any apartment (either single- or multi-threaded) in a process to get access to an interface implemented on an object in any other apartment in the process.
  4. If you have an object with two interfaces, can you custom marshal one of them? No! The decision to use custom marshaling is an all-or-nothing decision; an object has to custom marshal all its interfaces or none of them.
  5. Is there a way to register in-proc server without regsvr32.exe? Yes. Call DllRegisterServer() from the client. Do not forget to call DLLUnregisterServer() from the same client. You may also use Registrar object for the same purpose or use direct manipulation of the windows registry.
  6. What is VARIANT? Why and where would you use it? VARIANT is a huge union containing automation type. This allows easy conversion of one automation type to another. The biggest disadvantage of VARIANT is size of the union.

VARIANT是个啥东东?为何要这东东?在哪里用到它呢?VARIANT 是一个大的联合体,它能够自动将一种类型转为另外一种类型,它的大小由它里面最大的那个决定(这是联合体的概念了)

  1. How can you guarantee that only remote server is ever created by a client? Create an object (call CoCreateObjectEx()) with CLSCTX_REMOTE_SERVER flag.
  2. What is __declspec(novtable)? Why would you need this? __declspec(novtable) is a Microsofts compiler optimization. The main idea of this optimization is to strip the vtable initialization code from abstract class (for abstract class the vtable is empty, while it is initialized in contructor)

什么是__declspec(novtable)?在哪里需要用到这东东?  __declspec(novtable)是微软件的编绎器做一种优化,也就是对一些抽象类不生成虚函数表。

  1. What is an IDL? IDL stands for Interface Definition Language. IDL is the language to describe COM interfaces.

什么是IDL?IDL全称是Interface Definition Language,也就是用来描述COM接口的语言了。

  1. What is In-proc? In-proc is in-process COM object, i.e. COM object that implemented as DLL and supposed to be hosted by a container. When you have to instantiate the in-proc object remotely, you may use DLLHost.exe application that was design specially for this purpose.

什么是in-proc? In-proc即COM对象内部的进程,也就COM对象可以为为DLL格式,但却是宿主容器。当你必须远程启动一个in-proc进程时,你可以使用DLLHost.exe进程。

  1. What is OLE? OLE is an object and embedding first implementation of COM spec available from MS before COM was officially named COM.
  2. Give examples of OLE usage. The most famous examples are probably drag and drop and structured storage implementations.
  3. What are 2 storage types for composite document? Storage and Stream.
  4. Is .doc document a compound document? Is it a structured storage? Compound document is a document that contains information about other documents hosted in this document. All office documents _may_ be compound documents, but may be not. Word documents from version 6.0 and up are stored as structured storage.

 还有一些没译,大家自己看看吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值