在C#中使用C++编写的类

现在在Windows下的应用程序开发,VS.Net占据了绝大多数的份额。因此很多以前搞VC++开发的人都转向用更强大的VS.Net。在这种情况下,有很多开发人员就面临了如何在C#中使用C++开发好的类的问题。下面就用一个完整的实例来详细说明怎样用托管C++封装一个C++类以提供给C#使用。
比如,现在有一个工程名为NativeCppDll的由C++编写的DLL,里面输出了一个CPerson类。下面是具体的代码:

  1. //NativeCppDll.h
  2. #pragmaonce
  3. #ifndefLX_DLL_CLASS_EXPORTS
  4. #defineLX_DLL_CLASS__declspec(dllexport)
  5. #else
  6. #defineLX_DLL_CLASS__declspec(dllimport)
  7. #endif
  8. classLX_DLL_CLASSCPerson
  9. {
  10. public:
  11. CPerson();
  12. CPerson(constwchar_t*pName,constwchar_tcSex,intiAge);
  13. voidSetName(constwchar_t*pName);
  14. wchar_t*GetName();
  15. voidSetSex(constwchar_tcSex);
  16. wchar_tGetSex();
  17. voidSetAge(intiAge);
  18. intGetAge();
  19. wchar_t*GetLastError();
  20. private:
  21. wchar_tm_szName[128];
  22. wchar_tm_cSex;
  23. intm_iAge;
  24. wchar_tm_szLastError[128];
  25. voidShowError();
  26. };
  27. //NativeCppDll.cpp
  28. #include"stdafx.h"
  29. #include"NativeCppDll.h"
  30. #include<iostream>
  31. #include<tchar.h>
  32. usingnamespacestd;
  33. CPerson::CPerson()
  34. {
  35. wcscpy_s(m_szName,_T("NoName"));
  36. m_cSex='N';
  37. m_iAge=0;
  38. wcscpy_s(m_szLastError,_T("NoError"));
  39. }
  40. CPerson::CPerson(constwchar_t*pName,constwchar_tcSex,intiAge)
  41. {
  42. wcscpy_s(m_szLastError,_T("NoError"));
  43. SetName(pName);
  44. SetSex(cSex);
  45. SetAge(iAge);
  46. }
  47. voidCPerson::SetName(constwchar_t*pName)
  48. {
  49. if((pName==NULL)||(wcslen(pName)==0)||(wcslen(pName)>127))
  50. {
  51. wcscpy_s(m_szName,_T("NoName"));
  52. wcscpy_s(m_szLastError,_T("Thelengthoftheinputnameisoutofrange."));
  53. ShowError();
  54. return;
  55. }
  56. wcscpy_s(m_szName,pName);
  57. }
  58. wchar_t*CPerson::GetName()
  59. {
  60. returnm_szName;
  61. }
  62. voidCPerson::SetSex(constwchar_tcSex)
  63. {
  64. if((cSex!='F')&&(cSex!='M')&&(cSex!='m')&&(cSex!='f'))
  65. {
  66. m_cSex='N';
  67. wcscpy_s(m_szLastError,_T("Theinputsexisoutof[F/M]."));
  68. ShowError();
  69. return;
  70. }
  71. m_cSex=cSex;
  72. }
  73. wchar_tCPerson::GetSex()
  74. {
  75. returnm_cSex;
  76. }
  77. voidCPerson::SetAge(intiAge)
  78. {
  79. if((iAge<0)||(iAge>150))
  80. {
  81. m_iAge=0;
  82. wcscpy_s(m_szLastError,_T("Theinputageisoutofrange."));
  83. ShowError();
  84. return;
  85. }
  86. m_iAge=iAge;
  87. }
  88. intCPerson::GetAge()
  89. {
  90. returnm_iAge;
  91. }
  92. wchar_t*CPerson::GetLastError()
  93. {
  94. returnm_szLastError;
  95. }
  96. voidCPerson::ShowError()
  97. {
  98. cerr<<m_szLastError<<endl;
  99. }

这是一个很典型的由C++开发的DLL,输出一个完整的C++类。如果现在要求开发一个C#工程,需要用到这个DLL中输出的C++类CPerson,该怎么办呢?针对这个例子来说,类CPerson非常小,可以用C#重新写一个跟这个C++类一样的类。可是,如果需要的C++类很大,或者很多的时候,重写工程将非常庞大。而且这样没有对现有的代码进行重用,浪费了现有资源,开发起来费时费力。
当然,还是有方法解决这个问题的。那就是用托管C++将C++类给封装一下,然后再提供给C#来使用。下面就用代码来详细说明怎样用托管C++来封装上面的那个C++类。
首先,要创建一个托管C++的DLL工程ManageCppDll,然后在里面添加下面的代码:

  1. //ManageCppDll.h
  2. #pragmaonce
  3. #defineLX_DLL_CLASS_EXPORTS
  4. #include"../NativeCppDll/NativeCppDll.h"
  5. usingnamespaceSystem;
  6. namespaceManageCppDll
  7. {
  8. publicrefclassPerson
  9. {
  10. //包装所有类CPerson的公有成员函数
  11. public:
  12. Person();
  13. Person(String^strName,CharcSex,intiAge);
  14. ~Person();
  15. propertyString^Name
  16. {
  17. voidset(String^strName);
  18. String^get();
  19. }
  20. propertyCharSex
  21. {
  22. voidset(CharcSex);
  23. Charget();
  24. }
  25. propertyintAge
  26. {
  27. voidset(intiAge);
  28. intget();
  29. }
  30. String^GetLastError();
  31. private:
  32. //类CPerson的指针,用来调用类CPerson的成员函数
  33. CPerson*m_pImp;
  34. };
  35. };

从这个头文件就能看出来,这是对C++类CPerson的包装。类Person的所有公有成员函数都跟C++类CPerson一样,只不过成员函数的参数和返回值就改成了托管C++的类型,这也是让类Person能在C#中使用的首要条件。当然只需要对公有成员函数进行封装,对于保护成员函数和私有成员函数则不必做任何封装。
类Person仅有一个私有的成员变量:一个类CPerson的指针。而类Person的所有成员函数的实现都是靠这个CPerson指针来调用类CPerson的相应成员函数来实现。
下面是具体的实现代码:

  1. //ManageCppDll.cpp
  2. #include"stdafx.h"
  3. #include"ManageCppDll.h"
  4. #include<vcclr.h>
  5. namespaceManageCppDll
  6. {
  7. //在构造函数中创建类CPerson的对象并在析构函数中将该对象销毁
  8. //所有的成员函数实现都是通过指针m_pImp调用类CPerson的相应成员函数实现
  9. Person::Person()
  10. {
  11. m_pImp=newCPerson();
  12. }
  13. Person::Person(String^strName,CharcSex,intiAge)
  14. {
  15. //将string转换成C++能识别的指针
  16. pin_ptr<constwchar_t>wcName=PtrToStringChars(strName);
  17. m_pImp=newCPerson(wcName,cSex,iAge);
  18. }
  19. Person::~Person()
  20. {
  21. //在析构函数中删除CPerson对象
  22. deletem_pImp;
  23. }
  24. voidPerson::Name::set(String^strName)
  25. {
  26. pin_ptr<constwchar_t>wcName=PtrToStringChars(strName);
  27. m_pImp->SetName(wcName);
  28. }
  29. String^Person::Name::get()
  30. {
  31. returngcnewString(m_pImp->GetName());
  32. }
  33. voidPerson::Sex::set(CharcSex)
  34. {
  35. m_pImp->SetSex(cSex);
  36. }
  37. CharPerson::Sex::get()
  38. {
  39. returnm_pImp->GetSex();
  40. }
  41. voidPerson::Age::set(intiAge)
  42. {
  43. m_pImp->SetAge(iAge);
  44. }
  45. intPerson::Age::get()
  46. {
  47. returnm_pImp->GetAge();
  48. }
  49. String^Person::GetLastError()
  50. {
  51. returngcnewString(m_pImp->GetLastError());
  52. }
  53. };

如果要在C#中使用类Person,首先要添加对ManageCppDll.dll的引用,然后就可以像用普通的C#类一样的使用类Person了。比如下面这样的代码:

  1. usingManageCppDll;
  2. Personperson=newPerson();
  3. person.Name="StarLee";
  4. person.Sex='M';
  5. person.Age=28;

熟悉设计模式的看了上面的代码肯定会发现,这样的设计跟BRIDGE模式如出一辙。其实,上面的方法也算是一种BRIDGE模式,由托管C++充当了C#中使用用C++开发的类的桥梁。另外,这种形式也可以理解为ADAPTER模式,托管C++类Person就是C++类CPerson的一个适配器。通过这个桥梁,可以很容易的重用以前用C++开发的类,让这些C++类继续在C#中发挥它们的效用,让开发变得事半功倍。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值