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

在那篇《在C#中使用C++编写的类》中我介绍了如何在C#中使用C++编写的类。可是由于C#在用户界面设计、数据库存储和XML文件读取等方面的优势,有时候也会出现要在C++中使用C#编写的类的情况。下面就用一个完整的实例来说明怎样在C++中使用C#编写的类。
    比如说,现在有一个用C#编写的DLL工程CsharpDll里面有一个Person类:

  1. // Person.cs
  2. using System;
  3. namespace CsharpDll
  4. {
  5. public class Person
  6. {
  7. public Person()
  8. {
  9. Name = "No Name";
  10. Sex = ‘N‘;
  11. Age = 0;
  12. m_strLastError = "No Error";
  13. }
  14. public Person(string strName, char cSex, int iAge)
  15. {
  16. m_strLastError = "No Error";
  17. Name = strName;
  18. Sex = cSex;
  19. Age = iAge;
  20. }
  21. public string Name
  22. {
  23. get
  24. {
  25. return m_strName;
  26. }
  27. set
  28. {
  29. if ((String.IsNullOrEmpty(value)) || (value.Length > 127))
  30. {
  31. m_strName = "No Name";
  32. m_strLastError = "The length of the input name is out of range.";
  33. return;
  34. }
  35. m_strName = value;
  36. }
  37. }
  38. public char Sex
  39. {
  40. get
  41. {
  42. return m_cSex;
  43. }
  44. set
  45. {
  46. if ((value != ‘F‘) && (value != ‘M‘) && (value != ‘m‘) && (value != ‘f‘))
  47. {
  48. m_cSex = ‘N‘;
  49. m_strLastError = "The input sex is out of [F/M].";
  50. return;
  51. }
  52. m_cSex = value;
  53. }
  54. }
  55. public int Age
  56. {
  57. get
  58. {
  59. return m_iAge;
  60. }
  61. set
  62. {
  63. if ((value < 0) || (value > 150))
  64. {
  65. m_iAge = 0;
  66. m_strLastError = "The input age is out of range.";
  67. return;
  68. }
  69. m_iAge = value;
  70. }
  71. }
  72. public string LastError
  73. {
  74. get
  75. {
  76. return m_strLastError;
  77. }
  78. }
  79. private string m_strName;
  80. private char m_cSex;
  81. private int m_iAge;
  82. private string m_strLastError;
  83. }
  84. }

如果需要在C++中使用这个C#编写的Person类,就需要用托管C++来对这个C#进行包装,将它包装成一个C++能用的类。
    首先,要创建一个托管C++的DLL工程ManageCppDll。并且,要添加对CsharpDll.dll的引用。然后对C#类所有的公有属性和方法进行包装。下面是具体的代码:

  1. // ManageCppDll.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. ~CPerson();
  14. void SetName(const wchar_t *pName);
  15. wchar_t * GetName();
  16. void SetSex(const wchar_t cSex);
  17. wchar_t GetSex();
  18. void SetAge(int iAge);
  19. int GetAge();
  20. wchar_t * GetLastError();
  21. private:
  22. // 用一个void指针指向Person的对象
  23. // 所有公有成员函数的实现都是通过这个对象来实现
  24. void *m_pImp;
  25. wchar_t m_szName[128];
  26. wchar_t m_szLastError[128];
  27. };
  28. // ManageCppDll.cpp
  29. #include "stdafx.h"
  30. #include "ManageCppDll.h"
  31. #include <vcclr.h>
  32. #include <string.h>
  33. #include <stdlib.h>
  34. using namespace System;
  35. using namespace System::Runtime::InteropServices;
  36. using namespace CsharpDll;
  37. // 将GCHandle转换成为void指针
  38. #define __GCHANDLE_TO_VOIDPTR(x) ((GCHandle::operator System::IntPtr(x)).ToPointer())
  39. // 将void指针转换为GCHandle
  40. #define __VOIDPTR_TO_GCHANDLE(x) (GCHandle::operator GCHandle(System::IntPtr(x)))
  41. // 辅助函数
  42. // 将void指针指向的对象转换成为Person对象
  43. inline Person ^ GetImpObj(void *pHandle)
  44. {
  45. Person ^ person = nullptr;
  46. if (pHandle != NULL)
  47. {
  48. person = static_cast<Person^>(__VOIDPTR_TO_GCHANDLE(pHandle).Target);
  49. }
  50. return person;
  51. }
  52. CPerson::CPerson()
  53. {
  54. m_pImp = NULL;
  55. Person ^ person = gcnew Person();
  56. // 创建GCHandle并将它转换成void指针保存到成员变量中
  57. GCHandle handle = GCHandle::Alloc(person);
  58. m_pImp = __GCHANDLE_TO_VOIDPTR(handle);
  59. }
  60. CPerson::CPerson(const wchar_t *pName, const wchar_t cSex, int iAge)
  61. {
  62. m_pImp = NULL;
  63. Person ^ person = gcnew Person();
  64. person->Name = gcnew String(pName);
  65. person->Sex = cSex;
  66. person->Age = iAge;
  67. GCHandle handle = GCHandle::Alloc(person);
  68. m_pImp = __GCHANDLE_TO_VOIDPTR(handle);
  69. }
  70. CPerson::~CPerson()
  71. {
  72. if (m_pImp == NULL)
  73. return;
  74. // 释放GCHandle
  75. GCHandle handle = __VOIDPTR_TO_GCHANDLE(m_pImp);
  76. handle.Free();
  77. m_pImp = NULL;
  78. }
  79. void CPerson::SetName(const wchar_t *pName)
  80. {
  81. // 将void指针转换成Person指针
  82. // 并用该指针调用相应的公有属性或方法
  83. Person ^ person = GetImpObj(m_pImp);
  84. person->Name = gcnew String(pName);
  85. }
  86. wchar_t * CPerson::GetName()
  87. {
  88. Person ^ person = GetImpObj(m_pImp);
  89. // 将C#返回的字符串转换为wchat_t*指针能指向的地址
  90. wchar_t * pName = static_cast<wchar_t*>(Marshal::StringToHGlobalUni(person->Name).ToPointer());
  91. wcscpy_s(m_szName, pName);
  92. Marshal::FreeHGlobal(System::IntPtr(pName)); // 释放内存
  93. return m_szName;
  94. }
  95. void CPerson::SetSex(const wchar_t cSex)
  96. {
  97. Person ^ person = GetImpObj(m_pImp);
  98. person->Sex = cSex;
  99. }
  100. wchar_t CPerson::GetSex()
  101. {
  102. Person ^ person = GetImpObj(m_pImp);
  103. return person->Sex;
  104. }
  105. void CPerson::SetAge(int iAge)
  106. {
  107. Person ^ person = GetImpObj(m_pImp);
  108. person->Age = iAge;
  109. }
  110. int CPerson::GetAge()
  111. {
  112. Person ^ person = GetImpObj(m_pImp);
  113. return person->Age;
  114. }
  115. wchar_t * CPerson::GetLastError()
  116. {
  117. Person ^ person = GetImpObj(m_pImp);
  118. wchar_t * pLastError = static_cast<wchar_t*>(Marshal::StringToHGlobalUni(person->LastError).ToPointer());
  119. wcscpy_s(m_szLastError, pLastError);
  120. Marshal::FreeHGlobal(System::IntPtr(pLastError));
  121. return m_szLastError;
  122. }

现在对上面代码中所用到的一些相关背景知识进行一下介绍。
    GCHandle结构提供从非托管内存访问托管对象的方法。
    GCHandle.Alloc方法(Object)为指定的对象分配Normal句柄。它保护对象不被垃圾回收。当不再需要GCHandle时,必须通过Free将其释放。Normal句柄类型表示不透明句柄,这意味着无法通过此句柄解析固定对象的地址。可以使用此类型跟踪对象,并防止它被垃圾回收器回收。当非托管客户端持有对托管对象的唯一引用(从垃圾回收器检测不到该引用)时,此枚举成员很有用。
    上面的代码中,在类CPerson的构造函数中用GCHandle为C#类Person的对象分配一个句柄,并将该句柄转换为void指针存放在成员变量中,以保证这个对象不会被垃圾回收器回收。然后在类CPerson的析构函数中释放这个句柄,将C#类Person的对象的回收权交给系统。
    Marshal类提供了一个方法集,这些方法用于分配非托管内存、复制非托管内存块、将托管类型转换为非托管类型,此外还提供了在与非托管代码交互时使用的其他杂项方法。
    Marshal..::.StringToHGlobalUni方法向非托管内存复制托管String的内容。StringToHGlobalUni对于自定义封送处理或者在混合托管和非托管代码时很有用。由于该方法分配字符串所需的非托管内存,因此应始终通过调用FreeHGlobal释放内存。
    (更多关于上面介绍的背景知识可以搜索MSDN。说实在的MSDN真是一个宝库!VS能在Windows平台开发中取得绝大多数的份额,除了因为它和Windows都是微软开发的之外,MSDN的完备性功不可没!)
    通过上面的方法,就把一个C#编写的类Person用托管C++给封装成了一个C++可以使用的CPerson类。我们可以在C++的工程中像使用一般的C++类一样使用类CPerson。比如下面的代码。

  1. CPerson person(_T("StarLee"), ‘M‘, 28);
  2. person.SetName(_T("StarLee"));
  3. person.SetSex(‘M‘);
  4. person.SetAge(28);
  5. wcout << "Name: " << person.GetName() << " Sex: " << person.GetSex() << " Age: " << person.GetAge() << endl;
  6. wcout << "Error: " << person.GetLastError() << endl;

这里的方法跟《在C#中使用C++编写的类》一样,都是借用托管C++这个桥梁来沟通C++编写的类和C#编写的类,使在C++中使用C#编写的类和在C#中使用C++编写的类成为现实。

时间: 2024-07-28 20:29:20

在C++中使用C#编写的类2的相关文章

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

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

在C#中使用C++编写的类——用托管C++进行封装[转]

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

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

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

Hibernate系列(二):简单编写HibernateUtil类来优化性能

相对于Hibernate系列(一)中的代码编写HibernateUtil类以提高程序的运行速度 首先,仍然要写一个javabean(User.java): package cn.itcast.hibernate.domain; import java.util.Date; public class User { private int id; private String name; private Date birthday; public int getId() { return id; }

编写高质量代码改善C#程序的157个建议——建议112:将现实世界中的对象抽象为类,将可复用对象圈起来就是命名空间

建议112:将现实世界中的对象抽象为类,将可复用对象圈起来就是命名空间 在我们身边的世界中,对象是什么?对象就是事物,俗称“东西”.那么,什么东西算得上是一个对象呢?对象有属性.有行为.以动物为例,比如猫(Cat).Cat可以有Name,这就是属性:Cat有一个恶习ScratchSofa(挠沙发),这就是行为.我们把这些属性和行为结合起来,就称为一个类型: class Cat { public string Name { get; set; } public void ScratchSofa()

ios 中Category类别(扩展类)专题总结

原创地址   http://www.code4blog.com/archives/294 类别 类别是一种为现有的类添加新方法的方式. 利用Objective-C的动态运行时分配机制,可以为现有的类添加新方法,这种为现有的类添加新方法的方式称为类别catagory,他可以为任何类添加新的方法,包括那些没有源代码的类. 类别使得无需创建对象类的子类就能完成同样的工作 一.创建类别 1.声明类别 声明类别与声明类的形式很相似 @interface  NSString(NumberConvenienc

学习MVC之租房网站(三)-编写实体类并创建数据库

在上一篇<学习MVC之租房网站(二)-框架搭建及准备工作>中,搭建好了项目框架,并配置了EF.Log4Net和进程外Session.接下来会编写Eneity类并采用CodeFirst的方式创建数据库. 一.数据库表结构举例 1. 在按照CodeFirst方式编写Entity类之前,仍然是需要先搞清楚数据库的表结构.首先肯定会有用户.管理员.角色.权限等相关的表,然后针对租房的特殊场景,还有房屋.房屋家电配置.房子所在的地址包括城市和区域等. 2. 这里面有两张表比较特殊,T_Settings和

c语言中字符串操作的工具类

 1.编写头文件 #define _CRT_SECURE_NO_WARNINGS //#pragmawarning(disable:4996) #include <stdio.h> #include <stdlib.h> #include <string.h> struct CString { char *p;        //保存字符串首地址 int reallength; //实际长度 }; typedef struct CString mystring;//

35.按要求编写Java程序: (1)编写一个接口:InterfaceA,只含有一个方法int method(int n); (2)编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方 法时,要求计算1到n的和; (3)编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口 方法时,要求计算n的阶乘(n

  35.按要求编写Java程序: (1)编写一个接口:InterfaceA,只含有一个方法int method(int n): (2)编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方 法时,要求计算1到n的和: (3)编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口 方法时,要求计算n的阶乘(n!): (4)编写测试类E,在测试类E的main方法中使用接口回调的形式来测试实现 接口的类. p