MATLAB: Does mxCreateNumericArray returning NULL in the C code which uses libraries compiled using MATLAB Compiler SDK

MATLAB Compiler SDKmclinitializeapplicationmxcreatenumericarraynull

Hi, 
I'm writing a Windows command line program to read an image from disk and then convert it into a mxArray suitable for calling a MATLAB compiled library. I'm having an issue with mxCreateNumericArray. It is not allocating the array and returning NULL instead. See attached sample code. 
Is there something I need to do in a command line program to initialize the libmat library? 
 

Best Answer

The reason why mxCreateNumericArray returns NULL is because MCR is not initialized in Windows application C code.
In order to make it work, call the 'mclInitializeApplication' routine. Make sure that the application was initialized properly by checking the return status.
This initialization has to be done before calling any MATLAB APIs or MATLAB Compiler SDK generated shared library functions.
if( !mclInitializeApplication(NULL,0) )
    {
        fprintf(stderr, "Could not initialize the application.\n");
        return -1;
    }
The following document explains step-by-step guide to integrate a C shared library into a Windows Application-