Wednesday, August 26, 2009

How to use COM object in C#.NET Visual studio 2008

Use COM object in Your project?

What COM? legacy com .com+...Phew!!!!

Its easy to use COM object in your project.

If you have a dll which says it cannot be added as reference to your Project...So Register it using regsvr32 dllname.dll

Goto start->Run->
regsvr32 dllname.dll

Once you register the com object dll ,now you can add as a reference by going to the COM tab in the Add reference Dialog box.
Right click on Reference->Add Refernce->COM tab->search for the dll name which u registered

Thats it you added com components to your project.Now you can access the COM object by adding


Using comClassName; // Add this mostly it will same as the dll name

and instantiate an object for that

comClassName obj=new comClassName;
string returnsValofComFunc =obj.funcInComDLL();


Thats it now you can use the functions are properties inside the com component dll directly instantiating the class.

What is instantiating?
className obj =new className(); // creating an Object for a Class is called instantiating.

What is Class?
Skeleton structure of a component


Class Car
{
int carWheels;
int carDoors;
string engine;

public int func()
{
carWheels=4;
return carWheels;
}
}

//Now if you instantiate this class you can get the class Car details
//Do you want to findout how many wheels in the car

Car carObj=new Car();
int noWheels=carObj.func();

No comments:

Post a Comment