使用非托管接口包装托管
我有一个公开一些接口的非托管库。用户可以实现接口并通过自定义实现将它们粘贴到库中。
我想为这个库提供一个托管包装器。 用托管接口包装非托管接口很容易。但就我而言,我想支持各种接口的用户实现,这意味着我需要采用接口的托管实现,并使用其非托管对应部分将其包装,然后再将其发送到库的非托管部分的深处。
我尝试过类似的操作:
class UnmanagedWrapper {
DoSomething() {m_clr.DoSomething();}
IManaged^ m_clr;
}
但编译器正确地声明,我不能在非托管类中拥有托管成员。
我可以在这里做一些优雅的事情吗?
I have an unmanaged library exposing some interfaces. Users can implement interfaces and stick them into the library with their custom implementation.
I would like to supply a managed wrapper for this library.
Wrapping an unmanaged interface with a managed one is easy. But in my case I would like to support user implementations of various interfaces which means I need to take a managed implementation of an interface and wrap it using its unmanaged counterpart before I send it into the depths of the unmanaged part of the library.
I tried something like:
class UnmanagedWrapper {
DoSomething() {m_clr.DoSomething();}
IManaged^ m_clr;
}
But I cannot have managed members within an unmanaged class, the compiler rightfully claimed.
Can I do anything elgant here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是当一个库不受托管并且托管语言使用这些库时解决方法的一些相关信息。
此信息的上下文是在 Visual Studio 中使用 GoogleTest 的一种方法:
获取从 Google C++ 测试框架开始
Here is some related information for work arounds when one library is unmanaged and a managed language uses those libraries.
The context of this information is a way to use GoogleTest in Visual Studio:
Getting started with Google C++ Testing Framework
也许 gcroot 就是您想要的想:
Maybe gcroot<> is what you want: