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

空扰寡人 提交于 2019-12-05 17:11:46
Matt Smith

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!

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