C#调用C++dll的问题(高手入,如果你C++没有我这样的输出接口,你可能能学到点东西哦)

charm charm 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我这里的代码样子都是表示个意思,实际比这复杂多了,但是基本同理(请不要试图编译这个代码,你要是编译,可能需要修改修改,我直接在notepad写的)


dll的输出头文件定义:

//对象驱动接口,仅仅是接口,没有export,因为提供了头文件,应用程序可以使用
class CClassCallback
{
public:
virture HRESULT OnAdded() = 0;
virture HRESULT OnDeled() = 0;
virture HRESULT OnFailed() = 0;
};

//
struct SAtype
{
CString strTest;
int nTest;
CClassCallback* pClassCallback;
};

//类接口,类似COM的接口,没有export,因为提供了头文件,应用程序可以使用
class CTestInterface
{
public:
virture HRESULT Test1(SAtype* pTest) = 0;
virture HRESULT Test2() = 0;
virture void Release() = 0;
};

输出函数export:CTestInterface* ConstructInterface(); //仅仅这个函数是输出的

在dll的实现类似如下:

class CTestObject:public CTestInterface
{
public:
HRESULT Test1(SAtype* pTest)
{
m_sType = *pTest;
}
HRESULT Test2()
{

}
void Release()
{
delete this;
}
private:
void InnerDemoFuc()
{
if(true)
{
m_sType.pClassCallback->OnAdded();
}
}
SAtype m_sType;
}

CTestInterface* ConstructInterface()
{
return new CTestObject();
}


在C++的App中一般这么使用:

(1)派生一个CClassCallback子类

class CClassCallbackEx:public CClassCallback
{
public:
CClassCallbackEx();

virture HRESULT OnAdded()
{
.....//干些通知的业务的活
}
virture HRESULT OnDeled()
{
}
virture HRESULT OnFailed()
{
}
}

(2)如下使用:
CTestInterface* m_pInterface = ConstructInterface(); //调用输出函数创建对象,获取外部接口
SAtype sAtype;
sAtype->pClassCallback = new CClassCallbackEx(); //设置回调驱动对象(我使用对象驱动,非函数驱动)
m_pInterface->Test1(&sAtype); //调用某个函数

。。。。。
m_pInterface->Relese();
m_pInterface=NULL; //在合适的时候释放对象


现在我要想在C#的界面程序中使用这个dll,这么弄啊?
我搜了一下,如果我这个东西弄好的话,所以C#调用C++的dll技术都全活了
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
charm
C# 111,129 成员 642,540 内容