Interfacing VC++ with C# through COM - A Guide

Introduction

This tutorial helps you to identify a way of interfacing components written in VC++ with a Client application in C#. Do any of the scenarios below apply to you ? 1: You've done most of your coding in VC++ and now the client wants it in C# 2: You are quite strong in VC++ , but you are a beginner in C#. Yet you have been assigned some complex task in C#. 3: You've got an existing codebase written in VC++ and it has been thoroughly tested. You want it changed to C# now. If you've just told yourself "YEAH. THATS MY CURRENT PROBLEM" to any of the scenarios above, then this tutorial is for you.It would help you to create your background code ( Main core code ) in VC++ itself and then you can just create the UI in C# ( A Windows Form ) and associate the methods of your class in it.


PART 1 : Writing your own generalized class


	 1)	Write generic classes as we always do in VC++

	 2)	Put the class inside a namespace.

		Example
 
		namespace TestModule
		{
			Class CMyClass
			{
			  // blah blah blah here
			};
		}

	 3)	Before each function definition, add the keywords __declspec(dllexport)
	
		Example

		 __declspec(dllexport) BOOL MyFunction()

PART 2 : Adding the class to an ATL project to generate the Library


	 4)	Create a new project and select "ATL Project" and click "Finish".
		For example, it is "MyComponent".

	 5) 	In the class view , right click the project and select "Add Class"
		and in the list displayed, select "ATL" and in the new list 
		displayed, select "ATL Simple Object"

	 6)	Type the short name of the class we want in the displayed dialog ( Eg. Test )
		box and click "Finish"

	 7)	Add the header file and .CPP file of your generalized class. Here it is 
		MyClass.h and MyClass.cpp

	 8)	In the Interface CPP file, use the keywords "using namespace "
		and create a variable for your class.

		Example

		using namespace TestModule
		CMyClass m_MyClass;

	 9)	Right click the Interface added ( Eg. ITest ) and select "Add Method"
		Type the function name and the parameters if required. Then click 
		"Finish". For example it is , CallMyFunction.

	10)	In the CallMyFunction function, use your member variable of your class
		to call its functions.

		Example

		m_MyClass.MyFunction();

	11)	Compile the DLL. You will get a .DLL file and a .LIB file as output.
		In this example, you would get "MyComponent.dll" and "MyComponent.lib"

PART 3 : Using the obtained DLL in any other application ( For example , in C# )


	12)	Copy the obtained DLL and Library to your application folder.

	13)	Right click on the "References" tab in the Solution explorer and select
		"Add Reference". 

		If your DLL had compiled without errors, it would have registered itself
		in the available components list. To access it, select the "COM" tab of 
		the "Add Reference" dialog. Select your component from the list. In this 
		case, it would be registered as "MyComponent Type Library".
		
		Click the "Select" button. This would add it to your project.

	14)	Before the namespace declaration of your form.cs , use the keywords
		using ;

		Example

		using MyComponent;

	15)	In your class's private area, declare a private member variable for
		your component. 

		Example

		private MyComponent.CIMyClass TestClass = null;

	16)	In the "Windows Form Designer generated code" area, go to the 
		"InitializeComponent" function and there, Initialize your 
		component variable.
		
		Example

		this.TestClass = new MyComponent.CIMyClass();

	17)	To use the various other functions in your Component, For example
		the MyFunction call in the DLL, directly use it within your 
		event functions.

		Example,

		private void button1_Click(object sender, System.EventArgs e)
		{
			this.TestClass.MyFunction();
		}

PART 4 : Enjoy the fruits of your labour


	Sit back, Relax and watch the C# application happily using the functions of your
	VC++ component. Enjoy.

Conclusion :

THATS ALL FOLKS. The end of a simple tutorial for interfacing VC++ with C#.All Luck.

Hosted by www.Geocities.ws

tutorials /
VC++ & C#
home /
Hosted by www.Geocities.ws

1