C# : Creating a COM component enabled application
Hi Friends, This application shows how to create application with COM component enabled. This application contains one C# project and one C# class liberary and one VB project. The HelloCom C# project accesses both C# and VB dlls.
Follow below steps to create COM component enabled application example:
Remember
to set below attribute in all the library and project in AssemblyInfo.cs
[assembly: ComVisible(true)]
Create C# project class library: Give its name to CSDLL:
----------------------------------------------------------
put below code:
using System;
using System.Collections.Generic;
using System.Text;
namespace MyParcelTracker
{
public class ParcelTracker
{
public string GetDateTime()
{
return DateTime.Now.ToString();
}
public virtual string GetOrderStatus(int ordno)
{
return "Order new Dispatched";
}
}
}
Create VB project
class library: Give its name to VBDLL:
------------------------------------------------------------------------
put
below code:
Imports System
Imports MyParcelTracker
Public
Class VBParcelTracker
Inherits ParcelTracker
Public
Overrides Function GetOrderStatus(ByVal ordno As Integer) As String
Return
MyBase.GetOrderStatus(ordno)
End Function
End Class
Create
a C# concole application which uses both the class library : give its name HelloCOM
-------------------------------------------------------------------------------------------------------------------------
First
of all compile both the above libraries and add it as a referece here.
put below code:
using System; using System.Collections.Generic; using System.Text; using MyParcelTracker; using VBDll; //this is the implicit name space created while creating vb.net application class HelloCom{ public static void Main(string[] args) {System.Console.WriteLine("Hello world"); int orderno = 1234; ParcelTracker pt = new ParcelTracker(); Console.WriteLine("From new C# " + pt.GetDateTime()); Console.WriteLine("From new C# " + pt.GetOrderStatus(orderno));
VBParcelTracker vpt = new VBParcelTracker(); Console.WriteLine("From new VB " + vpt.GetDateTime()); Console.WriteLine("From new VB " + vpt.GetOrderStatus(orderno)); Console.Read();} }
Your final program structure will look like below:
Run the application and Enjoy!!!!!!!!!!You have created your first COM visible application.
Your questions are most welcomed.
Megha
By Perry Popularity (2153 Views)