COM Flashcards
What are event sync and event source objects
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 calls are executed in MTA
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.
While in MTA, we cannot make assumption about TLS remaining constant between different method invocation. Why?
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 DLLGetClassObject() function is implemented in case of EXE server. How IClassFactory interface pointer is actually created
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.
Why marshalling is required?
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 marshalling works internally. What actually gets marshalled, Interface marshalling and/or data
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.
What is table marshalling
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.
What is GIT marshalling
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.
What is marshalling by value. Example?
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.
what is automation? How it is supported in COM?
A COM component which support Idispatch interface is called automation server. Automation means scripts and VB client can call to COM components.
Vtable layout for disp interface and dual interface
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.
what is disp, dual, and custom interface
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 dispinterface is implemented. How calls from scripts are executed
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.
Name any 4 ATL macros
BEGIN_COM_MAP, END_COM_MAP, BEGIN_OBJECT_MAP, END_OBJECT_MAP
Can you have 2 interfaces having diff marshalling technique. What should be the approach, marshalling related to object or interfaces supported by object
“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.”
Advantages of COM?
- Location transperancy. 2. Language independence 3. Better versioning
Difference between MTA & NTA
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.
When does a thread is assigned to an apartment and when object is assigned an apartment
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.
what will happen it you don’t marshal the interface ptr?
The behaviour is unpredictable. Could cause random crash, deadlock etc.
In MTA, if client and object are on different thread we don’t need marshalling. Why?
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.
why do we need to marshals in /out/out,retral params?when?why?
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.
an objects belongs to one & only one aptr - does this holds true for threads ?
Yes, threads are also belong to one and only one apartment.
What is free threaded marshaller?
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 reusablility is implemented in COM
Reusability is implemented in the form of containment and aggregation.
why objects manage their own lifetine
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 error handelling implement in com
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.
what is the size of GUID ? What is GUID ?
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.
When will you go for STA & MTA
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.