How do I return an error to C++ from a compiled Matlab dll?

2 次查看(过去 30 天)
I have a Matlab function: foo=doFoo(); I have used the Matlab compiler to convert doFoo() to a function callable from C++: success=mlfDoFoo(int argOut, mxArray **foo); Every once in a while, doFoo crashes.
Is there a way to set success based on whether or not and how doFoo crashes?

回答(1 个)

Rahul Goel
Rahul Goel 2015-9-28
Fred,
The syntax to use shared libraries in C++ code is such that their return type is "void" and hence cannot be used in the way you mentioned. Also, what exactly do you mean by "crash"? Did the whole program crashed or is it just the library which throws an error? All the outputs to be collected are passed as reference using the second argument("mxArray **foo" in this case) of the function called from shared library, if it crashes there will not be any variable which might store this error information.
I would suggest trying the try/catch in your code while using the shared library. An example can be found in the documentation at:
where a try/catch block is used to handle any exception thrown by the function called from the shared library:
try
{
// Create input data
double data[] = {1,2,3,4,5,6,7,8,9};
mwArray in1(3, 3, mxDOUBLE_CLASS, mxREAL);
mwArray in2(3, 3, mxDOUBLE_CLASS, mxREAL);
in1.SetData(data, 9);
in2.SetData(data, 9);
// Create output array
mwArray out;
// Call the library function
addmatrix(1, out, in1, in2);
std::cout << "The value of added matrix is:" << std::endl;
std::cout << out << std::endl;
}
catch (const mwException& e)
{
std::cerr << e.what() << std::endl;
return -2;
}
catch (...)
{
std::cerr << "Unexpected error thrown" << std::endl;
return -3;
}
Hope this helps.
  1 个评论
Fred
Fred 2015-9-28
Is the return type really void? As far as I could tell, success is a Boolean which indicates whether or not the function returned successfully. I ended up putting doFoo into a try-catch loop in Matlab and then setting an output error parameter based on the caught error.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Deploy to C++ Applications Using mwArray API (C++03) 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by