Classes written in C # using C + +-encapsulated with managed C + + [go]

Source: Internet
Author: User

Now the application development under Windows, VS. NET occupies the majority of the share. So many people who used to develop VC + + have turned to use more powerful vs.net. In this case, there are many developers who are faced with the problem of how to use C + + to develop good classes in C #. Here is a complete example of how to encapsulate a C + + class in Managed C + + for use in C #.

For example, there is now a DLL written by C + + with the project named Nativecppdll, which outputs a CPerson class. Here is the specific code:
  1. NativeCppDll.h
  2. #pragma once
  3. #ifndef Lx_dll_class_exports
  4. #define Lx_dll_class __declspec (dllexport)
  5. #else
  6. #define Lx_dll_class __declspec (dllimport)
  7. #endif
  8. class Lx_dll_class CPerson
  9. {
  10. Public:
  11. CPerson ();
  12. CPerson (const wchar_t *pname, const wchar_t csex, int iage);
  13. void SetName (const wchar_t *pname);
  14. wchar_t * GetName ();
  15. void Setsex (const wchar_t csex);
  16. wchar_t Getsex ();
  17. void Setage (int iage);
  18. int Getage ();
  19. wchar_t * GetLastError ();
  20. Private:
  21. wchar_t m_szname[128];
  22. wchar_t M_csex;
  23. int m_iage;
  24. wchar_t m_szlasterror[128];
  25. void ShowError ();
  26. };
  27. NativeCppDll.cpp
  28. #include "stdafx.h"
  29. #include "NativeCppDll.h"
  30. #include
  31. #include
  32. using namespace std;
  33. Cperson::cperson ()
  34. {
  35. wcscpy_s (M_szname, _t ("No Name"));
  36. M_csex = ' N ';
  37. m_iage = 0;
  38. wcscpy_s (M_szlasterror, _t ("No Error"));
  39. }
  40. Cperson::cperson (const wchar_t *pname, const wchar_t csex, int iage)
  41. {
  42. wcscpy_s (M_szlasterror, _t ("No Error"));
  43. SetName (PName);
  44. Setsex (Csex);
  45. Setage (iage);
  46. }
  47. void Cperson::setname (const wchar_t *pname)
  48. {
  49. if ((PName = = NULL) | | (Wcslen (pName) = = 0) | | (Wcslen (PName) > 127))
  50. {
  51. wcscpy_s (M_szname, _t ("No Name"));
  52. wcscpy_s (M_szlasterror, _t ("The length of the input name is out of range."));
  53. ShowError ();
  54. return;
  55. }
  56. wcscpy_s (M_szname, pName);
  57. }
  58. wchar_t * Cperson::getname ()
  59. {
  60. return M_szname;
  61. }
  62. void Cperson::setsex (const wchar_t csex)
  63. {
  64. if (Csex! = ' F ') && (csex! = ' m ') && (csex! = ' m ') && (csex! = ' f ') ))
  65. {
  66. M_csex = ' N ';
  67. wcscpy_s (M_szlasterror, _t ("The input sex is out of [f/m]."));
  68. ShowError ();
  69. return;
  70. }
  71. M_csex = Csex;
  72. }
  73. wchar_t Cperson::getsex ()
  74. {
  75. return M_csex;
  76. }
  77. void Cperson::setage (int iage)
  78. {
  79. if ((iage < 0) | | (Iage > 150))
  80. {
  81. m_iage = 0;
  82. wcscpy_s (M_szlasterror, _t ("The input age was out ofrange."));
  83. ShowError ();
  84. return;
  85. }
  86. M_iage = iage;
  87. }
  88. int Cperson::getage ()
  89. {
  90. return m_iage;
  91. }
  92. wchar_t * Cperson::getlasterror ()
  93. {
  94. return M_szlasterror;
  95. }
  96. void Cperson::showerror ()
  97. {
  98. Cerr << m_szlasterror << Endl;
  99. }

This is a typical DLL developed by C + +, outputting a complete C + + class. What if you need to develop a C # project now and use the C + + class CPerson output from this DLL? For this example, class CPerson is very small and can be used to re-write a class like this C + + class in C #. However, if the required C + + classes are large, or many times, rewriting works will be very large. And it's not reusing existing code, wasting existing resources, and creating time-consuming and laborious development.
Of course, there are ways to solve this problem. That is to encapsulate the C + + class with managed C + + and then provide it to C # for use. The following code is used to describe in detail how to encapsulate the C + + class with managed C + +.
First, create a managed C + + DLL project Managecppdll, and add the following code inside:

  1. ManageCppDll.h
  2. #pragma once
  3. #define Lx_dll_class_exports
  4. #include ". /nativecppdll/nativecppdll.h "
  5. using namespace System;
  6. namespace Managecppdll
  7. {
  8. Public ref class person
  9. {
  10. //Wrapper public member functions for all class CPerson
  11. Public:
  12. Person ();
  13. Person (String ^ strName, Char csex, int iage);
  14. ~person ();
  15. Property String ^ Name
  16. {
  17. void Set (String ^ strName);
  18. String ^ get ();
  19. }
  20. Property Char Sex
  21. {
  22. void Set (Char csex);
  23. Char get ();
  24. }
  25. Property int Age
  26. {
  27. void Set (int iage);
  28. int get ();
  29. }
  30. String ^ GetLastError ();
  31. Private:
  32. //Class CPerson pointer, used to invoke the member function of class CPerson
  33. CPerson *m_pimp;
  34. };
  35. };

As you can see from this header file, this is the wrapper for C + + class CPerson. All public member functions of the class person are the same as the C + + class CPerson, except that the parameters and return values of the member functions are changed to the managed C + + type, which is the first condition for the class person to be used in C #. Of course, only the public member functions need to be encapsulated, and no encapsulation is necessary for the protection of member functions and private member functions.
The class person has only one private member variable: A pointer to a class CPerson. The implementation of all member functions of the class person is accomplished by invoking the corresponding member function of the class CPerson by this cperson pointer.
Here is the specific implementation code:

  1. ManageCppDll.cpp
  2. #include "stdafx.h"
  3. #include "ManageCppDll.h"
  4. #include
  5. namespace Managecppdll
  6. {
  7. //Create an object of class CPerson in the constructor and destroy the object in the destructor
  8. //All member function implementations are implemented by pointer M_pimp calling the corresponding member function of class CPerson
  9. Person::P Erson ()
  10. {
  11. M_pimp = new CPerson ();
  12. }
  13. Person::P Erson (String ^ strName, Char csex, int iage)
  14. {
  15. //Convert string to a pointer that C + + can recognize
  16. pin_ptr<</span>const wchar_t> wcname = PtrToStringChars (strName);
  17. M_pimp = new CPerson (Wcname, Csex, iage);
  18. }
  19. Person::~person ()
  20. {
  21. //Delete the CPerson object in the destructor
  22. Delete m_pimp;
  23. }
  24. void Person::name::set (String ^ strName)
  25. {
  26. pin_ptr<</span>const wchar_t> wcname = PtrToStringChars (strName);
  27. M_pimp->setname (Wcname);
  28. }
  29. String ^ Person::name::get ()
  30. {
  31. return gcnew String (M_pimp->getname ());
  32. }
  33. void Person::sex::set (Char csex)
  34. {
  35. M_pimp->setsex (Csex);
  36. }
  37. Char Person::sex::get ()
  38. {
  39. return m_pimp->getsex ();
  40. }
  41. void Person::age::set (int iage)
  42. {
  43. M_pimp->setage (iage);
  44. }
  45. int Person::age::get ()
  46. {
  47. return m_pimp->getage ();
  48. }
  49. String ^ Person::getlasterror ()
  50. {
  51. return gcnew String (M_pimp->getlasterror ());
  52. }
  53. };

If you want to use class person in C #, first add a reference to ManageCppDll.dll, and then you can use the class person as you would with a normal C # class. such as the following code:

    1. using Managecppdll;
    2. Person person = New person ();
    3. Person. Name = "Starlee";
    4. Person. Sex = ' M ';
    5. Person. Age = 28;

Familiar with the design pattern to see the above code will certainly find that this design is the same as bridge mode. In fact, the above method is also a bridge pattern, managed C + + serves as a bridge for C # to use classes developed in C + +. In addition, this form can also be understood as adapter mode, managed C + + class person is the C + + class CPerson an adapter. With this bridge, it is easy to reuse classes that were previously developed in C + +, so that these C + + classes continue to play their role in C #, making development more effective.

Blog Source: http://blog.csdn.net/starlee/article/details/2864588

Classes written in C # using C + +-encapsulated with managed C + + [go]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.