0

I'm currently developing a wrapper for a C++ library so it can be used in MATLAB. I want my C++ objects to be in MATLAB so the user can do something with them. Actually I'm casting those C++ objects to void * because MATLAB only supports C headers. These C functions look like this:

__declspec(dllexport) void *getPtr (int someArgument);

In MATLAB I'm calling the functions like this:

ptr = calllib('LibName', 'getPtr', 42);

In MATLAB the ptr is now a <1x1 lib.pointer>. I can't do anything with it besides to pass it to another C function, like the following:

__declspec(dllexport) int doSomethingWithPtr (void *ptr);

So I'm calling result = calllib('LibName', 'doSomethingWithPtr', ptr); within MATLAB which works perfectly fine and executes this function with the pointer as argument. (I've debugged the C code and the pointer is the same as the one returned from getPtr.

My C function needs more than one pointer to work like intended by the C++ library. I already passed numeric data within MATLAB matrices to C which works perfectly with the C type mxArray (see: http://www.mathworks.de/de/help/matlab/apiref/mxarray.html). For the purpose of handing over multiple pointers to C I constructed an array of MATLAB lib.pointers like this:

A = [];

for i = 1:10
    ptr = calllib('LibName', 'getPtr', i);
    A = [A, ptr];    
end

And I'm calling another C function (__declspec(dllexport) int doSomethingWithPtrArray (mxArray *ptrarr);) within MATLAB:

ptr = calllib('LibName', 'doSomethingWithPtrArray', A);

Within the C function I'm getting the correct array dimensions with mxGetN(ptrarr) and mxGetM(ptrarr). I also can retrieve the data pointer with:

void *mexPtr = mxGetData(ptrarr);

The problem is that mexPtr and the following pointers point to locations in the memory I didn't even allocate or know before. Perhaps MATLAB does some intelligent wrapping when calling the function with void * and doesn't do that, when I'm passing over the array. (I think I'm getting the addresses to the MATLAB lib.pointer wrapper object??)

Does anybody have a clue (or a workaround) how I get the correct pointers out of the mxArray, so I can evaluate multiple pointers at once?

1 Answer 1

0

Since I found no 'beautiful' solution, I constructed a helper method in C++, which lets me build a vector of my own:

void *buildClassifierVectorBase(void *vector, void *classifier)
{
    typedef IClassifierManager<input_dtype, feature_dtype, annotation_dtype> class_man_t;

    std::vector<std::shared_ptr<class_man_t>> *classMgrs;

    if (vector == nullptr)
    {
        classMgrs = new std::vector<std::shared_ptr<class_man_t>>();            
    } 
    else
    {
        classMgrs = static_cast<std::vector<std::shared_ptr<class_man_t>> *>(vector);
    }

    class_man_t *cls = static_cast<class_man_t *>(classifier);
    classMgrs->push_back(std::shared_ptr<class_man_t>(cls));        

    return static_cast<void *>(classMgrs);  
}

This method I can use within MATLAB to build that vector of pointers like this:

ptr1 = calllib('LibName', 'getPtr', 42);
ptr2 = calllib('LibName', 'getPtr', 43);
ptr3 = calllib('LibName', 'getPtr', 44);

vector = calllib('LibName', 'buildClassifierVectorBase', libpointer, ptr1);
vector = calllib('LibName', 'buildClassifierVectorBase', vector, ptr2);
vector = calllib('LibName', 'buildClassifierVectorBase', vector, ptr3);

Now I can call my method doSomethingWithPtrArray:

ptr = calllib('LibName', 'doSomethingWithPtrArray', vector);

This solution works, but is not the prettiest...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.