سؤال

We have to deal with a legacy C++ API (API A) that returns a void* from one function. That void* needs to be passed to a C++ /CX API of ours (API B) to later be routed back into another legacy C++ API (API C) that has a function taking a void* parameter. Our API B never dereferences the void*, it just needs to hold on to it for a while to later pass it untouched to API C.

But we cannot find the correct WinRT type to cast the void* to. API B needs to have the C++ /CX methods public (due to being an interface implementation), so native types like void* cannot be part of it.

Is there a WinRT type meant for transfering opaque pointer/handle types like that?

EDIT - some code to make the question clearer:

We get a void* from this legacy C++ class:

namespace A
{
    class Source
    {
    public:

        static void* GetVoidPtr();
    }
}

We want to pass the WinRT representation of a void* to this second legacy C++ class:

namespace C
{
    class Target
    {
    public:

        static void SetVoidPtr(void* ptr);
    }
}

In between them we have the C++ /CX classes Router, that receives a void* from Source, and Receiver, that gets the WinRT representation of the void* since the Receive method must be public and passes it on the legacy class Target:

namespace B
{
    public ref class Router sealed
    {
    private:

        Receiver^ m_receiver;

        void Route()
        {
            auto ptr = SOME_CAST<SOME_TYPE>(A::Source::GetVoidPtr());
            ...
            m_receiver->Receive(ptr);
        }
    }

    public interface class IReceiver
    {
        void Receive(SOME_TYPE ptr);
    }

    public ref class Receiver sealed : IReceiver
    {
    public:

        virtual void Receive(SOME_TYPE ptr)
        {
            C::Target::SetVoidPtr(SOME_CAST<void*>(ptr));
        }
    }
}
هل كانت مفيدة؟

المحلول

You are talking about a handle. Platform::IntPtr is the portable type.

They are exploitable, although not typically a great concern in a Store app. EncodePointer() is a good way to stop that. Client code can crash you by fumbling the value, handing out a simple integer that you store in a map is a countermeasure for that.

نصائح أخرى

Change class A to use internal: instead of public:. The internal keyword will make the GetVoidPtr() function accessible to pure C++ classes, but will make it unaccessible through the WinRT metadata.

namespace A
{
   class Source
    {
    internal:

        static void* GetVoidPtr();
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top