C#对导出抽象类/接口的C++DLL函数的消耗
C# consumption of C++ DLL function that exports an abstract class/interface?
我有一个DLL,它只有一个导出函数:
#ifdef __cplusplus
extern "C"
{
__declspec(dllexport) IRouter* CreateRouter();
}
#endif
IRouter的接口如下:
class IRouter
{
public:
virtual bool __stdcall DoSomething(LPCWSTR szCommand) = 0;
// Call Release() from DLL client to free any memory associated with this object
virtual bool __stdcall Release() = 0;
};
我有一个具体的类,它的接口如下:
class CMyRouter : public IRouter
{
public:
bool __stdcall DoSomething(LPCWSTR szCommand);
bool __stdcall Release();
}
正如您所期望的,MyRouter类的实现包含在DLL中。
我的单一导出功能的代码如下:
#ifdef __cplusplus
extern "C"
{
__declspec(dllexport) IRouter* CreateRouter()
{
return new CMyRouter;
}
}
#endif // __cplusplus
我的问题是:如何从C#客户端访问IRouter派生的对象?
您可以在C++/CLI中执行此操作(使用聚合,而不是继承)。例如,在C++/CLI中创建一个托管类,该类对应于并保存指向C++抽象类IRouter
的指针,并提供少数转发方法(如`DoSomething(string)
)。然后,您可以在C#中实现其余的托管逻辑。
使用p/Invoke或COM.
相关文章:
- Visual c ++,使用字符串引用/指针调用 dll 函数
- 在 Microsoft Access SQL 中调用自定义 DLL 函数时传递的内存地址无效
- 调用 "project" 函数和调用 DLL 函数之间的区别
- 代码在 msvcrt .dll函数上设置断点失败
- VB.NET DLL中的C++DLL函数复制
- 在python-ctypes中声明变量并传递给dll函数
- 从 Python 调用 Windows DLL 函数时出错
- 调用 dll 函数时"Run-Time Check Failure #0 - The value of ESP"
- 列出 C# 中的 non-.NET DLL 函数
- 基本 DLL 函数
- 如何在不可修改的源代码中使用 DLL 函数?
- 对需要双精度数组和结构作为输入C++ DLL 函数的 C# 调用
- WScript.CreateObject 在调用 C++ COM dll 函数时返回空
- 如何在 lua cpp 模块中调用托管 c++ dll 函数
- 测试C++ DLL 函数
- 从 C# GUI 将 char * 参数传递给C++ dll 函数
- 使用指针调用C++ DLL 函数
- 在C++中导入 DLL 函数
- #VALUE!在 64 位 Excel 中调用 DLL 函数 (C++) 时
- 如何从 UWP 应用调用"libheif.dll"函数