Posted by: Zeeshan Amjad | July 14, 2009

Managed object in Unmanaged class


 

Although historically Visual C++ is used to write unmanaged code, but with the advent of Microsoft .Net now it has dual role. One can write unmanaged as well as managed code with the help of Visual C++. Initially the support of writing managed code in Visual C++ is every cumbersome and one has to write use lots of keywords started with double underscore such as __gcnew, __delegate, etc, but gradually it has very good support in Visual Studio 2008.

The support to write managed code is not limited to keywords only. Previously one has to write code for marshaling the data when passing from unmanaged world to managed world, such as string, but now compiler does it for us behind the scene. Microsoft also provide a wrapper class to make the object of managed class inside the unmanaged class to take advantage of both worlds. It is possible to make a program that uses MFC, ATL, STL as well as .Net class library at the same time. Managed support is not limited to .Net class library, but one can make a user interface in managed code too using either Windows Form or Windows Presentation Foundation WPF.

Visual C++ considers .Net a first class citizen and you can do almost everything in Visual C++. In addition Visual C++ has .Net version of STL known as CLR/STL very specific to C++ and not available to other programming languages.

Sometimes we may need to create a managed object inside the unmanaged class. Here is one simple example of this.

  1: using namespace System;
  2: 
  3: class Employee
  4: {
  5: private:
  6: 	String^ firstName;
  7: 	String^ lastName;
  8: 
  9: public:
 10: 	Employee()
 11: 	{
 12: 		firstName = gcnew String("");
 13: 		lastName = gcnew String("");
 14: 	}
 15: 
 16: 	Employee(String^ firstName, String^ lastName)
 17: 	{
 18: 		this->firstName = firstName;
 19: 		this->lastName = lastName;
 20: 	}
 21: 
 22: 	void Setname(String^ firstName, String^ lastName)
 23: 	{
 24: 		this->firstName = firstName;
 25: 		this->lastName = lastName;
 26: 	}
 27: };
 28: 

If we try to compile this code then we will get compilation error. We can’t create a managed object inside the unmanaged class. One possible solution might be to make this class managed class by introducing “ref” keyword before “class” keyword. But this solution is not applicable in all cases. For example if there is one MFC based application and we want to store some managed object in the application class then we can’t change the application class to managed class. Here is a code to demonstrate this.

 

  1: #include <gcroot.h>
  2: using namespace System;
  3: 
  4: class Employee
  5: {
  6: private:
  7: 	gcroot<String^> firstName;
  8: 	gcroot<String^> lastName;
  9: 
 10: public:
 11: 	Employee()
 12: 	{
 13: 		firstName = gcnew String("");
 14: 		lastName = gcnew String("");
 15: 	}
 16: 
 17: 	Employee(String^ firstName, String^ lastName)
 18: 	{
 19: 		this->firstName = firstName;
 20: 		this->lastName = lastName;
 21: 	}
 22: 
 23: 	void Setname(String^ firstName, String^ lastName)
 24: 	{
 25: 		this->firstName = firstName;
 26: 		this->lastName = lastName;
 27: 	}
 28: };
 29: 
Advertisements

Responses

  1. Great! Works! Thank you so much!

    • Thanks to like it.
      Regards
      Zeeshan Amjad


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: