您的位置:首页 > 编程语言 > C语言/C++

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

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

比如说,现在有一个用C#编写的DLL工程CsharpDll里面有一个Person类:

// Person.cs

using System;

namespace CsharpDll

{

public class Person

{

public Person()

{

Name = "No Name";

Sex = 'N';

Age = 0;

m_strLastError = "No Error";

}

public Person(string strName, char cSex, int iAge)

{

m_strLastError = "No Error";

Name = strName;

Sex = cSex;

Age = iAge;

}

public string Name

{

get

{

return m_strName;

}

set

{

if ((String.IsNullOrEmpty(value)) || (value.Length > 127))

{

m_strName = "No Name";

m_strLastError = "The length of the input name is out of range.";

return;

}

m_strName = value;

}

}

public char Sex

{

get

{

return m_cSex;

}

set

{

if ((value != 'F') && (value != 'M') && (value != 'm') && (value != 'f'))

{

m_cSex = 'N';

m_strLastError = "The input sex is out of [F/M].";

return;

}

m_cSex = value;

}

}

public int Age

{

get

{

return m_iAge;

}

set

{

if ((value < 0) || (value > 150))

{

m_iAge = 0;

m_strLastError = "The input age is out of range.";

return;

}

m_iAge = value;

}

}

public string LastError

{

get

{

return m_strLastError;

}

}

private string m_strName;

private char m_cSex;

private int m_iAge;

private string m_strLastError;

}

}

如果需要在C++中使用这个C#编写的Person类,就需要用托管C++来对这个C#进行包装,将它包装成一个C++能用的类。

首先,要创建一个托管C++的DLL工程ManageCppDll。并且,要添加对CsharpDll.dll的引用。然后对C#类所有的公有属性和方法进行包装。下面是具体的代码:

// ManageCppDll.h

#pragma once

#ifndef LX_DLL_CLASS_EXPORTS

#define LX_DLL_CLASS __declspec(dllexport)

#else

#define LX_DLL_CLASS __declspec(dllimport)

#endif

class LX_DLL_CLASS CPerson

{

public:

CPerson();

CPerson(const wchar_t *pName, const wchar_t cSex, int iAge);

~CPerson();

void SetName(const wchar_t *pName);

wchar_t * GetName();

void SetSex(const wchar_t cSex);

wchar_t GetSex();

void SetAge(int iAge);

int GetAge();

wchar_t * GetLastError();

private:

// 用一个void指针指向Person的对象

// 所有公有成员函数的实现都是通过这个对象来实现

void *m_pImp;

wchar_t m_szName[128];

wchar_t m_szLastError[128];

};

// ManageCppDll.cpp

#include "stdafx.h"

#include "ManageCppDll.h"

#include <vcclr.h>

#include <string.h>

#include <stdlib.h>

using namespace System;

using namespace System::Runtime::InteropServices;

using namespace CsharpDll;

// 将GCHandle转换成为void指针

#define __GCHANDLE_TO_VOIDPTR(x) ((GCHandle::operator System::IntPtr(x)).ToPointer())

// 将void指针转换为GCHandle

#define __VOIDPTR_TO_GCHANDLE(x) (GCHandle::operator GCHandle(System::IntPtr(x)))

// 辅助函数

// 将void指针指向的对象转换成为Person对象

inline Person ^ GetImpObj(void *pHandle)

{

Person ^ person = nullptr;

if (pHandle != NULL)

{

person = static_cast<Person^>(__VOIDPTR_TO_GCHANDLE(pHandle).Target);

}

return person;

}

CPerson::CPerson()

{

m_pImp = NULL;

Person ^ person = gcnew Person();

// 创建GCHandle并将它转换成void指针保存到成员变量中

GCHandle handle = GCHandle::Alloc(person);

m_pImp = __GCHANDLE_TO_VOIDPTR(handle);

}

CPerson::CPerson(const wchar_t *pName, const wchar_t cSex, int iAge)

{

m_pImp = NULL;

Person ^ person = gcnew Person();

person->Name = gcnew String(pName);

person->Sex = cSex;

person->Age = iAge;

GCHandle handle = GCHandle::Alloc(person);

m_pImp = __GCHANDLE_TO_VOIDPTR(handle);

}

CPerson::~CPerson()

{

if (m_pImp == NULL)

return;

// 释放GCHandle

GCHandle handle = __VOIDPTR_TO_GCHANDLE(m_pImp);

handle.Free();

m_pImp = NULL;

}

void CPerson::SetName(const wchar_t *pName)

{

// 将void指针转换成Person指针

// 并用该指针调用相应的公有属性或方法

Person ^ person = GetImpObj(m_pImp);

person->Name = gcnew String(pName);

}

wchar_t * CPerson::GetName()

{

Person ^ person = GetImpObj(m_pImp);

// 将C#返回的字符串转换为wchat_t*指针能指向的地址

wchar_t * pName = static_cast<wchar_t*>(Marshal::StringToHGlobalUni(person->Name).ToPointer());

wcscpy_s(m_szName, pName);

Marshal::FreeHGlobal(System::IntPtr(pName)); // 释放内存

return m_szName;

}

void CPerson::SetSex(const wchar_t cSex)

{

Person ^ person = GetImpObj(m_pImp);

person->Sex = cSex;

}

wchar_t CPerson::GetSex()

{

Person ^ person = GetImpObj(m_pImp);

return person->Sex;

}

void CPerson::SetAge(int iAge)

{

Person ^ person = GetImpObj(m_pImp);

person->Age = iAge;

}

int CPerson::GetAge()

{

Person ^ person = GetImpObj(m_pImp);

return person->Age;

}

wchar_t * CPerson::GetLastError()

{

Person ^ person = GetImpObj(m_pImp);

wchar_t * pLastError = static_cast<wchar_t*>(Marshal::StringToHGlobalUni(person->LastError).ToPointer());

wcscpy_s(m_szLastError, pLastError);

Marshal::FreeHGlobal(System::IntPtr(pLastError));

return m_szLastError;

}

现在对上面代码中所用到的一些相关背景知识进行一下介绍。

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。比如下面的代码。

CPerson person(_T("StarLee"), 'M', 28);

person.SetName(_T("StarLee"));

person.SetSex('M');

person.SetAge(28);

wcout << "Name: " << person.GetName() << " Sex: " << person.GetSex() << " Age: " << person.GetAge() << endl;

wcout << "Error: " << person.GetLastError() << endl;

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

22

在本文章中我们再来实现一下C++代码调用C#代码的过程。我构造一个简单并且直观的例子:通过C++ UI 触发C# UI.



首先建立一个C#工程Class Library工程——CSharpUI



为该工程添加一个Form界面,并添加一个C#函数——InvokeUi()来构造并显示这个界面

[c-sharp] view
plaincopy

namespace CSharpUI

{

public class Program

{

public static void InvokeUi()

{

//class Form1-----C# UI

Form1 fm = new Form1();

fm.ShowDialog();

}

}

}





建立一个托管的DYNAMIC LINK LIBRARY工程——MgdPro。





在MgdPro工程中用代码包装了对上述C#函数的调用,并将包装后的类从DLL导出。前提条件是reference CSharpUI.dll.

[cpp] view
plaincopy

//.h file

#define DLLIMPEXP __declspec(dllexport)

class DLLIMPEXP MgdClass

{

public:

static void InvokeCsharpDlg();

};

//.cpp file

using namespace CSharpUI;

void MgdClass::InvokeCsharpDlg()

{

Program::InvokeUi();

}





最后,新建一个Dialog Based的C++工程——PureC++Pro(exe工程),在工程中静态链接MgdPro.dll.在Invoke按钮的触发函数里调用代码触发C#界面。void CPureCProDlg::OnBnClickedButton1()

[cpp] view
plaincopy

{

// TODO: Add your control notification handler code here

//call managed c++ to invoke c# UI

MgdClass::InvokeCsharpDlg();

}





运行界面如下所示:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: