I fixed the problem using Visual Studio instead. If the dll needs to be used in Labview do not dynamicaly allocate memory inside the dll if you want to return a pointer to that dynamicaly allocated memory. The memory management of C and Labview are different. If the dynamically allocated memory is local to the dll, then it is OK.
How to add a wrapper function written in C to a dll generated with the Matlab Coder?
5 views (last 30 days)
Show older comments
Ahmed Rashid
on 29 May 2016
Answered: Ahmed Rashid
on 30 May 2016
I have the function in Matlab
function y = foo(x)
y = 2*x;
end
where x is an array with a dynamic size. I have generated code/dll for that function using the Matlab Coder toolbox. Since x and y have dynamic size, x and y are pointers to the emxArray structure in the generated code and both are passed to the generated functions as arguments. In order not to have the emxArray structure at the interface to the generated code, I created a wrapper function in C, that looks like that
double* wrapper_foo(const double *x, const int nrOfElements)
{
foo_initialize();
double* y = NULL;
y = (double*) malloc(nrOfElements * sizeof(double));
emxArray_real_T* pEmxX;
emxArray_real_T* pEmxY;
pEmxX = emxCreateWrapper_real_T(x, 1, nrOfElements);
pEmxY = emxCreateWrapper_real_T(y, 1, nrOfElements);
foo(pEmxX, pEmxY);
double* ptr1 = y;
double* ptr2 = pEmxY->data;
for (size_t i = 0; i < nrOfElements; i++, ptr1++, ptr2++)
{
*ptr1 = *ptr2;
}
ptr1 = NULL;
ptr2 = NULL;
emxDestroyArray_real_T(pEmxX);
emxDestroyArray_real_T(pEmxY);
foo_terminate();
return y;
}
I can now call wrapper function now from my main function instead of calling the foo function. Now, what I want to do more is to create the dll of this function and add it to the foo library that I generated before, i.e. link the wrapper_foo to foo.lib that was the product after code generation with the Matlab Coder. Is there a function in the Matlab Coder Toolbox or generally in Matlab to do this?
0 Comments
Accepted Answer
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!