Pass a C++/CLI wrapper of a native type to another C++/CLI assembly

两盒软妹~` 提交于 2019-12-07 07:10:07

问题


Suppose I have the following simple wrapper of a NativeClassInstance.

public ref class Wrapper
{
private:
    NativeClass *_wrapped;
public:
    Renderer()
    {
        _wrapped = new NativeClass();
    }
    ~Renderer()
    {
        delete _wrapped;
    }
    operator NativeClass*()
    {
        return _wrapped;
    }
}

Now, I want to create an instance of Wrapper from C# with Wrapper wrapper = new Wrapper() and use it in another native functionalities wrapper that resides in another assembly with Helper.Foo(wrapper) (nothing strange having other functionalities not directly related to the wrapped classes in another assembly, IMO):

// Utilities is in another Assembly
public ref class Helper
{
public:
    static Foo(Wrapper ^wrapper)
    {
        // Do something in native code with wrapper->_wrapped
    }
}

The results with the implicit user conversion is:

  • candidate function(s) not accessible

If I make _wrapped public it is:

  • cannot access private member declared in class ...

Now, I've learnt that native type visibility is private outside of the assembly. So, how I'm supposed to use the wrapped entity in native code outside the assembly it's defined? I've read of make_public but you can't use with template types so it seems very limiting in the general case. Am I missing something? Is there a more correct solution?


回答1:


If you use make_public, your solution of making _wrapped public should work (it would obviously be best to make a public accessor instead). Regarding your comment "I've read of make_public but you can't use with template types so it seems very limiting in the general case." I agree--read here for the workaround I used: http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/b43cca63-b0bf-451e-b8fe-74e9c618b8c4/

More related info: Best workaround for compiler error C2158: make_public does not support native template types

Good luck!




回答2:


I haven't been able to successfully expose native types using make_public, however a solution I have used is to put NativeClass in its own native DLL and then a) reference the native DLL from both assemblies; and b) pass the pointer to the native class around as an IntPtr.

Under the above scenario, instead of having an operator NativeClass* you might use a property such as

property IntPtr WrappedObject {
    IntPtr get() { return IntPtr(_wrapped); }
}

You then retrieve NativeObject in you helper assembly by

static void Foo(Wrapper ^wrapper)
{
    NativeObject *_wrapped
        = static_cast<NativeObject*>(wrapper->WrappedObject.ToPointer());
    // ... do something ...
}


来源:https://stackoverflow.com/questions/6102566/pass-a-c-cli-wrapper-of-a-native-type-to-another-c-cli-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!