using MATLAB generated shared dlls in VC++???

21 visualizaciones (últimos 30 días)
Anuj Agrawal
Anuj Agrawal el 25 de Feb. de 2014
Editada: Anuj Agrawal el 25 de Feb. de 2014
I have generated the dll file of my MATLAB program and I wanna use this dll in my VC++2008 project. but when i call the function from my dll it returns false. Can anyone help me out.. here the section of the code..
the library file name is "Anujlib"
#include "mclmcrrt.h"
#include "Anujlib.h"
#include stdio.h
int main() { double *y = NULL;
double ret;
/* Call the MCR and library initialization functions */
if( !mclInitializeApplication_proxy(NULL,0) ) {
}
if (!AnujlibInitialize()) {
}
mxArray *A = mxCreateDoubleScalar(2);
mxArray *B = mxCreateDoubleScalar(5.0);
mxArray *Val = NULL;
bool status = mlfFuncSubtraction(1,&Val,A,B); // It returns false here
y = mxGetPr(Val);
AnujlibTerminate();
mxDestroyArray(A);
mxDestroyArray(B);
mxDestroyArray(Val);
mclTerminateApplication();
return 0;
}
/***********************************************************/
here is how my Anujlib.c looks like
#include stdio.h
#define EXPORTING_Anujlib 1
#include "Anujlib.h"
static HMCRINSTANCE _mcr_inst = NULL;
#if defined( MSC_VER) defined(__BORLANDC_) defined(_WATCOMC_)
defined(_LCC_)
#ifdef _LCC_
#undef EXTERN_C
#endif
#include windows.h
static char path_to_dll[_MAX_PATH];
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void *pv) { if (dwReason == DLL_PROCESS_ATTACH) { if (GetModuleFileName(hInstance, path_to_dll, _MAX_PATH) == 0)
return FALSE;
}
else if (dwReason == DLL_PROCESS_DETACH)
{
}
return TRUE;
}
#endif
#ifdef __cplusplus
extern "C" {
#endif
static int mclDefaultPrintHandler(const char *s) {
return mclWrite(1 /* stdout */, s, sizeof(char)*strlen(s));
}
#ifdef __cplusplus
} /* End extern "C" block */
#endif
#ifdef __cplusplus
extern "C" {
#endif
static int mclDefaultErrorHandler(const char *s) { int written = 0;
size_t len = 0;
len = strlen(s);
written = mclWrite(2 /* stderr */, s, sizeof(char)*len);
if (len > 0 && s[ len-1 ] != '\n')
written += mclWrite(2 /* stderr */, "\n", sizeof(char));
return written;
}
#ifdef __cplusplus
} /* End extern "C" block */
#endif
/* This symbol is defined in shared libraries. Define it here
* (to nothing) in case this isn't a shared library.
*/
#ifndef LIB_Anujlib_C_API
#define LIB_Anujlib_C_API /* No special import/export declaration */
#endif
LIB_Anujlib_C_API
bool MW_CALL_CONV AnujlibInitializeWithHandlers(
mclOutputHandlerFcn error_handler,
mclOutputHandlerFcn print_handler)
{ int bResult = 0;
if (_mcr_inst != NULL)
return true;
if (!mclmcrInitialize())
return false;
if (!GetModuleFileName(GetModuleHandle("Anujlib"), path_to_dll, _MAX_PATH))
return false;
{
mclCtfStream ctfStream =
mclGetEmbeddedCtfStream(path_to_dll);
if (ctfStream)
{
bResult = mclInitializeComponentInstanceEmbedded( &_mcr_inst,
error_handler,
print_handler,
ctfStream);
mclDestroyStream(ctfStream);
} else {
bResult = 0;
}
}
if (!bResult)
return false;
return true;
}
LIB_Anujlib_C_API
bool MW_CALL_CONV AnujlibInitialize(void)
{ return AnujlibInitializeWithHandlers(mclDefaultErrorHandler,
mclDefaultPrintHandler);
}
LIB_Anujlib_C_API
void MW_CALL_CONV AnujlibTerminate(void)
{ if (_mcr_inst != NULL)
mclTerminateInstance(&_mcr_inst);
}
LIB_Anujlib_C_API
void MW_CALL_CONV AnujlibPrintStackTrace(void) {
char** stackTrace;
int stackDepth = mclGetStackTrace(&stackTrace);
int i;
for(i=0; i<stackDepth; i++)
{
mclWrite(2 /* stderr */, stackTrace[i], sizeof(char)*strlen(stackTrace[i]));
mclWrite(2 /* stderr */, "\n", sizeof(char)*strlen("\n"));
}
mclFreeStackTrace(&stackTrace, stackDepth);
}
LIB_Anujlib_C_API
bool MW_CALL_CONV mlxFuncAddition(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
return mclFeval(_mcr_inst, "funcAddition", nlhs, plhs, nrhs, prhs);
}
LIB_Anujlib_C_API
bool MW_CALL_CONV mlxFuncSubtraction(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
return mclFeval(_mcr_inst, "funcSubtraction", nlhs, plhs, nrhs, prhs);
}
LIB_Anujlib_C_API
bool MW_CALL_CONV mlfFuncAddition(int nargout, mxArray** Value, mxArray* x, mxArray* y)
{
return mclMlfFeval(_mcr_inst, "funcAddition", nargout, 1, 2, Value, x, y);
}
LIB_Anujlib_C_API
bool MW_CALL_CONV mlfFuncSubtraction(int nargout, mxArray** Value, mxArray* x, mxArray* y) {
return mclMlfFeval(_mcr_inst, "funcSubtraction", nargout, 1, 2, Value, x, y);
}
/**********************************************************************************/

Respuestas (0)

Categorías

Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by