Click here to Skip to main content
15,905,028 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Exiting Application Pin
MrMcIntyre6-Mar-10 10:39
MrMcIntyre6-Mar-10 10:39 
GeneralRe: Exiting Application Pin
Tim Craig6-Mar-10 11:02
Tim Craig6-Mar-10 11:02 
GeneralRe: Exiting Application Pin
LunaticFringe7-Mar-10 0:27
LunaticFringe7-Mar-10 0:27 
GeneralRe: Exiting Application Pin
Richard MacCutchan7-Mar-10 2:10
mveRichard MacCutchan7-Mar-10 2:10 
GeneralRe: Exiting Application Pin
Tim Craig7-Mar-10 9:08
Tim Craig7-Mar-10 9:08 
GeneralRe: Exiting Application Pin
LunaticFringe7-Mar-10 9:22
LunaticFringe7-Mar-10 9:22 
GeneralRe: Exiting Application Pin
Tim Craig7-Mar-10 13:27
Tim Craig7-Mar-10 13:27 
QuestionCompiling .cpp made from Matlab > Errors Pin
skyhr6-Mar-10 4:20
skyhr6-Mar-10 4:20 
Hello,

My main goal is to convert Matlab functions into .dll and then import that into a program called Metatrader. But my shorter goal is to actually be able to compile the .cpp made from Matlab, and I am unable to do so for some reason. Here are the details:

Although the functions I want to import are much more complex, for the sake of the problem, I've created 2 sample functions - AddTwo(x,y) and MultiplyTwo(x,y) in Matlab.

function output = AddTwo(x,y)
output = x + y;

function output = MultiplyTwo(x,y)
output = x * y;


Next, I compile them using mcc command (using Visual C++ 6.0 as C++ compiler).
mcc -W cpplib:libmatrix -T link:lib AddTwo MultiplyTwo


This got me several files, like the .cpp, .h, and .dll files. If I import the .dll file into Metatrader and try to use it now, it gives me 127 error (problem with .def/function names) or a critical error (crash when function names are corrected). So my goal here is to go inside the .cpp file and edit it to make it compatible with Metatrader and then compile it. The problem is that even when I don't edit it I can't seem to compile the basic file.

On the side, these are the requirements of the .dll file and exported functions to work properly in Metatrader:

Your DLL and the exported functions must fullfill the following requirements to be used from metatrader:

function name:

    * not mangled


calling convention:

    * stdcall

possible function arguments (when passed by value):

    * 32bit integer
    * double
    * Pointer to nullterminated string

possible function arguments (when passed by reference):

    * Pointer to array of
          o 32 bit integer
          o double
    * no intergers by reference due to bug(?) in mql compiler
    * no doubles by reference due to bug(?) in mql compiler

possible function return values:

    * 32bit integer
    * double
    * Pointer to nullterminated string (contents will be copied immediately)


I am not completely familiar with these things (the top two I kind of understand).

So my primary goals are to:
1. Be able to compile the .cpp file in the first place
2. Make the .cpp file fulfill the above requirements for Metratrader.

Now, what I've done so far trying to compile the unedited .cpp file is include the necessary .lib files - mclmcr.lib mclmcrrt.lib - from Matlab's extern/lib/win32/microsoft folders. This took away many LINK2001 errors. But now I don't know what to do and when I try to build the .dll I get the following error message:

--------------------Configuration: libmatrix - Win32 Debug--------------------
Compiling...
libmatrix.cpp
Linking...
   Creating library Debug/libmatrix.lib and object Debug/libmatrix.exp
libmatrix.obj : error LNK2001: unresolved external symbol ___MCC_libmatrix_component_data
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/libmatrix.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

libmatrix.exe - 3 error(s), 0 warning(s)


I am not very familiar with using Visual C++ 6.0

Also, because I can't attach files here, I will post the libmatrix.cpp and libmatrix.h code here. But you can generate these by making the Matlab functions and converting them via mcc.

libmatrix.cpp
//
// MATLAB Compiler: 4.11 (R2009b)
// Date: Sat Mar 06 04:06:14 2010
// Arguments: "-B" "macro_default" "-W" "cpplib:libmatrix" "-T" "link:lib"
// "AddTwo" "MultiplyTwo" 
//

#include <stdio.h>
#define EXPORTING_libmatrix 1
#include "libmatrix.h"
#ifdef __cplusplus
extern "C" {
#endif

extern mclComponentData __MCC_libmatrix_component_data;

#ifdef __cplusplus
}
#endif


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_libmatrix_C_API
#define LIB_libmatrix_C_API /* No special import/export declaration */
#endif

LIB_libmatrix_C_API 
bool MW_CALL_CONV libmatrixInitializeWithHandlers(
    mclOutputHandlerFcn error_handler,
    mclOutputHandlerFcn print_handler)
{
  if (_mcr_inst != NULL)
    return true;
  if (!mclmcrInitialize())
    return false;
  if (!GetModuleFileName(GetModuleHandle("libmatrix"), path_to_dll, _MAX_PATH))
    return false;
  if (!mclInitializeComponentInstanceWithEmbeddedCTF(&_mcr_inst, 
                                                     &__MCC_libmatrix_component_data, 
                                                     true, NoObjectType, LibTarget, 
                                                     error_handler, print_handler, 78041, 
                                                     path_to_dll))
    return false;
  return true;
}

LIB_libmatrix_C_API 
bool MW_CALL_CONV libmatrixInitialize(void)
{
  return libmatrixInitializeWithHandlers(mclDefaultErrorHandler, mclDefaultPrintHandler);
}

LIB_libmatrix_C_API 
void MW_CALL_CONV libmatrixTerminate(void)
{
  if (_mcr_inst != NULL)
    mclTerminateInstance(&_mcr_inst);
}

LIB_libmatrix_C_API 
long MW_CALL_CONV libmatrixGetMcrID() 
{
  return mclGetID(_mcr_inst);
}

LIB_libmatrix_C_API 
void MW_CALL_CONV libmatrixPrintStackTrace(void) 
{
  char** stackTrace;
  int stackDepth = mclGetStackTrace(_mcr_inst, &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_libmatrix_C_API 
bool MW_CALL_CONV mlxAddTwo(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
  return mclFeval(_mcr_inst, "AddTwo", nlhs, plhs, nrhs, prhs);
}

LIB_libmatrix_C_API 
bool MW_CALL_CONV mlxMultiplyTwo(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
  return mclFeval(_mcr_inst, "MultiplyTwo", nlhs, plhs, nrhs, prhs);
}

LIB_libmatrix_CPP_API 
void MW_CALL_CONV AddTwo(int nargout, mwArray& output, const mwArray& x, const mwArray& y)
{
  mclcppMlfFeval(_mcr_inst, "AddTwo", nargout, 1, 2, &output, &x, &y);
}

LIB_libmatrix_CPP_API 
void MW_CALL_CONV MultiplyTwo(int nargout, mwArray& output, const mwArray& x, const 
                              mwArray& y)
{
  mclcppMlfFeval(_mcr_inst, "MultiplyTwo", nargout, 1, 2, &output, &x, &y);
}


libmatrix.h

//
// MATLAB Compiler: 4.11 (R2009b)
// Date: Sat Mar 06 04:06:14 2010
// Arguments: "-B" "macro_default" "-W" "cpplib:libmatrix" "-T" "link:lib"
// "AddTwo" "MultiplyTwo" 
//

#ifndef __libmatrix_h
#define __libmatrix_h 1

#if defined(__cplusplus) && !defined(mclmcrrt_h) && defined(__linux__)
#  pragma implementation "mclmcrrt.h"
#endif
#include "mclmcrrt.h"
#include "mclcppclass.h"
#ifdef __cplusplus
extern "C" {
#endif

#if defined(__SUNPRO_CC)
/* Solaris shared libraries use __global, rather than mapfiles
 * to define the API exported from a shared library. __global is
 * only necessary when building the library -- files including
 * this header file to use the library do not need the __global
 * declaration; hence the EXPORTING_<library> logic.
 */

#ifdef EXPORTING_libmatrix
#define PUBLIC_libmatrix_C_API __global
#else
#define PUBLIC_libmatrix_C_API /* No import statement needed. */
#endif

#define LIB_libmatrix_C_API PUBLIC_libmatrix_C_API

#elif defined(_HPUX_SOURCE)

#ifdef EXPORTING_libmatrix
#define PUBLIC_libmatrix_C_API __declspec(dllexport)
#else
#define PUBLIC_libmatrix_C_API __declspec(dllimport)
#endif

#define LIB_libmatrix_C_API PUBLIC_libmatrix_C_API


#else

#define LIB_libmatrix_C_API

#endif

/* This symbol is defined in shared libraries. Define it here
 * (to nothing) in case this isn't a shared library. 
 */
#ifndef LIB_libmatrix_C_API 
#define LIB_libmatrix_C_API /* No special import/export declaration */
#endif

extern LIB_libmatrix_C_API 
bool MW_CALL_CONV libmatrixInitializeWithHandlers(
       mclOutputHandlerFcn error_handler, 
       mclOutputHandlerFcn print_handler);

extern LIB_libmatrix_C_API 
bool MW_CALL_CONV libmatrixInitialize(void);

extern LIB_libmatrix_C_API 
void MW_CALL_CONV libmatrixTerminate(void);



extern LIB_libmatrix_C_API 
void MW_CALL_CONV libmatrixPrintStackTrace(void);

extern LIB_libmatrix_C_API 
bool MW_CALL_CONV mlxAddTwo(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);

extern LIB_libmatrix_C_API 
bool MW_CALL_CONV mlxMultiplyTwo(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);

extern LIB_libmatrix_C_API 
long MW_CALL_CONV libmatrixGetMcrID();


#ifdef __cplusplus
}
#endif

#ifdef __cplusplus

/* On Windows, use __declspec to control the exported API */
#if defined(_MSC_VER) || defined(__BORLANDC__)

#ifdef EXPORTING_libmatrix
#define PUBLIC_libmatrix_CPP_API __declspec(dllexport)
#else
#define PUBLIC_libmatrix_CPP_API __declspec(dllimport)
#endif

#define LIB_libmatrix_CPP_API PUBLIC_libmatrix_CPP_API

#else

#if !defined(LIB_libmatrix_CPP_API)
#if defined(LIB_libmatrix_C_API)
#define LIB_libmatrix_CPP_API LIB_libmatrix_C_API
#else
#define LIB_libmatrix_CPP_API /* empty! */ 
#endif
#endif

#endif

extern LIB_libmatrix_CPP_API void MW_CALL_CONV AddTwo(int nargout, mwArray& output, const mwArray& x, const mwArray& y);

extern LIB_libmatrix_CPP_API void MW_CALL_CONV MultiplyTwo(int nargout, mwArray& output, const mwArray& x, const mwArray& y);

#endif
#endif


I know this is a long message. I hope someone can help! Thanks! I've tried pretty much everywhere!
AnswerRe: Compiling .cpp made from Matlab > Errors Pin
Eugen Podsypalnikov6-Mar-10 8:57
Eugen Podsypalnikov6-Mar-10 8:57 
GeneralRe: Compiling .cpp made from Matlab > Errors Pin
skyhr6-Mar-10 9:47
skyhr6-Mar-10 9:47 
GeneralRe: Compiling .cpp made from Matlab > Errors Pin
Richard MacCutchan6-Mar-10 21:26
mveRichard MacCutchan6-Mar-10 21:26 
GeneralRe: Compiling .cpp made from Matlab > Errors Pin
Eugen Podsypalnikov7-Mar-10 4:19
Eugen Podsypalnikov7-Mar-10 4:19 
AnswerRe: Compiling .cpp made from Matlab > Errors Pin
Avi Berger6-Mar-10 21:28
Avi Berger6-Mar-10 21:28 
QuestionHow to pass datas to a modal dialog? Pin
Aric Wang5-Mar-10 23:29
Aric Wang5-Mar-10 23:29 
AnswerRe: How to pass datas to a modal dialog? Pin
Richard MacCutchan5-Mar-10 23:33
mveRichard MacCutchan5-Mar-10 23:33 
AnswerRe: How to pass datas to a modal dialog? Pin
Eugen Podsypalnikov6-Mar-10 8:31
Eugen Podsypalnikov6-Mar-10 8:31 
AnswerRe: How to pass datas to a modal dialog? Pin
enhzflep6-Mar-10 19:33
enhzflep6-Mar-10 19:33 
Question[MFC] Error when call function CreateDialog in method InitInstance in order to create a modeless dialog as main window [modified] Pin
rudyono5-Mar-10 22:59
rudyono5-Mar-10 22:59 
AnswerRe: [MFC] Error when call function CreateDialog in method InitInstance in order to create a modeless dialog as main window Pin
Richard MacCutchan5-Mar-10 23:05
mveRichard MacCutchan5-Mar-10 23:05 
GeneralRe: [MFC] Error when call function CreateDialog in method InitInstance in order to create a modeless dialog as main window Pin
rudyono5-Mar-10 23:13
rudyono5-Mar-10 23:13 
GeneralRe: [MFC] Error when call function CreateDialog in method InitInstance in order to create a modeless dialog as main window Pin
Richard MacCutchan5-Mar-10 23:29
mveRichard MacCutchan5-Mar-10 23:29 
AnswerRe: [MFC] Error when call function CreateDialog in method InitInstance in order to create a modeless dialog as main window Pin
Eugen Podsypalnikov6-Mar-10 8:51
Eugen Podsypalnikov6-Mar-10 8:51 
AnswerRe: [MFC] Error when call function CreateDialog in method InitInstance in order to create a modeless dialog as main window Pin
Avi Berger6-Mar-10 17:25
Avi Berger6-Mar-10 17:25 
GeneralRe: [MFC] Error when call function CreateDialog in method InitInstance in order to create a modeless dialog as main window Pin
rudyono12-Mar-10 4:23
rudyono12-Mar-10 4:23 
Questionconvert a CString value to DWORD Pin
learningvisualc5-Mar-10 20:43
learningvisualc5-Mar-10 20:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.