Welcome to weblogs.com.pk Sign in | Join | Help

Creating COM-Aware Component in C#

If you want to create a COM in C# (.NET) you might have to follow these steps.

 

  1. Create a class project.
  2. Create couple of GUIDs, you will be needing atleast two, 1 for interface and other for implementation.
  3. Create an Interface class. Decorate it using attributes in System.Runtime.InteropServices namespace. (Listing 1) You will also need to decorate the function definitions.
  4. Create an implementation class. Decorate it with attributes. (Listing 2)
  5. Register the assembly using regasm e-g regasm CRMFramework.dll /tlb CRMFramework.tlb /codebase PathToYourAssembly
    1. PathToYourAssembly includes the dll, eg c:\CRMFramework.dll
    2. You might need to give appropriate NTFS permission to the assembly (and its referenced assemblies)
    3. 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

Published Monday, March 14, 2005 5:30 PM by khurram
Filed under: ,

Comments

No Comments

New Comments to this post are disabled