Mark Ingram
Mark Ingram

Reputation: 73683

Writing a C++/CX wrapper class for native C++ class

Due to C++/CX classes not allowing native types in the public signature, I need to find an alternative way of getting the information in. I've found a couple of references to writing a wrapper class on the internet, but no actual implementations. How can I enable the following scenario in my code?

public ref class MyRefClass
{
public:
    void SetNativeType(NativeType *pType);
};

Obviously this won't work, so how do I wrap NativeType so that it can be passed into MyRefClass?

I should add that this is in a separate library, so the use of the internal keyword won't help here.

Upvotes: 2

Views: 2153

Answers (2)

CÅdahl
CÅdahl

Reputation: 542

Since WinRT (and COM) solves the very real problem of binary API compatibility, I've personally settled on using COM for this. You can declare a private interface without IDL or type libraries. All you need is to include the header with the interface declaration in the client. The matching IID and the interface signature is all you need (except to also figure out how to pass the IUnknown pointer).

Upvotes: 0

yms
yms

Reputation: 10418

Assuming it is enough just to wrap a pointer to your NativeType, you could use Platform::IntPtr as a "generic" parameter.

From MSDN:

static IntPtr::operator IntPtr( void* value1);
static IntPtr::operator IntPtr( int value2);
static IntPtr::operator void*( IntPtr value3 );

As an alternative, you can have SetNativeType(NativeType *pType); as internal, and then distribute a static library with header files instead of a Windows Store class library.

About the warning you mentioned in your comments, you could try having a plain C++ class MyPlainClass, and export its implementation instead in a lib file (even if you consume other ref classes inside), then provide a header-only ref class MyRefClass that wraps MyPlainClass and acts as a public interface. This solution is not perfect either, I am guessing you will have troubles combining in the same project two winrt libraries that use your lib+header files, but maybe you do not need to support this scenario.

Upvotes: 1

Related Questions