Creating COM-Aware Component in C#
If you want to create a COM in C# (.NET) you might have to follow these steps.
- Create a class project.
- Create couple of GUIDs, you will be needing atleast two, 1 for interface and other for implementation.
- Create an Interface class. Decorate it using attributes in System.Runtime.InteropServices namespace. (Listing 1) You will also need to decorate the function definitions.
- Create an implementation class. Decorate it with attributes. (Listing 2)
- Register the assembly using regasm e-g regasm CRMFramework.dll /tlb CRMFramework.tlb /codebase PathToYourAssembly
- PathToYourAssembly includes the dll, eg c:\CRMFramework.dll
- You might need to give appropriate NTFS permission to the assembly (and its referenced assemblies)
- You may sign your assembly with strong name and register in GAC, in this case you will not need to give codebase option
[System.Runtime.InteropServices.Guid("AB22B090-46F6-4e6e-BF0B-1C3575CD5283")]
[System.Runtime.InteropServices.InterfaceType(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch)]
public interface ICustomers
{
[System.Runtime.InteropServices.DispId(1)]
ADODB.Recordset GetCustomersFromLastMonth(int thresholdAmount);
}
Listing 1
[System.Runtime.InteropServices.Guid("30CCD71B-1D44-45a5-A8EB-22E815B59C46")]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ProgId("CRMFramework.Customers")]
public class Customers : ICustomers
{
public Customers() {}
#region ICustomers Members
public ADODB.Recordset GetCustomersFromLastMonth(int thresholdAmount)
{
//Implementation
}
#endregion
}
Listing 2