COM Flashcards

1
Q

What are event sync and event source objects

A

Connection points is a technology that enables an object to ‘talk back’ to its clients. It involves two objects. Event sink is an object that resides in the client. This object implements an interface called ‘source interface’. This interface pointer is passed to the object i.e. COM component. So anything that needs to be notified to the client is notificed using source interface passed to the COM component. The COM component is called as event source object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How calls are executed in MTA

A

The threads that resides in MTA can directly make calls to the objects that reside in MTA. A thread that resides in STA, when calls to an MTA object, the call is delegated to an RPC thread which is spawned by the system and the call is executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

While in MTA, we cannot make assumption about TLS remaining constant between different method invocation. Why?

A

In MTA any available thread can execute method call, so it might happen that for one instance the method call is executed by thread A and for other instance its executed by thread B. So its not recommonded to use TLS in objects supporting MTA.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How DLLGetClassObject() function is implemented in case of EXE server. How IClassFactory interface pointer is actually created

A

When an activation request comes, COM SCM (Service Control Manager) looks for LocalServer key for the given CLSID and starts the given exe (server process). It is then responsibility of server process to notify SCM which classes are actually available from the new process. Server process register themselves with SCM using API CoRegisterClassObject(). When a process sees that there are no outstanding references to the supported objects it informs SCM about unloading by calling the API CoRevokeClassObject(). Once class objects are registered with SCM, its SCMs job to fulfill object creation requests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why marshalling is required?

A

Marshalling is required to achieve cross apartment call on an interface pointer. If raw interface pointers are passed among apartments, undefined behaviour could happen. Crash, deadlocks etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How marshalling works internally. What actually gets marshalled, Interface marshalling and/or data

A

Generally an interface pointer is marshalled. The proxy generated because of marshalling takes care of input and output parameter marshalling requirements. So the user just focuses on marshalling the interface pointer only.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is table marshalling

A

In normal marshalling, the marshalled interface pointer could be unmarshalled only once. Table marshalling supports marshal once and unmarshal zero or more times. Table marshalling is not suppported if the original pointer is proxy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is GIT marshalling

A

Global interface table marshalling is allowed in process only. This is optimization over CoMarshalInterface()_ API that allows interface pointer to be accessed by all apartments in the process. Marshalled interface pointer can be unmarshalled n times. The GIT marshalling maintain one GIT table per process. This GIT table actually holds the marshalled interface pointer. The registration of interface pointer with GIT is done using IGlobalInterfaceTable interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is marshalling by value. Example?

A

Standard marshalling operates by reference. It creates a proxy of interface pointer and operates using that proxy pointer. Marshalling by value permits local copies of objects to be made by value so the subsequent calls results in local calls. Custom marshalling is used to support marshalling by value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is automation? How it is supported in COM?

A

A COM component which support Idispatch interface is called automation server. Automation means scripts and VB client can call to COM components.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Vtable layout for disp interface and dual interface

A

The vtable layout of custom interface is same as a C++ object layout having virtual functions. The functions are accessed through vptr. The dispatch interface means a set of functions are implemented by Idispatch::Invoke(). Each function is assigned a dispid and then Invoke is called on this dispid. Depending on dispid, Invoke makes calls to appropriate functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is disp, dual, and custom interface

A

Dispatch Interface: The set of functions implemented by an Idispatch::Invoke() is called a dispatch interface or dispinterface. Custom interface: user Interface which is derived from Iunknown. Dual interface: Interface which supports both disp and custom interface i.e. members of this interface are accessible through Invoke and vtable both.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How dispinterface is implemented. How calls from scripts are executed

A

IDispatch::Invoke() method implements functionality of all the functions supported by the user interface. It calls to the specific function using dispid. Only Interfaces which derives from Idispatch can be accessible through any scripting language. The script engine creates COM object and asks for IDispatch interface pointer.IDispatch is a standard COM interface so its IID is known to all. It calls then GetIdsofNames() function to get dispid of a particular function and then calls Invoke on that dispid.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Name any 4 ATL macros

A

BEGIN_COM_MAP, END_COM_MAP, BEGIN_OBJECT_MAP, END_OBJECT_MAP

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can you have 2 interfaces having diff marshalling technique. What should be the approach, marshalling related to object or interfaces supported by object

A

“Yes you can have 2 difference interfaces of same object supporting 2 different marshalling techniques. There are only two marshaling techniques in COM - standard marshaling and custom marshaling. Standard marshaling is per interface. Custom
marshaling is per object and overrides standard marshaling - all your object needs to do to support custom marshaling is implement IMarshal. However, you can delegate certain interfaces to standard marshaling from within your custom marshaling implementation (e.g. IMarshal). Marshalling technique is decided per COM object.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Advantages of COM?

A
  1. Location transperancy. 2. Language independence 3. Better versioning
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Difference between MTA & NTA

A

The main difference between MTA and NTA is threads are not assigned to NTA, NTA contains only objects. If a thread makes calls to the objects in NTA then it leaves its present apartment and temporarily enters NTA.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

When does a thread is assigned to an apartment and when object is assigned an apartment

A

COM library is initialized by calling function CoInitialize() or CoInitializeEx(). Thread is assigned to apartment, when any of these function is called. The apartment type is dependant on the parameters passed to the function. In-proc object is assigned to an apartment when it is created. The type of apartment it is assigned depends on the apartment type specified in its registry settings. Out-of-proc objects are placed in the same apartment as the thread in the server process that creates the object. Most of out-of-proc COM servers begin by calling either CoInitialize() or CoInitializeEx() to place their primary thread in an STA.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

what will happen it you don’t marshal the interface ptr?

A

The behaviour is unpredictable. Could cause random crash, deadlock etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

In MTA, if client and object are on different thread we don’t need marshalling. Why?

A

If client thread and object thread both are placed in same MTA then we don’t need marshalling. Marshalling is required for cross apartment calls only.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

why do we need to marshals in /out/out,retral params?when?why?

A

Marshalling is just packaging of arguments so that they can be passed to an object which is residing in other process or may be on other machine. In process cross apartment params don’t need marshalling as the parameters can easily be put on a stack and can be retrieved in a called method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

an objects belongs to one & only one aptr - does this holds true for threads ?

A

Yes, threads are also belong to one and only one apartment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is free threaded marshaller?

A

FTM (Free threaded marshaler) is an optimization technique. The FTM enables an in-process object to pass a direct pointer into any client apartment. When an object uses FTM, all client threads, regardless of whether they are STA or MTA threads, call the object directly instead of through a proxy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How reusablility is implemented in COM

A

Reusability is implemented in the form of containment and aggregation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

why objects manage their own lifetine

A

Its one of the feature of the COM that objects manage their own lifetimes. By giving this feature the client are not bothered about the components and one component can serve many clients

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

how error handelling implement in com

A

These are some interfaces defined in COM like ISupportErrorInfo, IErrorInfo using which error handling is supported. ISupportErrorInfo interface is used to determine if a particular interface supports error handling or not. IErroInfo interface represents an error object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

what is the size of GUID ? What is GUID ?

A

GUID: Global Unique Identifier. This is used to uniquely identify COM classes, interfaces and libraries. It’s a 128 bit structure and is gaurantteed to be unique across all system at any point of time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

When will you go for STA & MTA

A

The objects threading model depends on the type of synchronization provided by the object. If COM object’s internal data members are shared between two different instances of same component, or global and static variables all are synchronized then object should be placed in MTA. If variables shared by two instances of same component are synchronized and rest is not synchronized then object should be placed into STA. If variables shared by two instances of same COM component are not synchronized and rest is also not synchronized then object should be place into main STA.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What is ATL?

A

ATL is ActiveX Template Libaray. This library provides basic infrastructure needed to create a COM component.

30
Q

Why use ATL and MFC for developing COM component

A

For faster development of the COM components. These frameworks provides basic infrastructure needed to develop various types of COM components like implementation of Iunknown etc tec

31
Q

Why use proxy and stub?

A

Proxy and stub are created when cross apartment calls are made on interface pointer. Proxy and stub facilitates marshalling and unmarshalling of interface pointers.

32
Q

Why do you need out-of-proc server

A

In-process activation of COM objects has some drawbacks. 1. Lack of fault isolation 2. Shared security context of client process. 2. Not a true distributed components as it uses CPU, resources of parent process.

33
Q

Remote machine calls. How will you make sure that there are no memory leaks like GetMachineName(BSTR*). How the BSTR will be freed.

A

GetMachineName allocates one BSTR internally, that BSTR then transferred to client through stub. Its job of the stub to free allocated BSTR and pass on the name to the client.

34
Q

What are the efficiency measures taken by ATL

A
  1. ATL_NO_VTABLE - This macro is used for the classes which acts as base class always. This macro suppresses the initiazation of vtable and vptr in the class’s constructor. This reduces the code size and so makes it faster. Note that initialisation is suppressed, this means that the vtable is generated but its not initialized. 2. ATL_MIN_CRT - By default all VC++ projects links to C runtime library even if one is not using any functions of it. Linking to CRT means startup code is compiled into the project, increasing size of the executable image. When ATL_MIN_CRT macro is defined ATL defines its own entry point functions for EXEs and DLLs, independant of CRTs. This avoids incurring overhead of the CRT startup code. 3. The dispatch interface’s function names and dispid are cached internally, this improves performance.
35
Q

How to make VB call thru vtable and dispinterface

A

A visual basic program can either connect to the dispinterface part or the vtable part of a dual interface. If a variable is declared as object, it connects thru the dispinterface( Dim a as Object) and if type of the variable is given then it connects thru table (Dim a as Document)

36
Q

A multithreaded appliacation is using 6/7 COM objects. What will be the threading model of the objects? Why?

A

Threading model for an object depend on the level of synchronization they support. See above answer.

37
Q

How to initialize NTA threading model

A

CoInitializeEx funciotn is used only to bind threads to a particular threading model, either STA or MTA; it cannot be used to create a NA. The only way to instantiate an object in NA is to call CoGetClassObject or CoCreateInstanceEX for a coclass that is marked in the registry as ThreadingModel=Neutral.

38
Q

Who marshals interface pointer and when?

A

The apartment which holds true interface pointer marshals interface pointer so that other apartments can be used. The marshalling is done when it is required.

39
Q

In MTA, if client and object are diff thread we don’t need marshalling? Why? Why do we need to marshal in case of cross apartment or one STA calling into other STA

A

In MTA one thread can call objects that are on other threads directly that is why marshalling is not required there. In case of STA, only the thead which has created the object can call a method on it. So all other STA thread must delegate the calls to creating thead. For that the interface pointer need to marshalled so that the other threads can safely post their calls as window message to the creator STA.

40
Q

How to marshal in and unmarshal out parameters

A

Programmer don’t need to do anything special to marshal / unmarshal in and out parameters. This case is taken by proxy and stub.

41
Q

CoCreateInstance() and CoGetClassObject(). Why we need to diff functions

A

CoCreateInstance() creates a COM object and returns desired interface pointer to that COM object. CoGetClassObject() API is used to create class factory object and get class factory interface pointer.

42
Q

What is the main drawback of STA. If server is multithreaded and client is in STA, what will get affected i.e. performance or scalability

A

The performance will be affected since the marshalling is rquired and alos thread switch will be made as call moves from STA to MTA

43
Q

Can COM objects be created on stack? If no why?

A

No, not possible. The COM objects are designed in such a way that its not possible to create them on stack. The destructor of the COM class is made protected so that it can not be called outside the class members.

44
Q

Coclass is a feature of ATL or COM

A

CoClass is a feature of COM. Idl file contains a library section, the coclass is mentioned in this section. The library definition sets the contents of the type libraray file (.tlb file).

45
Q

what is use of object map

A

Object map hooks up the class to class object and does the automatic registration of all classes as they appear in object map. All externally creatable objects should have entry into the object map. The object map creates an array of structures _ATL_OBJMAP_ENTRY. This structure defines 2 functions and initializes some function pointers. BEGIN_OBJECT_MAP marks start of the array and END_OBJECT_MAP end of the array. OBJECT_ENTRY macro defines new entry into the array.

46
Q

What is class factory

A

Class factory is simply a component with an interface for creating other components. All COM objects are created using class factory. The class factory component implements IClassFactory interface.

47
Q

how do you get an object of class factory

A

Each COM component has a corresponding ‘class object’ which is used to do tasks which are not instance specific. This ‘class object’ is itself a COM object having same CLSID as the type of COM objects it will be creating and supports IClassFactory interface. A class factory object can be created using CoGetClassObject() API.

48
Q

which function are exposed by com DLL

A

DllGetClassObject(), DllCanUnloadNow(), DllRegisterServer(), DllUnregisterServer().

49
Q

what is MTA

A

MTA is multi threaded apartment. One process can have maximum 1 MTA. N threads and n objects can reside in a MTA. No hidden window is associated with MTA. Threads can directly call into the object if its in the same apartment.

50
Q

what is STA

A

STA is single threaded apartment. One process can have n STA. 1 thread is associated with STA and can have multiple objects associated with it. STA cannot execute more than one call at a time, so no 2 objects or no 2 methods of same object can be called. Every call destined for an object in STA is transferred to the STA’s thread before being delievered.

51
Q

which are the function required for marshalling

A

CoMarshalInterface() CoUnmarshalInterface() these functions can be used to marshal any interface pointer that to be used across apartments which spans process boundaries. CoMarshalInterThreadInterfaceInStream() and CoGetInterfaceAndReleaseStream() APIs are used to marshal interface pointers to be used in single process

52
Q

What is marshalling

A

marshalling is packaging and unpackaging of interface and interface method paramters so that they can be safely passed across apartments, processes or machines

53
Q

How to convert an STA component to Both and vice versa

A

STA to both - change the registry of the compoent to have both as threading model. Make the component thread safe as both marked objects can be instantiated in MTA as well as MTA. Both -> STA, just change the registry of the component to have apartment threading model

54
Q

why BSTR is invented

A

String size is not predetermied so there is a problem in passing string across processes. The marshalling layer must exactly know the length of the string to pass it to the server. Also for out paramters COM states the the caller release the parameters so the caller would need to have access to the allocator. So BSTR are invented. The advantages of BSTR is the length of the string is prepended to it. This makes it possible to have NULL characters in the string and thus making it possible to pass binary data to the COM component. BSTRs are created using sysallocstring API so caller can free BSTR using sysfreestring() API thus gaining an access to the allocator.

55
Q

what are connection points

A

Connection points is a technology that enables an object to ‘talk back’ to its clients. It involves two objects. Objects that supports connection points are called as connectable objects

56
Q

What are COM interface properties

A

Each COM interface must support following three properties: 1. Symmetric - pA->QA(IIDA) must succeed. i.e pA->QA(A)=A. 2. Reflexive- if pA->QA(IIDB) = pB then pB->QA(IIDA)= pA. 3. Transitive pA->QA(IIDB) = PB, pB->QA(IIDC)=pC then pC->QA(IIDA)=pA.

57
Q

What does NO_VTABLE means?

A

ATL_NO_VTABLE - This macro is used for the classes which acts as base class always. This macro suppresses the initiazation of vtable and vptr in the class’s constructor. This reduces the code size and so makes it faster. Note that initialisation is suppressed, this means that the vtable is generated but its not initialized.

58
Q

Why COM supports only single inheritance

A

COM is compatible at binary level. So it doesn’t support any feature which might break its binary compatibility. Multiple inheritance is a feature which can break its binary compatibility since its implementation may differ from compiler to compiler and certain languages may not be able to provide MI itself

59
Q

Why use ATL

A

ATL provides basic infrastructure for a COM component. Using ATL developer can concentrate on his own functionaly & don’t need to bother about infrastructure code. ATL is quite fast as well.

60
Q

What is connection point? How do you get it?

A

Connection points is a technology that enables an object to ‘talk back’ to its clients. It involves two objects. Event sink is an object that resides in the client. This object implements an interface called ‘source interface’. This interface pointer is passed to the object i.e. COM component. So anything that needs to be notified to the client is notificed using source interface passed to the COM component.

61
Q

What is an interface

A

An interface is a specific memory structure containg array of function pointers. This memory structure’s layout is defined by COM specifications.

62
Q

What is Iunknown and its usage

A

All COM interfaces are required to inherit from Iunknown i.e. each COM component should support this interface. The client uses Iunknown to ask for other custom interface and reference counting.

63
Q

Diff between inproc and out-of-proc server? When each of them is used

A

Out of proc server are used to for fault isolation and to apply certain security context to the COM objects. At all rest places in proc servers can be used.

64
Q

How you register COM component? Which function is used internally to register? (Both Inproc/outproc)

A

InProc : DLLRegisterServer() and DllUnRegServer() are used to register COM component. Out of proc server provides command line options /regserver and /unregserver for registration.

65
Q

What is COM threading model

A

COM uses win32 theads and doesn’t define any new threads. These threading models are defined to address synchronization needs of a COM object. There are 3 main apartments STA, MTA and NTA.

66
Q

what is an apartment

A

Apartment is a logical concurrancy boundary. It contains thread and objects. Apartments are defined to handle synchronization need of COM objects.

67
Q

Diff between CoInitialize() and CoInitializeEx()

A

Not much difference. CoInitialize always initializes a STA, CoInitializeEx() takes two parameters, Second parameter decides the apartment type. It could be STA or MTA.

68
Q

What is Idispatch? Diff methods provided by Idispath? When it gets used?

A

Idispatch is a standard COM interface. All COM components supporting this interface are called as automation servers. Scripting languages can call COM methods using this interface only. It implements following methods GetTypeInfoCount(), GetTypeInfo(), GetIDsofNames(), and Invoke(). Out of these methods GetIDsofNames() and Invoke() are most frequently used and important methods.

69
Q

whats connection points analogy in SDK?

A

Callback functioins are the analogy for connection points in windows SDK

70
Q

Difference between Main STA and STA apartments

A

In STA data shared by two instances of same COM component is not synchronized. E.g. compoent A’s two instances are in two different STA’s and if these two instances share some data between them then that data is not synchronized. Main STA provides this facility. All COM components with Main STA (“none”) threading model are placed in one apartment, and so is synchronized.

71
Q

List of files generated by ATL appwizard?

A

Project.cpp, stdafx.h, stdafx.cpp, resource.h, project.rc, Project.idl, Project.def, COM_class.h, COM_class.cpp, COM_class.reg file.