Click here to Skip to main content
15,886,799 members
Articles / Programming Languages / C++
Article

COM Interview Questions

Rate me:
Please Sign up or sign in to vote.
2.80/5 (27 votes)
23 Jun 20058 min read 141.5K   72   19
Good interview question COM

Introduction

  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 QueryInterface.
  2. 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.

  3. What should QueryInterface functions do if requested object was not found? Return E_NOINTERFACE and nullify its out parameter.
  4. 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, you’ll 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. 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.
  10. What’s 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 today’s world is not exactly true as well, because what people call COM today is likely implementation of COM spec by Microsoft.
  11. What’s 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.
  12. 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.
  13. 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:
    dim d1 as IDualInterface1
    dim d2 as IDualInterface2
    set d1 = new MyClassWithTwoDuals
    set d2 = d1
    
    In ATL’s class you would have to use macro COM_INTERFACE_ENTRY2(IDispatch,
    IDualInterface1), to distinguish between different dual interfaces.
  14. 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 won’t 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”.
  15. 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.
  16. Let’s assume we have object B and aggregated object C (in-proc server), created by B. Can you access any interface of B from C? What’s 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. QI’ing 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.
  17. 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 object’s 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.
  18. 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.
  19. 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.
  20. 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.
  21. How can you guarantee that only remote server is ever created by a client? Create an object (call CoCreateObjectEx()) with CLSCTX_REMOTE_SERVER flag.
  22. What is __declspec(novtable)? Why would you need this? __declspec(novtable) is a Microsoft’s 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)
  23. What is an IDL? IDL stands for Interface Definition Language. IDL is the language to describe COM interfaces.
  24. 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.
  25. What is OLE? OLE is an object and embedding first implementation of COM spec available from MS before COM was officially named COM.
  26. Give examples of OLE usage. The most famous examples are probably drag and drop and structured storage implementations.
  27. What are 2 storage types for composite document? Storage and Stream.
  28. 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Think good and good will happen...

Comments and Discussions

 
QuestionI failed with CoCreateInstanceFromFile Pin
pikkaro7-Jan-07 16:56
pikkaro7-Jan-07 16:56 
Questionthread usage in structured storage? Pin
cpp_prgmer17-Sep-06 19:52
cpp_prgmer17-Sep-06 19:52 
GeneralBig Problem Pin
Ahmed Charfeddine11-Jul-06 2:41
Ahmed Charfeddine11-Jul-06 2:41 
Is Agregation the solution to the following problem :

I have many resembling activex controls (third party components with their SDKs) ( resemblance in functionnality). they are controls that controls different types of IP Surveillance Camera. I need to develop an application that is independent of them, ie access them independently, so that when i add new control for a new camera, the application needs not be recompiled. so the code has to be external to the application, more than that the linking should be explicit and at execution time rather at load time.
I tried using a DLL which contains a Cwnd object that embeds the activex control in order to declare the EventSink map there and be able to capture events and then relay them to the application by telling the Dll to link to its caller module.
but i did'nt even succeed to make calls in the direct sense, ie application-->exp Dll functions-->Cwnd member functions-->activex specific members, so to create the object etc, the problem being MFC-class pointer can't pass between app and Dll !!?
So how to encapsulate the different activex components in an abstract one. i tried to see COM, but unsuccessfully: is there a sort of activex control inheritance at least (derivation)? Or can I apply containment ( by creation of new activeX control or a general COM component which would contains an object of the particular ActiveX)

here's some other details (Note for the ActiveX control I can access them via C++, by building an MFC proxy class ( Create New class-->MFC class from ActiveX ) then integrate that class into a container where i insert the EventSink Map. Well i succeeded 100% in integrating evry control, and capturing its events, etc, but it's just about encapsulation)
the details :
"
the activex control i'm talking about are as i understood a sort of COM objects, which are to me classes that can be instantiated at execution time, so the dll needs not an implicit link to the client exe.
i have an application that access different camera on the network through those particular activex control, the code being like this :
for example for an axis camera, in the main view window i call
m_Axis.Create(...,this,CRect(..),..); ( m_Axis being here an object of an MFC proxy class to an activeX control (Axis Camera Media Control)).
and of course i handle the onsize member function :
so you can see that the activex control is even responsible for rendering the video stream. other members do the connection, allow me to get a handle to the image being displayed, etc.
so my problem is that i do'nt want the code to be integrated into my application, in order for the application not to depend on those control. so i want some sort of specification such the application can load and talk independently to those objects.

the code would look like :
CcameraControl *_cam[5];
and when the user adds a camera to be controled by the application, a dialog prompting for its ip address, and very possibly its type, appears, then i use the type information to localize the control which is then loaded into a case of the table above.
so i need to encapsulate, create new controls for evry activex, such that those control have the same functions and can talk to the caller-application using a same mechanism.
here's what i tried :
i thought i must derive new classes from the activex control, such those classes have the same functions. they need to relay the control from the application to their parent specific services. and capture events back from their parent and relay them to the application, but here i came to the inevitable ad the impossible :
although succeeding in encapsulating the controls into uniform components, there is no WAY for those classes to be loaded at the execution time and behave as true plug-in components. for example for MFC, if there is a wa&y to write a dll that exports some classes dervived from mfc classes, that dll need be implicitly linked to the exe, and this latter wouldn't succeed even to load without her.

Is in COM, ATL a solution ?
So what i did was to create a simple dll, that can access mfc, then in the CWinApp object i declared a Cwnd object ( acontainer for the specific control) and to there i shifted an activex control data member: when the window is created, it creates the control in its area. i declared the eventsink map in the CWnd so to capture the events. and then you have simple plain exportable functions that enable the caller to create the internal CWnd object and calles its members. but since i need to pass a parent window (originating from the application interface classes) then the problem i talked about comes ( mfc pointer)

here's also what i tried :
a new MFC ActiveX project, thinking i can apply the containment principle :
an abstract, wrapper Control contains the specific one, relay the evnts in either senses, but it just didn't work, the specific activex control create function fails when the OnCreate container member function is called to load the global control"
Thank u in advance.
charfeddine_ahmed@yahoo.fr
GeneralI voted you down Pin
Trollslayer10-Oct-05 2:44
mentorTrollslayer10-Oct-05 2:44 
QuestionWhat is IDispatch Pin
Anonymous23-Aug-05 18:24
Anonymous23-Aug-05 18:24 
AnswerRe: What is IDispatch Pin
guestcat2-May-06 1:43
guestcat2-May-06 1:43 
GeneralNice Pin
melwyn28-Jun-05 23:30
melwyn28-Jun-05 23:30 
GeneralRe: Nice Pin
Blue_Aqua29-Jun-05 0:34
Blue_Aqua29-Jun-05 0:34 
Generalhelp on perf error Pin
jacksmith1239-Jul-06 3:26
jacksmith1239-Jul-06 3:26 
QuestionHow to extract COM information from a DLL ? Pin
Rogerio Silva28-Jun-05 8:14
Rogerio Silva28-Jun-05 8:14 
AnswerRe: How to extract COM information from a DLL ? Pin
Blue_Aqua28-Jun-05 22:09
Blue_Aqua28-Jun-05 22:09 
GeneralRe: How to extract COM information from a DLL ? Pin
Rogerio Silva29-Jun-05 3:43
Rogerio Silva29-Jun-05 3:43 
GeneralRe: How to extract COM information from a DLL ? Pin
Anonymous29-Jun-05 10:58
Anonymous29-Jun-05 10:58 
GeneralRe: How to extract COM information from a DLL ? Pin
windows_linuxOVL24-Nov-05 21:59
windows_linuxOVL24-Nov-05 21:59 
AnswerRe: How to extract COM information from a DLL ? Pin
Shashikant_200617-Jul-06 19:53
Shashikant_200617-Jul-06 19:53 
GeneralOh Gawd -- not again! Pin
fwsouthern24-Jun-05 10:44
fwsouthern24-Jun-05 10:44 
GeneralRe: Oh Gawd -- not again! Pin
Blue_Aqua27-Jun-05 20:34
Blue_Aqua27-Jun-05 20:34 
GeneralRe: Oh Gawd -- not again! Pin
mcbain30-Jun-05 0:00
mcbain30-Jun-05 0:00 
GeneralRe: Oh Gawd -- not again! Pin
Andrea7527-Jul-05 2:43
Andrea7527-Jul-05 2:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.