- .NET Interoperability
- Abstract
- Prerequisite
- COM
- .NET Interoperability
- Runtime Callable Wrapper
- COM Callable Wrapper
- ActiveX
- Conclusion
Abstract
This article begins by taking a brief backward glimpse at COM then compares it with the way that components interact in .NET and finally it takes a look at the tool provided by Microsoft to let these two technologies coordinate. Essentially, COM is the antecedent technology to . NET. COM defines a component prototypical where components can be written in dissimilar programming languages. In addition to that, they can be cast off inside a process, across a process or across the network (DCOM). But the COM technology became more and more complicated in terms of implementation and turned out not to be extensible enough. However, .NET justifies similar goals as COM but introduces new conceptions to make the job easier.
Prerequisites
In order to implement COM components interoperability with the .NET Framework, the following are required:
- Visual Studio 6 with VC++ (optional)
- Visual Studio 2010 with VC++ .Net features
COM
Before delving into .NET-COM interoperability, it is important to be aware of the legacy Component Object Model (COM) itself. COM was Microsoft's first attempt at creating a language-independent standard for programs. The idea was that interfaces between components would be defined or invoked according to a binary standard. This would have inferred that you could invoke a VC++ component from a VB application and vice versa. COM components implement one or more interfaces that define the various methods that an application may invoke during marshaling.
COM in Practice
To see how a .NET application can use a legacy COM component, you first need to create a COM component using VC++ because creating a COM component is not possible with C# or VB.Net.
- To create a COM component with ATL and C++, create a new ATL project. Here set the name to comServerTest and with the application setting, select Dynamic Linking Library and finally click Finish.

- The first thing you need to define is a class, mathLib, then place the methods Addition() and Square() definitions in the ImathLib interface. Once we have done with this, look at the comServerTest.idl file that shows all the method definitions as in the following.
// comServerTest.idl: IDL source for comServerTest // This file will be processed by the MIDL tool to // produce the type library (comServerTest.tlb) and marshalling code. import "oaidl.idl"; import "ocidl.idl"; [ object, uuid(DFC4EDDD-1115-4B36-90A3-15F547018FE8), dual, nonextensible, pointer_default(unique) ] interface ImathLib : IDispatch{ [id(1)] HRESULT Addition([in] LONG val1, [in] LONG val2, [out,retval] LONG* val); [id(2)] HRESULT Square([in] LONG val1, [out,retval] LONG* val); }; [ uuid(F2DBF0C9-8136-4F38-BED2-333A365ED980), version(1.0), ] library comServerTestLib { importlib("stdole2.tlb"); [ uuid(C670DD23-8B50-445D-9E53-17D6248EF1DA) ] coclass mathLib { [default] interface ImathLib; }; }; - Now implement the business logic of those methods into the mathLib.cpp class as.
STDMETHODIMP CmathLib::Addition(LONG val1, LONG val2, LONG* val) { *val = val1 + val2; return S_OK; } STDMETHODIMP CmathLib::Square(LONG val1, LONG* val) { *val = val1 * val1; return S_OK; } - Finally, build the component. The build process also configures the component in the registry.
- If you are developing your client .NET application on the same machine, you don't need to manually register it. However, if you are working entirely on a new machine, you need to register this component with the regsvr32.exe utility as in the following.

.NET Interoperability
The mechanism where data is passed from COM components to .NET and vice-versa is called marshaling. The foremost concern during this process is the conversion of passed data types because both technologies have different data type definition schemes. But .NET and COM have a common representation for some data types such as int, long, short, and byte where no conversion is required during marshaling called Blittable data types.









Sam HobbsPosted Nov 5, 2014, 4:22 PM
People might be interested in knowing that Object Linking and Embedding (OLE) was the beginning that evolved into COM. Along the way, other terms (such as Automation) were also used to refer to the same or related technology. Thank you for making it totally clear that the main feature of ActiveX controls is the UI.