C#.NET COM Interop FAQ
1. In what purpose COM interop is using?
To provide access to the existing COM components without requiring that the original component be modified.

2. What mechanism is using in COM to convert source data type into target datatype?
marshalling

 
3. What is RegAsm .exe?
It exports the managed types into a type library and registers the managed component as a traditional COM component.

 
4. How C# uses COM objects to develop applications?
First thing to be done is the creation of wrapper class for the selected COM object. It can be done manually through the command line application called TlbImp.exe (Type Library Importer). This utility converts COM type library to the .NET Framework metadata. This procedure can be done automatically through the .NET environment. We just need to add reference to the COM object to our C# project. So, type in .NET command line next string: tlbimp $WindowsPath$\system32\quartz.dll /out: quartzNET.dll. This command will create a new dll with types that are compatible with any of Managed .NET languages. Now we can add this dll to our C#project or compile our C# file with additional feature "/r quartzNET.dll".

 
5. How wrapper class can be created?
ANSI-C++ is the name by which the international ANSI/ISO standard for the C++ language is known. But before this standard was published, C++ was already widely used and therefore there is a lot of code out there written in pre-standard C++. Referring to ANSI-C++ explicitly differenciates it from pre-standard C++ code, which is incompatible in some ways.It can be done manually through the command line application called TlbImp.exe (Type Library Importer). This utility converts COM type library to the .NET Framework metadata. This procedure can be done automatically through the .NET environment. We just need to add reference to the COM object to our C# project. So, type in .NET command line next string: tlbimp $WindowsPath$\system32\quartz.dll /out: quartzNET.dll. This command will create a new dll with types that are compatible with any of Managed .NET languages. Now we can add this dll to our C#project or compile our C# file with additional feature "/r quartzNET.dll".

 
6. Is DCOM dead?
DCOM will still be used in interop scenarios.

7. Is COM+ dead?
Not immediately. The approach for .NET 1.0 was to provide access to the existing COM+ services (through an interop layer) rather than replace the services with native .NET ones. Various tools and attributes were provided to make this as painless as possible. Over time it is expected that interop will become more seamless - this may mean that some services become a core part of the CLR, and/or it may mean that some services will be rewritten as managed code which runs on top of the CLR.



 
8. Which dll is needed for compiling an attached file?
To compile an attached example we just need this quartzNET.dll (is attached too) and .NET command line. Type here next command csc InteropSample.cs /r:quartzNET.dll. It must create an executable file.

 
9. What benefit do you get from using a Primary Interop Assembly (PIA)?
*A primary interop assembly contains type definitions (as metadata) of types implemented with COM.
*Only a single PIA can exist, which needs to be signed with a strong name by the publisher of the COM type library.
*One PIA can wrap multiple versions of the same type library.
*A COM type library imported as an assembly can be a PIA only if it has been signed and published by the same publisher.
Therefore, only the publisher of a type library can produce a true PIA, that can be considered as the unit of an official type definition for interoperating with the underlying COM types.

 
10. Demonstrate how to access unmanaged code using Interop?
a) Use System.Runtime.InteropServices.
b) Use DllImport to declare the unmanaged procedure.
c) Map the data types of the procedures parameters to equivalent .NET types.
d) Call the unmanaged procedure.
e) Test it

 
11. Explain the namespaces in which .NET has the data functionality class.?
System.Data

12. Which are the types of COM interop?
NET code can call COM components and COM code can call .NET components.

 
13. Which is creating COM type Library?
TLBEXP.EXE

 
14. What are the .NET rules for finding an assembly?
* First, the Global Assembly Cache (GAC) is checked.
* Then, the local directory is checked. 

 
15. What are the guidlines for to keep the overhead of thread switches to a minimum?
* Reduce or avoid cross-apartment calls.
* Use ASPCOMPAT when you call STA objects from ASP.NET.
* Use MTAThread when you call free-threaded objects.
* Avoid thread switches by using Neutral apartment COM components.

 
16. A quick search of Stack Overflow for using IDispatch from C# turns up this post saying what I want is not possible in .NET. So is it possible to use COM from C# .NET 2.0?
The question is that there is an accepted design pattern that I want to use in C#/.NET. It involves launching Internet Explorer out of process, and giving it HTML content, all the while not using temporary files. A rejected design idea is hosting Internet Explorer on a WinForm.
An acceptable alternative is launching the system registered web browser, giving it HTML to display, without using a temporary file.
The stumbling block is continuing to use COM objects in the .NET world. The specific problem involves performing late-binding calls to IDispatch without needing C# 4.0. (i.e. while using .NET 2.0)

17. Which one helps to interact with managed code with com componets?
Common Language Runtime.

 
18. Is managed code bi directional?
yes

 
19. How Call into unmanaged APIs from managed code achieving?
This can be done for both flat APIs (static DLL exports, such as the Win32 API, which is exposed from DLLs like kernel32.dll and user32.dll) and COM APIs (object models such as those exposed by Microsoft® Word, Excel, Internet Explorer, ActiveX® Data Objects (ADO) and so forth). 

 
20. How Expose managed APIs to unmanaged code achieving?
Creating an add-in for a COM-based application like Windows Media® Player or embedding a managed Windows Forms control on an MFC form.

 
21. What is Platform Invoke technology?
The service provided by the CLR interop layer for calling unmanaged flat APIs from managed code. Platform invoke is available in all managed languages.

22. Which is Interop providing?
Common Language Runtime.

 
23. Which are the techniques enabling managed and un managed interactions?
Platform Invoke,COM interop,C++ interop.

 
24. What is COM interop?
The service provided by the CLR interop layer for using COM APIs from managed code, or exposing managed APIs as COM APIs to unmanaged clients. COM interop is available in all managed languages.

 
25. What is ANSI-C++?
ANSI-C++ is the name by which the international ANSI/ISO standard for the C++ language is known. But before this standard was published, C++ was already widely used and therefore there is a lot of code out there written in pre-standard C++. Referring to ANSI-C++ explicitly differenciates it from pre-standard C++ code, which is incompatible in some ways.

 
26. C++ interop some times reffered to as ?
it just works (i j w).

27. How to support a given Office version correctly?
When developing an Office COM add-in supporting several versions of an Office application, the typical approach is to use the interop for the oldest Office version supported by your add-in. Because versions of an Office application are almost 100% backward compatible, the oldest application version supported by your add-in is often thought of as the least common multiple.

 
28. How does an interop assembly version influence the development time?
There is an approach that lets you diminish that time: you can start developing your add-in using interops for the highest version of the Office application. When this is done, you can replace early-binding calls to the features introduced in this Office version with late binding calls and make sure these calls work Then you can replace the interop assemblies referenced by your project with the interop for a previous version of the application and reiterate the procedure. Kind of refactoring. Whether this approach is useful or not, this is you who decides.

 
29. What interop assembly to choose for your add-in project?
When accessing a COM type library in .NET via early binding you need to use a corresponding interop. An interop for an Office application is a .NET assembly providing meta-information about objects, properties, methods, and parameters available in the type library (=object model) of the Office application. Microsoft provides interops for all Office applications starting from Office version 2002. These interops are called Primary Interop Assemblies or PIAs. Add-in Express for Office and .NET provides version-neutral interops that are interops for Office 2000 applications.

 
30. What is c++ interop?
It is a C++-specific feature, which enables flat APIs and COM APIs to be used directly, as they have always been used. This is more powerful than COM interop, but it requires much more care. Make sure that you check the C++ resources before you use this technology.

 
31. What is primary interop?
When accessing a COM type library in .NET via early binding you need to use a corresponding interop. An interop for an Office application is a .NET assembly providing meta-information about objects, properties, methods, and parameters available in the type library (=object model) of the Office application. Microsoft provides interops for all Office applications starting from Office version 2002. These interops are called Primary Interop Assemblies or PIAs.

32. Which are the .NET interoperability?
Interoperability of .NET code with COM components (called as COM interop)
Interoperability of COM components with .NET (called .NET interop)
Interoperability of .NET code with Win32 DLLs (called P/Invoke)

 
33. Differences Between .NET Framework and COM Framework ?
The .NET framework object model and its workings are different from Component Object Model (COM) and its workings. For example, clients of .NET components don't have to worry about the lifetime of the object. Common Language Runtime (CLR) manages things for them. In contrast, clients of COM objects must take care of the lifetime of the object. Similarly, .NET objects live in the memory space that is managed by CLR. CLR can move objects around in the memory for performance reasons and update the references of objects accordingly, but COM object clients have the actual address of the object and depend on the object to stay on the same memory location.
Similarly, .NET runtime provides many new features and constructs to managed components. For example, .NET components can have parameterized constructors, functions of the components can have accessibility attributes (like public, protected, internal, and others) associated with them, and components can also have static methods. Apart from these features, there are many others. These include ones that are not accessible to COM clients because standard implementation of COM does not recognize these features. Therefore .NET runtime must put something in between the two, .NET server and COM client, to act as mediator.

 
34. Explain Interoperability of .NET code with COM components (called as COM interop)?
A .NET component in C# named CManagedServer. This component is in the ManagedServer assembly. Client side, I have created a Visual Basic (VB) 6.0-based client that uses the services of our managed server. 

 
35. What is Runtime Callable Wrapper(RCW)?
The main job of the RCW is to hide all the differences between the two worlds and as such is an object that is created from the managed heap. Only one instance of the RCW is ever created, irrespective of how many .NET clients access it. Once a .NET client object wants to instantiate a COM client, the RCW intervenes and creates the object on your behalf and then manages the lifetime of the COM object. COM objects are reference counted. This means that each client accessing the COM client will increase its reference count by 1 and each release of the reference decreases its reference count by 1. When the reference count becomes 0, the COM client is released. This counting happens by calling the Add and Release methods of the IUnknown interface, an interface that all COM objects may implement. All these intricacies are taken over by the RCW. It is important to remember that .NET is a garbage collected environment. The RCW will be collected when the last client holding a reference to the COM object releases the reference. At this point, the reference count on the COM object becomes 0 and will thus be collected by the operating system while the RCW will be garbage collected by the .NET runtime.

 
36. What is .NET TYPES CALLING Win32 DLLs?
Platform Invoke (PInvoke) services:
The platform invoke services offers a method to call functions that are exported from an unmanaged DLL. The most distinctive use of PInvoke is to allow .Net components to interact with the Win32 API. PInvoke is also used to access functions exports defined in custom DLLs. 

37. Which is the Unmanaged thread's apartment type of Single* component threading model?
STA

 
38.  What is Interop Assemblies?
Interop assemblies are .NET assemblies that act as a bridge between managed and unmanaged code, mapping COM object members to equivalent .NET managed members. Interop assemblies created by Visual Basic .NET handle many of the details of working with COM objects, such as interoperability marshaling.

 
39. What is Interoperability Marshaling?
All .NET applications share a set of common types that allow interoperability of objects, regardless of the programming language used. The parameters and return values of COM objects sometimes use data types that are different than those used in managed code. Interoperability marshaling is the process of packaging parameters and return values into equivalent data types as they move to and from COM objects.

 
40. What are the main attributes to perform COM mapping are?
ComImport - Marks a class as an externally implemented COM class.
Guid – Used to specify a universally unique identifier (UUID) for a class or an interface.
InterfaceType – specifies whether an interface derives from IUnknown or IDispatch.
PreserveSig – specifies whether the native return value should be converted from an HRESULT to a .NET Framework exception.

 
41. What is It Just Works (IJW)?
This feature allows users of Managed Extensions for C++ to directly call libraries within standard Windows DLLs.

42. What is Platform Invoke (P/Invoke)?
To call a function in a standard Windows DLL by using P/Invoke, you must add a declaration to map a callable managed method to the target unmanaged function. In C#, you use the [DllImport] attribute to make a P/Invoke declaration; in Visual Basic .NET, you use the Declare statement.

 
43. What are the main issues that can adversely affect the performance and scalability of application's interop code?
* Marshaling parameter types inefficiently. As interop calls transition between managed code and unmanaged code, parameters that are marshaled inefficiently or unnecessarily waste system processing cycles.
* Not disposing COM objects for server applications under load. The lifetime of a COM object is managed through reference counting. If your Web application calls COM components on a per-request basis under load, then failure to call Marshal.ReleaseComObject at the appropriate time prolongs the lifetime of the COM object. This adversely affects your server's resource (including memory) utilization, particularly if you use single-threaded apartment (STA) COM objects.
* Using chatty interfaces that require excessive round trips. It is inefficient to write managed code against COM objects that expose interfaces requiring multiple round trips to perform a single logical operation. The classic example is a COM object that requires the caller to assign multiple property values before executing a method.
* Not disposing of unmanaged resources in a timely manner. The CLR garbage collector executes an object's Finalize method at a time that is not predictable. If time-critical resources are released in a Finalize method, your code can hold on to these resources much longer than it should.
* Aggressively pinning short-lived objects. If you unnecessarily extend the life of a buffer beyond the duration of the P/Invoke call, you can fragment the managed heap.
* Incurring overhead due to late binding and reflection. Late binding is based on reflection and requires many more processing cycles for a caller to execute a method than does early binding.
* Incurring overhead due to unnecessary thread switching. The majority of COM objects are apartment-threaded and can run only under the COM STA model. Failure to match threading models between the calling thread and the COM object can result in cross-apartment calls. Cross-apartment calls usually require a thread switch, which further degrades performance.
* Overhead of Unicode to ANSI string conversions. Interop calls to older functions in the Win32 API can result in the conversion of string data between the Unicode format used by the CLR and the older ANSI format. This conversion is costly in terms of processing cycles and should be avoided or reduced as much as possible.

 
44. What are the design consideration for interop code is optimized for performance?
* Design chunky interfaces to avoid round trips.
* Reduce round trips with a facade.
* Implement IDisposable if you hold unmanaged resources across client calls.
* Reduce or avoid the use of late binding and reflection.

 
45. Explain Design Chunky Interfaces to Avoid Round Trips?
When you design code that is to be called through P/Invoke or COM interop, design interfaces that reduce the number of calls required to complete a logical unit of work. This guideline is particularly important for interfaces that handle calls to COM components located on a remote server, where the performance impact of using chatty interfaces is significant.

 
46. Explain Reduce Round Trips with a Facade?
Often, we cannot design the interfaces of the unmanaged libraries that you use because they are provided for you. If you must use a preexisting unmanaged library with a chatty interface, consider wrapping the calls in a facade. Implement the facade on the boundary side that exposes the chatty interface. For example, given a chatty Win32 API, you would create a Win32 facade. Creating a .NET facade would still incur the same number of managed/unmanaged boundary crossings. The following is an example of a chatty unmanaged interface wrapped with an unmanaged facade..

47. Explain Implement IDisposable if You Hold Unmanaged Resources Across Client Calls?
Holding shared server resources across remote client calls generally reduces scalability. When you build managed objects, you should acquire and release shared unmanaged resources on a per-request basis whenever possible. The platform can then provide optimizations, such as connection pooling, to reduce the resource-intensive operations for per-request calls.
If you acquire and release resources within a single request, you do not need to explicitly implement IDisposable and provide a Dispose method. However, if you hold on to server resources across client calls, you should implement IDisposable to allow callers to release the resources as soon as they are finished with them.

 
48. Explain Reduce or Avoid the Use of Late Binding and Reflection?
COM objects support two styles of binding: early binding and late binding. You use early binding when you program against the types defined within an interop assembly. You can use late binding to program against a COM object from managed code by using reflection.
To use late binding in C# or C++, you must explicitly program against types defined inside the System.Reflection namespace. In Visual Basic .NET, the compiler adds in automatic support for late binding.
To use late binding from Visual Basic .NET, you must disable the Option Strict compile-time setting and program against the Object type. The following code activates a COM object, by using a string-based ProgID, and calls methods by using late binding.

 
49. Explain Reduce or Avoid the Use of Late Binding and Reflection?
COM objects support two styles of binding: early binding and late binding. You use early binding when you program against the types defined within an interop assembly. You can use late binding to program against a COM object from managed code by using reflection.
To use late binding in C# or C++, you must explicitly program against types defined inside the System.Reflection namespace. In Visual Basic .NET, the compiler adds in automatic support for late binding.
To use late binding from Visual Basic .NET, you must disable the Option Strict compile-time setting and program against the Object type. The following code activates a COM object, by using a string-based ProgID, and calls methods by using late binding.

 
50. What are the guidlines for optimizing code's marshaling performance?
* Explicitly name the target method you call.
* Use blittable types where possible.
* Avoid Unicode to ANSI conversions where possible.
* Use IntPtr for manual marshaling.
* Use [in] and [out] to avoid unnecessary marshaling.
* Avoid aggressive pinning of short-lived objects.

 

 
Subscribe Newsletter
Register to the site for free, and subscribe to the newsletter. You will receive new articles and special content not available elsewhere on the site, right into your e-mail box!