标签:结果 last 链接 ima rlock dde cpp prot property
1.先建立一个Winform工程
2.添加一个C++动态链接库项目
3.添加一个纯c++静态库(LIB)项目
4.向静态库中添加一个类PureCClass
功能,计算两个数的和
.h文件
class PureCClass { public: PureCClass(); ~PureCClass(void); public: int getFirstNum(); void setFirstNum(int fistNum); int getLastNum(); void setLastNum(int lastNum); int AddNum(); private: int m_FirstNum; int m_LastNum; int m_AddedNum; };
.cpp文件
PureCClass::PureCClass() { } PureCClass::~PureCClass(void) { } int PureCClass::getFirstNum() { return m_FirstNum; } void PureCClass::setFirstNum(int fistNum) { m_FirstNum = fistNum; } int PureCClass::getLastNum() { return m_LastNum; } void PureCClass::setLastNum(int lastNum) { m_LastNum = lastNum; } int PureCClass::AddNum() { m_AddedNum = m_FirstNum + m_LastNum; return m_AddedNum; }
5.修改MgdDll项目的属性
5.1允许clr
5.2添加include目录和lib目录,添加库
5.3.注释掉dllmain.cpp中的代码
6.添加一个类mgClass
.h文件
#pragma once #include "../PureCClass.h"//修改路径 namespace MgdDll { public ref class mgClass { public: mgClass(); protected: !mgClass(); public: ~mgClass(); property int FirstNum { int get(); void set(int ival); } property int LastNum { int get(); void set(int ival); } int AddNum(); private: PureCClass* m_pCObj; }; }
.cpp文件
#include "mgClass.h"
namespace MgdDll { mgClass::mgClass() { m_pCObj = new PureCClass(); } mgClass::!mgClass() { delete m_pCObj; } mgClass::~mgClass() { this->!mgClass(); } int mgClass::FirstNum::get() { return m_pCObj->getFirstNum(); } void mgClass::FirstNum::set(int ival) { m_pCObj->setFirstNum(ival); } int mgClass::LastNum::get() { return m_pCObj->getLastNum(); } void mgClass::LastNum::set(int ival) { m_pCObj->setLastNum(ival); } int mgClass::AddNum() { return m_pCObj->AddNum(); } }
7.在C#项目中引用MgdDll.dll,
先using MgdDll;再并使用代码
mgClass mgc = new mgClass(); mgc.FirstNum = 10; mgc.LastNum = 4; MessageBox.Show(mgc.AddNum().ToString());
8.刚开始时没有注释掉MgdDll项目dllmain中的代码,总是出现
错误,网上搜索的去掉LoaderLock异常,也仅仅是不提示了,但并没有出来正确的结果
最后才发现关键是
//创建托管dll时无需入口
//C++托管代码不需要导出函数声明 __declspec(dllexport)
参考
使用托管C++粘合C#和C++代码
https://blog.csdn.net/jadeflute/article/details/5689502
标签:结果 last 链接 ima rlock dde cpp prot property
原文地址:https://www.cnblogs.com/sinceret/p/12244076.html