COM components can also be generated under .net environment using Visual studio and languages such as C#.
A COM component is essentially class library having an interface declared as below.
and class implementation as below. TheComvisible attribute makes the class visible to COM based clients and the guid serves as clsid. The class name serves as the second parts of the program id. for example, servercalc.calucalator.[ComVisible(true)] [Guid("E3D02A14-ED36-471E-BE32-2D65F1BC97DA")] public interface ICalucalator { int add(int op1, int op2); }
[ComVisible(true)]
[Guid("0AD70785-5539-4EE0-83D5-37A62CF5318F")]
public class Calucalator : ICalucalator
{
public int add(int op1, int op2)
{
return op1 + op2;
}
}
AssemblyInfo.cs should look as below. The Comvisible attribute makes the class visible to COM based clients and the guid identifies the typelib. A typelib contains metadata about the classes, interfaces, methods within the assembly. It will be used com based clients to make calls.the project file .csproj should look as below. The assembly name serves as the first part of the program id. for example, servercalc.calucalator. The registerforInterop executes RegAsm for the assembly making it visible for COM clients.// Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. [assembly: ComVisible(true)] // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("8CF1D751-666D-40C6-A741-0C14B4727A9E")]
<AssemblyName>CalcServer</AssemblyName>
<RegisterForComInterop>true</RegisterForComInterop>
Note:- All the guids must be unique. if the same project is cloned they must be replaced apart from the code
Registration
The assembly must be registered. it's done Regasm tool as below. Note that a typelibrary is also generated.
regasm D:\Github\TechBlog\COM+\Managed\server\bin\CalcServer.dll /tlb:D:\Github\TechBlog\COM+\Managed\server\bin\CalcServer.tlb
Com callable wrappers are used to consume managed com components from native clients. The process involves creating the type library from the managed dll by using tlbexp.exe and import it. Later native clients query type library for published interfaces to make calls.
Serviced Component
COM+ offers services such as object pooling. A Serviced component can take advantage of this by deriving from System.EnterpriseServices.ServicedComponent class.
public class Calucalator : System.EnterpriseServices.ServicedComponent, ICalucalator
This enable overriding the default methods such as object pooling with custom code.
Scripting host Client
The component can be consumed by a scripting host client such as cscript or wscript as below. Save the C# client
Unlike unmanaged com component, managed com component cannot be consumed by importing typelibrary in a managed client. It needs to be hard coded as below.
Debugging scripting component
It's possible to debug Managed Com Component at run time using Visual Studio as below.
1. Start the client
2. Load the symbols for the managed com component
No comments:
Post a Comment