C#调用C++dll的问题(高手入,如果你C++没有我这样的输出接口,你可能能学到点东西哦)
我这里的代码样子都是表示个意思,实际比这复杂多了,但是基本同理(请不要试图编译这个代码,你要是编译,可能需要修改修改,我直接在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技术都全活了
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技术都全活了
...全文
相关推荐
C++调用C#的DLL实例程序
C++调用C#的DLL实例程序,压缩包中包含部分程序说明,很小的几行代码,但是C++调用C#的DLL目的已经达到。
C#项目中调用C++生成的Dll 入门教程
如何在C#中调用C++生成的DLL文件
C#动态调用C++接口
有些场景需要用到C#调用C++编译的接口,这就要使用[DllImport]直接调用,该[DllImport]所在的名字空间为using System.Runtime.InteropServices;当我们得到一个C++的DLL接口时,我们可以使用[DllImport]调用该DLL就得知道DLL的接口,传入的参数、传出的参数、入口函数。(包括DLL的路径) 如: [DllImport(@"\..\....
C#调用C++DLL
在合作开发时,C#时常需要调用C++DLL。研究了一下C#,发现其强大简洁, 在跨语言调用方面封装的很彻底,提供了强大的API与之交互.这点比JNA方便多了. Java与C#都只能调用C格式导出动态库,因为C数据类型比较单一,容易映射. 两者都是在本地端提供一套与之映射的C#/java描述接口,通过底层处理这种映射关系达到调用的目的. 1、调用例子(例子中提供了传值调用和传址调用两种方法
基于C#调用c++Dll结构体数组指针的问题详解
下面小编就为大家分享一篇基于C#调用c++Dll结构体数组指针的问题详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧


