Passing unmanaged pointers in C ++ / CLI

I am creating a C ++ / CLI DLL that depends on many C ++ static libraries. Some function calls expect unmanaged pointers to be passed. How to transfer them correctly?

In addition, other functions expect "this pointer" to be passed as void *. What is the correct way to go through this?

Here is my class definition ...

public ref class RTPClient
{
    public:
        RTPClient();
        ~RTPClient();

        bool Connect();
        void Disconnect();

    private:
        CIsmaClient* mClient;
};

Here is my use where pointers are used ...

RTPClient::RTPClient():
    mClient(NULL)
{
    CIsmaClient::Create(&mClient, NULL, &AllocBuffer, &GetDataPointer, this);
}

Using & mClient and "this" leads to the following compiler errors ... 1>. \ VBLoadSimulatorDll.cpp (40): error C2664: 'CIsmaClient :: Create': cannot convert parameter 1 from 'cli :: interior_ptr' to ' CIsmaClient ** '1> with 1> [1> Type = CIsmaClient * 1>]

1 > .\VBLoadSimulatorDll.cpp(40): C2664: 'CIsmaClient:: Create': 5 'VBLoadSimulator:: RTPClient ^ const' 'VOID *'

+5
2

, ^ , , GC ( , )

pin_ptr

,

RTPClient::RTPClient():
        mClient(NULL)
{
    CIsmaClient::Create(
        &mClient,          // 1
        NULL, 
        &AllocBuffer, 
        &GetDataPointer, 
        this);            //2
}

1) - ( mClient .

, , ( GC). pinned, , Create ( , , ).

2) handle ( ), . ( wikipedia, ) ( ) .

, , - , ( , ). 'this' , , pin_ptr .

, () , , .

RTPClient::RTPClient():
        mClient(NULL)
{
    // make it clear you want the address of the instance variable
    pin_ptr<CIsmaClient*> pinnedClient = &this->mClient; 
    CIsmaClient::Create(
        pinnedClient,          // fixed
        NULL, 
        &AllocBuffer, 
        &GetDataPointer, 
        x /* pass something else in */);            //2
}

, , .

+9

, , void:

void SomeFunction(void* input)
{
  gcroot<ManagedClass^>* pointer = (gcroot<ManagedClass^>*)(input);
  (*pointer)->ManagedFunction();
}

void Example()
{
  ManagedClass^ handle = gcnew ManagedClass();
  gcroot<ManagedClass^>* pointer = new gcroot<ManagedClass^>(handle);
  SomeFunction((void*)pointer);
  delete pointer;
}
+2

All Articles